utilitaire
Version:
Common utils for JavaScript and Typescript development
63 lines (62 loc) • 2.02 kB
JavaScript
;
/**
*
* @param item
* @returns {any|boolean}
*/
function isObject(item) {
return (item && typeof item === 'object' && !Array.isArray(item) && item !== null);
}
exports.isObject = isObject;
/*
// Polyfill for object assign
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
if (typeof Object.assign != 'function') {
Object.assign = function(target) {
'use strict';
if (target == null) {
throw new TypeError('Cannot convert undefined or null to object');
}
target = Object(target);
for (var index = 1; index < arguments.length; index++) {
var source = arguments[index];
if (source != null) {
for (var key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
}
}
}
return target;
};
}*/
// see http://stackoverflow.com/questions/27936772/deep-object-merging-in-es6-es7
/**
*
* @param target
* @param source
* @returns {({}&any)|any}
*/
// TODO THis function is buggy. It canot be used accurately in HasDescriptor.updateInPlace or copy
function deepAssign(target, source) {
var output = Object.assign({}, target);
if (isObject(target) && isObject(source)) {
Object.keys(source).forEach(function (key) {
if (isObject(source[key])) {
if (!(key in target))
Object.assign(output, (_a = {}, _a[key] = source[key], _a));
else
output[key] = deepAssign(target[key], source[key]);
}
else {
Object.assign(output, (_b = {}, _b[key] = source[key], _b));
}
var _a, _b;
});
}
return output;
}
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = deepAssign;
//# sourceMappingURL=DeepAssign.js.map