gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit33072bb533072bb5|||PR Record day72 gates and Chrome proofmre33072bb5/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

  • 33072bb5|||PR Record day72 gates and Chrome proofmre
  • 95f2cd73|||PR Use typed parent methods for screen playback and local server facesmre
  • 6422a020Sync media-player at 51c3818mre
  • b582ba1cVendor complete Hybriel day72 plugin setmre
  • 02dccbc9Merge branch 'records-1119' into mainJukebox
  • a39e1c23|||PR Records: missions 013-015, reconnect fix livemre
  • fc7db075Merge branch 'reconnect-retry' into mainJukebox
  • ac6f4c6fL2: plain declaration for a in pickedNextmre
  • 2f335086Review round 2: only an explicit queue pick drops pend (not the automatic advance); jb23 records every source changemre
  • 3eb6695fReview fixes: action generation, queue pick clears pend, Previous/Next retry the decided request, typed handlersmre
  • 7a53c0c2|||PR A tap lost to a dead socket plays after reconnect (mission 015)mre
  • 1f0cd77bMerge branch 'records-day41' into mainJukebox
  • 67be229f|||PR Records: day41 live (PR #4), mission 012, statusmre
  • 0bb027daconductor: caramboleyo is the user, not owner/creatorApollo
  • 04f5d9d5Merge branch 'day41' into mainJukebox
  • 8ed6da45|||PR README: media-player pin df9e7dfmre
  • d5d45fc3|||PR media-player df9e7df (repeat-one fix), sync pin and SOURCEmre
  • 89cdabb1Vendor Hybriel day41 (57054d9e): plugin dirs, set .somre
  • 1fecd6a7Merge branch 'records-day39' into mainJukebox
  • d6b3e068|||PR Records: day39 live (PR #2), mission 011 PR step, statusmre