gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit1838d7e81838d7e8Records: S1 startedmre1838d7e8/lib/util.hl

3.7 KB

  1. // lib/util.hl — small helpers every topic shares: env, the storage folders, lists, text folding for search and sorting,
  2. // numbers from text. Statics only; no topic logic here.
  3. import { env } from 'hl:proc'
  4. // a newline (the language has no \n escape in a string: a literal line break)
  5. static NL = "
  6. "
  7. // the separator of sort keys: lower than every character a name can hold, so "alt" sorts before "alt lied"
  8. static SEP = String.fromCharCode(1)
  9. // A KEY FOR AN INDEX HAS NO COMMA: hl:mpackdb keeps an index as lines `key,offset,length`, and a key with a comma is lost when the
  10. // table is opened again (repro .scratch/mpackdb-comma-key/). Every indexed text goes through here: the comma becomes character 2.
  11. static safeKey = (s) => { return s.replaceAll(',', String.fromCharCode(2)) }
  12. static envOr = (name, fallback) => {
  13. v = env(name)
  14. return v != null && v.trim() != '' ? v.trim() : fallback
  15. }
  16. // WHERE THE APP WRITES: the table folder of the mpackdb tables, the settings file and the cover cache. All under
  17. // storage/ of the project (JUKEBOX_STORAGE overrides). A relative path resolves against the ENTRY SCRIPT's directory.
  18. // The music library itself is never written (README "Read only").
  19. static storageRoot = envOr('JUKEBOX_STORAGE', './storage')
  20. static storageDir = storageRoot + '/mpackdb'
  21. static coverDir = storageRoot + '/covers'
  22. static settingsFile = storageRoot + '/settings.json'
  23. // an empty list read from hl:mpackdb comes back without a `.length`: every found list goes through here
  24. static countOfList = (list) => {
  25. if (list == null) { return 0 }
  26. n = list.length
  27. return n == null ? 0 : n
  28. }
  29. // ---- numbers out of text (the language has no parseInt) --------------------------------------------
  30. // the leading run of digits of a text as a Number, or null when it does not start with one ("11/21" → 11)
  31. static leadingInt = (s) => {
  32. if (s == null) { return null }
  33. let n = 0
  34. let i = 0
  35. let seen = false
  36. while (i < s.length) {
  37. c = s.charCodeAt(i)
  38. if (c < 48 || c > 57) { break }
  39. n = n * 10 + (c - 48)
  40. seen = true
  41. i = i + 1
  42. }
  43. return seen ? n : null
  44. }
  45. // a Number as digits padded with zeros to `width` (sort keys: "007")
  46. static padNumber = (n, width) => {
  47. let s = '' + n
  48. while (s.length < width) { s = '0' + s }
  49. return s
  50. }
  51. // ---- text folding: lower case, accents folded away (search and sort keys) --------------------------------
  52. static foldMap = {
  53. 'ä' = 'a' 'ö' = 'o' 'ü' = 'u' 'ß' = 'ss' 'à' = 'a' 'á' = 'a' 'â' = 'a' 'ã' = 'a' 'å' = 'a' 'æ' = 'ae'
  54. 'ç' = 'c' 'è' = 'e' 'é' = 'e' 'ê' = 'e' 'ë' = 'e' 'ì' = 'i' 'í' = 'i' 'î' = 'i' 'ï' = 'i' 'ð' = 'd'
  55. 'ñ' = 'n' 'ò' = 'o' 'ó' = 'o' 'ô' = 'o' 'õ' = 'o' 'ø' = 'o' 'ù' = 'u' 'ú' = 'u' 'û' = 'u' 'ý' = 'y'
  56. 'ÿ' = 'y' 'þ' = 'th' 'œ' = 'oe' 'š' = 's' 'ž' = 'z' 'č' = 'c' 'ć' = 'c' 'ł' = 'l' 'ń' = 'n' 'ř' = 'r'
  57. 'ś' = 's' 'ź' = 'z' 'ż' = 'z' 'ď' = 'd' 'ě' = 'e' 'ę' = 'e' 'ą' = 'a' 'ő' = 'o' 'ű' = 'u' 'ů' = 'u'
  58. 'ğ' = 'g' 'ı' = 'i' 'ş' = 's' 'ț' = 't' 'ť' = 't' 'ň' = 'n' 'ā' = 'a' 'ē' = 'e' 'ī' = 'i' 'ō' = 'o' 'ū' = 'u'
  59. }
  60. static fold = (s) => {
  61. if (s == null) { return '' }
  62. low = s.toLowerCase()
  63. let out = ''
  64. let i = 0
  65. while (i < low.length) {
  66. ch = low[i]
  67. r = foldMap[ch]
  68. out = out + (r == null ? ch : r)
  69. i = i + 1
  70. }
  71. return out
  72. }
  73. // the file name without folders and extension
  74. static baseName = (path) => {
  75. let n = path
  76. k = n.lastIndexOf('/')
  77. if (k >= 0) { n = n.slice(k + 1) }
  78. d = n.lastIndexOf('.')
  79. if (d > 0) { n = n.slice(0, d) }
  80. return n
  81. }
  82. static folderOf = (path) => {
  83. k = path.lastIndexOf('/')
  84. return k > 0 ? path.slice(0, k) : '/'
  85. }
  86. static extOf = (path) => {
  87. k = path.lastIndexOf('.')
  88. s = path.lastIndexOf('/')
  89. return k > s ? path.slice(k + 1).toLowerCase() : ''
  90. }

Branches

Latest commits

  • 1838d7e8Records: S1 startedmre
  • a43fca3fRecords: mission 001 decided pointsmre
  • 41773828Records: statusmre
  • a82c0805Records: mission 001 brief (Style form)mre
  • a4bce6d1Records: ticket push subscriptionmre
  • 14abde3bjukebox: records: Atlas's records moved to worldapi-conductor/archivemre
  • 0433e9a8jukebox: records: gitoria remote added and pushedmre
  • 1722d707jukebox: records: repo creation handed to Gitoriamre
  • b42e7dfbjukebox: conductor records (CONDUCTOR.md, STATUS.md), old STATUS archivedmre
  • 1b0d0bc1jukebox: a startup error prints its message before exit 1 (hybriel#247)mre
  • b19c478bjukebox JB18 (jukebox#17): onto Hybriel set-day31 (b287cd82, contains the SIGPIPE fix 6d2ca454): plugins/core and plugins/web re-vendored, bin/ from the set; a client that closes /stream while it is written no longer kills the server (90 aborted streams survived, the old bin died with exit 141 on the first)mre
  • 73867a2ejukebox#2: Style blocks in the language's nested form (rules named after the View's own elements, pseudo-classes nested, merged duplicates); the same CSS rules, the pages look the samemre
  • 06c548d6jukebox JB15: report, log linemre
  • 980859cbjukebox JB15 (jukebox#15): the now-playing overlay on a short screen (tablet or phone on its side) gave the queue 12 to 100 px because the big cover took the room; the cover now shrinks with the screen height and goes away under 41rem, the queue keeps 3+ rows; gate tests/jb15.mjs at 8 sizesmre
  • 9efc40ffjukebox#12 #14: Grok session 1793246574527-181160mre
  • 7d58b36ejukebox JB13 (jukebox#13): Next and Previous ask for the songs after / before the current song's id in the current order, not for place + 1; gate tests/jb13.mjs checks Next with the queue down to one song after the order changedmre
  • 51358794jukebox JB14 (jukebox#14): the Songs screen opens sorted the way it was left (kept on the server in storage/ui.json), the sort control shows the sort the rows really have (selected options, autocomplete off); gate tests/jb14.mjsmre
  • cc68a063jukebox JB13 (jukebox#13): a tapped song plays the song that was tapped, found by its id in the list as it is now (not by its row number, which counted plays move); the queue goes on from its place there, the rows refresh on the tap; gate tests/jb13.mjs (songs by plays, favourites)mre
  • 88d3d5c1jukebox JB12b (jukebox#12): the scroll handle is big (2.75rem x 4rem, accent colour, grip) and drawn on every device, no media query; native bar hidden; gate tests/touch.mjs checks the drawn width with and without touchmre
  • b58e4495jukebox JB11 (jukebox#11): the next song starts in the ended event on the same audio element, so it also starts with the screen locked; gate tests/jb11.mjs (hidden tab, no frames)mre