UNPKG

@bitblit/ratchet-common

Version:

Common tools for general use

173 lines 5.72 kB
import { Logger } from '../logger/logger.js'; import { ErrorRatchet } from './error-ratchet.js'; export class MapRatchet { static expandNestedKeysToObjects(src, separator = '.') { if (!separator || separator.length !== 1) { throw new Error('Invalid separator (must be single character)'); } const rval = {}; Object.keys(src).forEach((k) => { const path = k.split(separator); let target = rval; while (path.length > 1) { target[path[0]] = target[path[0]] || {}; target = target[path[0]]; path.splice(0, 1); } target[path[0]] = src[k]; }); return rval; } static mapByUniqueProperty(input, propName) { if (!input || !propName) { throw new Error('Neither input nor propName can be null'); } const rval = new Map(); input.forEach((i) => { const val = i ? i[propName] : null; if (val === null || val === undefined) { throw new Error('No value for ' + propName + ' found in ' + JSON.stringify(i)); } if (rval.has(val)) { throw new Error('Multiple values found for ' + val); } rval.set(val, i); }); return rval; } static groupByProperty(input, propName) { if (!input || !propName) { throw new Error('Neither input nor propName can be null'); } const rval = new Map(); input.forEach((i) => { const val = i ? i[propName] : null; if (val === null || val === undefined) { throw ErrorRatchet.fErr('No value for %s found in %j', propName, i); } if (!rval.has(val)) { rval.set(val, []); } rval.get(val).push(i); }); return rval; } static findValue(toSearch, path) { if (!path || path.length == 0) { return toSearch; } else { if (toSearch) { return MapRatchet.findValue(toSearch[path[0]], path.slice(1)); } else { return null; } } } static findValueDotPath(toSearch, dotPath) { if (!dotPath || dotPath.length == 0) { return toSearch; } else { if (toSearch) { return MapRatchet.findValue(toSearch, dotPath.split('.')); } else { return null; } } } static simpleDeepCompare(object1, object2) { if (object1 == null && object2 == null) return true; if (object1 == null || object2 == null) return false; return JSON.stringify(object1) == JSON.stringify(object2); } static toKeyValueList(value) { const returnArray = []; for (const k of Object.keys(value)) { returnArray.push({ key: k, value: value[k], }); } return returnArray; } static fromKeyValueList(list) { const rval = {}; list.forEach((a) => (rval[a.key] = a.value)); return rval; } static cleanup(obj, stripZero = false, stripNull = true, stripUndefined = true, stripEmptyString = true) { if (obj === null || obj === undefined || typeof obj !== 'object') { return obj; } const o = JSON.parse(JSON.stringify(obj)); Object.keys(o).forEach((key) => { if (o[key] && typeof o[key] === 'object') { if (Array.isArray(o[key])) { for (let i = 0; i < o[key].length; i++) { o[key][i] = MapRatchet.cleanup(o[key][i]); } } else { o[key] = MapRatchet.cleanup(o[key]); } } else if ((o[key] === undefined && stripUndefined) || (o[key] === null && stripNull) || (o[key] === '' && stripEmptyString) || (o[key] === 0 && stripZero)) { delete o[key]; } else { } }); return o; } static extractValueFromMapIgnoreCase(src, key) { let rval = null; if (src && key) { const finder = key.toLowerCase(); Object.keys(src).forEach((s) => { if (s.toLowerCase() === finder) { const newVal = src[s]; if (rval) { Logger.warn('Multiple entries found for %s (replacing %s with %s', key, rval, newVal); } rval = newVal; } }); } return rval; } static safeCallFunction(ob, fnName) { let rval = false; if (!!ob && !!ob[fnName] && typeof ob[fnName] === 'function') { try { ob[fnName](); rval = true; } catch (err) { Logger.warn('Error calling %s on %s : %s', fnName, ob, err, err); } } return rval; } static caseInsensitiveAccess(ob, keyName) { let rval = null; if (!!ob && !!keyName) { rval = ob[keyName]; if (!rval) { const keyNameCI = Object.keys(ob).find((f) => f.toLowerCase() === keyName.toLowerCase()); if (keyNameCI) { rval = ob[keyNameCI]; } } } return rval; } } //# sourceMappingURL=map-ratchet.js.map