glowers-radio/lib/util.js

52 lines
1.1 KiB
JavaScript

"use-strict";
const objSerialize = (obj, keys, cb) => {
let a = Object.create(null)
for (const key of keys)
a[key] = obj[key]
if (cb)
return cb(a, obj)
return a
}
const objBulkSet = (obj, target, pairs) => {
if (typeof obj[target] != "object")
obj[target] = Object.create(null)
for (const key in pairs)
if (pairs[key] != undefined)
obj[target][key] = pairs[key]
}
const fromConstToPascal = string => {
let a = ''
let b = true
for (const char of string)
if (char == '_')
b = true
else {
a += b ? char : char.toLowerCase()
b = false
}
return a
}
const notNullElse = (a, fallback) => a != null && a != undefined ? a : fallback
const arrSerializeSort = array => array.filter(Boolean).map((a,i) => a.serialize(i)).sort((a,b) => a.id.localeCompare(b.id))
const SCHEMA_VERSION = 8
const toRadioSongs = songs => ({
"version": SCHEMA_VERSION,
songs
})
module.exports = {
SCHEMA_VERSION,
objSerialize,
objBulkSet,
arrSerializeSort,
fromConstToPascal,
notNullElse,
toRadioSongs
}