tdl
Version:
Node.js bindings to TDLib (Telegram Database library)
36 lines (35 loc) • 1.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.mergeDeepRight = mergeDeepRight;
exports.deepRenameKey = deepRenameKey;
function isObject(item) {
return item != null && typeof item === 'object' && !Array.isArray(item);
}
function mergeDeepRight(obj1, obj2) {
if (isObject(obj1) && isObject(obj2)) {
const result = {};
for (const key2 in obj2) {
const val1 = obj1[key2];
const val2 = obj2[key2];
result[key2] = mergeDeepRight(val1, val2);
}
for (const key1 in obj1) {
if (!(key1 in result))
result[key1] = obj1[key1];
}
return result;
}
return obj2;
}
/** Renames `oldKey` to `newKey` deeply. The objects should not contain functions. */
function deepRenameKey(oldKey, newKey, v) {
if (Array.isArray(v))
return v.map(x => deepRenameKey(oldKey, newKey, x));
if (typeof v === 'object' && v !== null) {
const newObj = {};
for (const k in v)
newObj[k === oldKey ? newKey : k] = deepRenameKey(oldKey, newKey, v[k]);
return newObj;
}
return v;
}