UNPKG

ng2-stable-nxtc

Version:

Angular Smart Table neXtCode Version

81 lines 2.87 kB
import { cloneDeep } from 'lodash'; /** * Extending object that entered in first argument. * * Returns extended object or false if have no target object or incorrect type. * * If you wish to clone source object (without modify it), just use empty new * object as first argument, like this: * deepExtend({}, yourObj_1, [yourObj_N]); */ export const deepExtend = function (...objects) { if (arguments.length < 1 || typeof arguments[0] !== 'object') { return false; } if (arguments.length < 2) { return arguments[0]; } const target = arguments[0]; // convert arguments to array and cut off target object const args = Array.prototype.slice.call(arguments, 1); let val, src; args.forEach((obj) => { // skip argument if it is array or isn't object if (typeof obj !== 'object' || Array.isArray(obj)) { return; } Object.keys(obj).forEach(function (key) { src = target[key]; // source value val = obj[key]; // new value // recursion prevention if (val === target) { return; /** * if new value isn't object then just overwrite by new value * instead of extending. */ } else if (typeof val !== 'object' || val === null) { target[key] = val; return; // just clone arrays (and recursive clone objects inside) } else if (Array.isArray(val)) { target[key] = cloneDeep(val); return; // overwrite by new value if source isn't object or array } else if (typeof src !== 'object' || src === null || Array.isArray(src)) { target[key] = deepExtend({}, val); return; // source value and new value is objects both, extending... } else { target[key] = deepExtend(src, val); return; } }); }); return target; }; export class Deferred { constructor() { this.promise = new Promise((resolve, reject) => { this.resolve = resolve; this.reject = reject; }); } } // getDeepFromObject({result: {data: 1}}, 'result.data', 2); // returns 1 export function getDeepFromObject(object = {}, name, defaultValue) { const keys = name.split('.'); // clone the object let level = deepExtend({}, object); keys.forEach((k) => { if (level && typeof level[k] !== 'undefined') { level = level[k]; } }); return typeof level === 'undefined' ? defaultValue : level; } //# sourceMappingURL=helpers.js.map