cerceis-lib
Version:
Contains list of quality of life functions that is written in TypeScript and es6
140 lines (139 loc) • 4.55 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/object/index.ts
var object_exports = {};
__export(object_exports, {
FromObject: () => FromObject
});
module.exports = __toCommonJS(object_exports);
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;
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
FromObject
});
//# sourceMappingURL=index.js.map