rb-data-table
Version:
Angular Smart Table
89 lines • 3.04 kB
JavaScript
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 var deepExtend = function () {
var objects = [];
for (var _i = 0; _i < arguments.length; _i++) {
objects[_i] = arguments[_i];
}
if (arguments.length < 1 || typeof arguments[0] !== 'object') {
return false;
}
if (arguments.length < 2) {
return arguments[0];
}
var target = arguments[0];
// convert arguments to array and cut off target object
var args = Array.prototype.slice.call(arguments, 1);
var val, src;
args.forEach(function (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;
};
var Deferred = /** @class */ (function () {
function Deferred() {
var _this = this;
this.promise = new Promise(function (resolve, reject) {
_this.resolve = resolve;
_this.reject = reject;
});
}
return Deferred;
}());
export { Deferred };
// getDeepFromObject({result: {data: 1}}, 'result.data', 2); // returns 1
export function getDeepFromObject(object, name, defaultValue) {
if (object === void 0) { object = {}; }
var keys = name.split('.');
// clone the object
var level = deepExtend({}, object);
keys.forEach(function (k) {
if (level && typeof level[k] !== 'undefined') {
level = level[k];
}
});
return typeof level === 'undefined' ? defaultValue : level;
}
//# sourceMappingURL=helpers.js.map