UNPKG

cerceis-lib

Version:

Contains list of quality of life functions that is written in TypeScript and es6

115 lines 3.54 kB
// src/object/index.ts var _isObj = (x) => Object.prototype.toString.call(x) === "[object Object]"; var _isNumber = (x) => Object.prototype.toString.call(x) === "[object Number]"; var FromObject = { /** * Convert an object into a 2D array ([[key, value], ...]). * @param inputObj Input object. */ ObjectToArray(inputObj) { return Object.keys(inputObj).map((key) => [key, inputObj[key]]); }, /** * Flatten a multi-level object into a single level. * Duplicate keys are given a generated ID when overwriteKey is false. * @param obj Object to flatten. * @param overwriteKey Overwrite duplicate keys (default false). */ flatten(obj, overwriteKey = false) { if (Object.keys(obj).length === 0) return {}; const rs = {}; const genId = () => (Date.now() / 1e3 | 0).toString(16) + "xxxxxxxxxxxxxxxx".replace(/x/g, () => (Math.random() * 16 | 0).toString(16)); const recurse = (o) => { for (const key in o) { const val = o[key]; if (_isObj(val)) { recurse(val); } else if (key in rs && !overwriteKey) { rs[genId()] = val; } else { rs[key] = val; } } }; recurse(obj); return rs; }, /** * Find and return the deepest nested object. * Returns the first deepest object found; or all if multi = true. * @param obj Input object. * @param multi Return all objects at the deepest level. */ getDeepest(obj, multi = false) { if (Object.keys(obj).length === 0) return {}; let deepest = 0; let rs = {}; const rsMulti = {}; const recurse = (o, depth = 0) => { depth++; if (depth > deepest) { deepest = depth; rs = o; if (multi) rsMulti[depth] = [o]; } else if (depth === deepest && multi) { if (!rsMulti[depth]) rsMulti[depth] = []; rsMulti[depth].push(o); } for (const key in o) { const val = o[key]; if (_isObj(val)) recurse(val, depth); } }; recurse(obj); return multi ? rsMulti[deepest] ?? [] : rs; }, /** * Sum all numeric values in an object (supports single-level nesting via field). * @param obj Input object. * @param field Target field when values are nested objects. */ sumAll(obj, field = "") { let sum = 0; for (const prop in obj) { const raw = obj[prop]; const t = field !== "" ? raw[field] : raw; if (!_isNumber(t)) throw new Error(`Value of property "${prop}" is not a number.`); sum += t; } return sum; }, /** * Find the maximum numeric value in an object. * @param obj Input object. * @param field Target field when values are nested objects. */ max(obj, field = "") { let max = -Infinity; for (const prop in obj) { const raw = obj[prop]; const t = field !== "" ? raw[field] : raw; if (!_isNumber(t)) throw new Error(`Value of property "${prop}" is not a number.`); if (t > max) max = t; } return max; }, /** * Find the minimum numeric value in an object. * @param obj Input object. * @param field Target field when values are nested objects. */ min(obj, field = "") { let min = null; for (const prop in obj) { const raw = obj[prop]; const t = field !== "" ? raw[field] : raw; if (!_isNumber(t)) throw new Error(`Value of property "${prop}" is not a number.`); if (min === null || t < min) min = t; } return min; } }; export { FromObject }; //# sourceMappingURL=index.mjs.map