UNPKG

@topgroup/diginext

Version:

A BUILD SERVER & CLI to deploy apps to any Kubernetes clusters.

38 lines (37 loc) 1.43 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.flattenObject = void 0; const lodash_1 = require("lodash"); const mongoose_1 = __importDefault(require("mongoose")); /** * Flatten the object into 1-level-object (with key paths) * @example * const flattenObj = flattenObject({a: {b: [{c: 1}, {c: 2}]}, e: 3}); * console.log(flattenObj); // {"a.b.0.c": 1, "a.b.1.c": 2, "e": 3} */ function flattenObject(obj) { const flattenKeys = (_obj, prefix = "") => { return Object.keys(_obj).reduce((acc, key) => { const newKey = prefix ? `${prefix}.${key}` : key; const value = _obj[key]; if (typeof value === "object" && value !== null && !(value instanceof mongoose_1.default.Types.ObjectId) && !(value instanceof Date)) { Object.assign(acc, flattenKeys(value, newKey)); } else { acc[newKey] = value; } return acc; }, {}); }; const flattenedObj = flattenKeys(obj); const mappedObj = (0, lodash_1.mapKeys)(flattenedObj, (value, key) => { const keys = key.split("."); const newKey = keys.join("."); return newKey; }); return mappedObj; } exports.flattenObject = flattenObject;