gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit28a82fa228a82fa2jukebox: README, STATUS, LOG, DECISIONSmre28a82fa2/lib/util.hl

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

Branches

Latest commits

  • 28a82fa2jukebox: README, STATUS, LOG, DECISIONSmre
  • d13987dfjukebox: library scanner, ID3 reader, mpackdb database and JSON API, with fixtures and a gatemre