gitoria

tickets

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commita3cd5329a3cd5329Tickets conductor: tickets#51 opened, brief 026 draftmrea3cd5329/lib/util.hl

6.1 KB

  1. // lib/util.hl — small helpers every topic shares (env, storage path, URLs, lists, text checks, Vienna time).
  2. // Statics only; no topic logic here.
  3. import { env } from 'hl:proc'
  4. import { timestamp } from 'hl:time'
  5. // ---- env and storage -----------------------------------------------------------------------
  6. static envOr = (name, fallback) => {
  7. v = env(name)
  8. return v != null && v.trim() != '' ? v.trim() : fallback
  9. }
  10. // THE TABLE DIRECTORY of every hl:mpackdb table (storage/mpackdb/; TICKETS_STORAGE moves it — the gate runs on its
  11. // own). Relative paths resolve against the ENTRY SCRIPT's directory (project.hl's) — tools must pass an absolute one.
  12. static storageDir = env('TICKETS_STORAGE') != null ? env('TICKETS_STORAGE') : './storage/mpackdb'
  13. // ---- the URLs a person uses ------------------------------------------------------------------
  14. // ticket #25: a project's page is /<slug>, a ticket's /<slug>/<number> (the old /projects/… forms answer 301);
  15. // the API keeps its /api/projects/… paths
  16. static ticketHref = (project, number) => { return '/' + project + '/' + number }
  17. static ticketApiHref = (project, number) => { return '/api/projects/' + project + '/tickets/' + number }
  18. static projectHrefOf = (project) => { return '/' + project }
  19. static settingsHrefOf = (project) => { return '/projects/' + project + '/settings' }
  20. // ---- lists and records -----------------------------------------------------------------------
  21. // an empty list written to hl:mpackdb comes back as an empty hybrid (`.length` null):
  22. // every read of a found list goes through here
  23. static countOf = (list) => {
  24. if (list == null) { return 0 }
  25. n = list.length
  26. return n == null ? 0 : n
  27. }
  28. static first = (list) => { return countOf(list) > 0 ? list[0] : null }
  29. // every record is written as a whole (hl:mpackdb's update replaces it)
  30. static merged = (rec, changes) => {
  31. out = {}
  32. for (k of rec.keys()) { out[k] = rec[k] }
  33. for (k of changes.keys()) { out[k] = changes[k] }
  34. return out
  35. }
  36. // insertion sorts (no list sort() in the language, hybriel #6) — stable; they sort their copy and return it
  37. static oldestFirst = (rows, field) => {
  38. let i = 1
  39. while (i < rows.length) {
  40. cur = rows[i]
  41. let j = i - 1
  42. while (j >= 0 && rows[j][field] > cur[field]) {
  43. rows[j + 1] = rows[j]
  44. j = j - 1
  45. }
  46. rows[j + 1] = cur
  47. i = i + 1
  48. }
  49. return rows
  50. }
  51. static newestFirst = (rows, field) => {
  52. let i = 1
  53. while (i < rows.length) {
  54. cur = rows[i]
  55. let j = i - 1
  56. while (j >= 0 && rows[j][field] < cur[field]) {
  57. rows[j + 1] = rows[j]
  58. j = j - 1
  59. }
  60. rows[j + 1] = cur
  61. i = i + 1
  62. }
  63. return rows
  64. }
  65. // ---- text ---------------------------------------------------------------------------------------
  66. static clean = (s) => { return s == null ? '' : ('' + s).trim() }
  67. // a SUBJECT is one line (mission ST): a line break or other control character in it becomes a space — it stands in a heading of the
  68. // Markdown view, in list rows, in a <title>, in a mail subject; a break there let a writer start their own heading / lines in what
  69. // programs and agents read. (The summary and comments are the multi-line texts.)
  70. static oneLine = (s) => {
  71. t = clean(s)
  72. let out = ''
  73. let i = 0
  74. while (i < t.length) {
  75. c = t.charCodeAt(i)
  76. out = out + (c < 32 || c == 127 ? ' ' : t[i])
  77. i = i + 1
  78. }
  79. return out.trim()
  80. }
  81. // a query value, percent-encoded (encodeURIComponent is a global, hybriel#14)
  82. static encode = (s) => { return encodeURIComponent('' + s) }
  83. // a string of ASCII digits only (a ticket number in a URL); '' is not
  84. static isDigits = (s) => {
  85. if (s == null) { return false }
  86. t = '' + s
  87. if (t.length == 0 || t.length > 12) { return false }
  88. let i = 0
  89. while (i < t.length) {
  90. c = t.charCodeAt(i)
  91. if (c < 48 || c > 57) { return false }
  92. i = i + 1
  93. }
  94. return true
  95. }
  96. // only lowercase hex (ident's one-time codes are 48 hex)
  97. static isHex = (s, max) => {
  98. if (s == null || hlTypeName(s) != 'String' || s.length == 0 || s.length > max) { return false }
  99. let i = 0
  100. while (i < s.length) {
  101. c = s.charCodeAt(i)
  102. if (!((c >= 48 && c <= 57) || (c >= 97 && c <= 102))) { return false }
  103. i = i + 1
  104. }
  105. return true
  106. }
  107. // a short one-line text: trimmed, at most `max` characters, no control characters
  108. static plainError = (s, max, what) => {
  109. if (s == null || hlTypeName(s) != 'String') { return what + ' must be text' }
  110. t = s.trim()
  111. if (t.length > max) { return what + ' is too long (at most ' + max + ' characters)' }
  112. let i = 0
  113. while (i < t.length) {
  114. c = t.charCodeAt(i)
  115. if (c < 32 || c == 127) { return what + ' must be one line without control characters' }
  116. i = i + 1
  117. }
  118. return null
  119. }
  120. // ---- time: epoch ms → "YYYY-MM-DD HH:MM" in Europe/Vienna (ticket #10) ---------------------------
  121. // hl:time has no time zones (now() + a UTC timestamp() only; "timezones are a userland
  122. // concern" — its header). This is the smallest correct userland rule for Vienna: the EU
  123. // rule in force since 1996 — CEST (UTC+2) from the last Sunday of March 01:00 UTC to the
  124. // last Sunday of October 01:00 UTC, CET (UTC+1) otherwise. Storage stays epoch ms (UTC);
  125. // only the rendering shifts. Checked against node's Intl (tests/localtime.hl + the gate).
  126. static DAY = 86400000
  127. static HOUR = 3600000
  128. // integer division for a >= 0 (the language has `%` but no floor in the core)
  129. static idiv = (a, b) => { return (a - a % b) / b }
  130. // days since 1970-01-01 of a civil date (H. Hinnant's days_from_civil, years >= 1970)
  131. static daysFromCivil = (y, m, d) => {
  132. yy = m <= 2 ? y - 1 : y
  133. era = idiv(yy, 400)
  134. yoe = yy - era * 400
  135. mp = m > 2 ? m - 3 : m + 9
  136. doy = idiv(153 * mp + 2, 5) + d - 1
  137. doe = yoe * 365 + idiv(yoe, 4) - idiv(yoe, 100) + doy
  138. return era * 146097 + doe - 719468
  139. }
  140. // the last Sunday of a 31-day month (March, October), 01:00 UTC, as epoch ms
  141. static lastSundayAt1 = (y, m) => {
  142. days = daysFromCivil(y, m, 31)
  143. wd = (days + 4) % 7 // 0 = Sunday: 1970-01-01 was a Thursday
  144. return (days - wd) * DAY + HOUR
  145. }
  146. static viennaOffset = (ms) => {
  147. y = toNumber(timestamp(ms).slice(0, 4))
  148. return ms >= lastSundayAt1(y, 3) && ms < lastSundayAt1(y, 10) ? 2 * HOUR : HOUR
  149. }
  150. // "YYYY-MM-DD HH:MM", Vienna wall time — what a person reads
  151. static localStamp = (ms) => {
  152. t = timestamp(ms + viennaOffset(ms))
  153. return t.slice(0, 10) + ' ' + t.slice(11, 16)
  154. }

Branches

Latest commits

  • a3cd5329Tickets conductor: tickets#51 opened, brief 026 draftmre
  • 6f6e5797Tickets conductor: STATUS wordingmre
  • 957de51fTickets conductor: T36 live, #36 review, #50 donemre
  • bc2fde32Merge branch 'main' into t36-stylemre
  • b7ca093atests: wait for the new row and normalize editor marker spacesmre
  • 41da3525Tickets conductor: flake cause, T36 deploy stepmre
  • 64c477e6Tickets conductor: deploy waits for Arachnemre
  • 467d7286Tickets conductor: T36 done, deploy questionmre
  • fed5ca81ticket_list: remove repeated selected-state coloursmre
  • 8542fc8aTickets conductor: T36 proof reviewedmre
  • 35980936inbox: preserve desktop columns after list styles were scopedmre
  • 2a5b8b91Tickets conductor: T36 cap stop, resumed for proofmre
  • c077a5cbstyles: format Style with nested language rulesmre
  • ccdde2beticket: format Style with nested language rulesmre
  • d083535bticket_list: nest Style rules and bare media queriesmre
  • c24608ebmarkdown: format Style with nested language rulesmre
  • de964b3bsettings: format Style with nested language rulesmre
  • 03c1de05new_project: format Style with nested language rulesmre
  • bd76cfa6connect: format Style with nested language rulesmre
  • 01fad593inbox: format Style with nested language rulesmre