@angular-devkit/core
Version:
Angular DevKit - Core Utility Library
37 lines (36 loc) • 1.16 kB
JavaScript
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.deepCopy = deepCopy;
const copySymbol = Symbol();
function deepCopy(value) {
if (Array.isArray(value)) {
return value.map((o) => deepCopy(o));
}
else if (value && typeof value === 'object') {
const valueCasted = value;
if (valueCasted[copySymbol]) {
// This is a circular dependency. Just return the cloned value.
return valueCasted[copySymbol];
}
if (valueCasted['toJSON']) {
return JSON.parse(valueCasted['toJSON']());
}
const copy = Object.create(Object.getPrototypeOf(valueCasted));
valueCasted[copySymbol] = copy;
for (const key of Object.getOwnPropertyNames(valueCasted)) {
copy[key] = deepCopy(valueCasted[key]);
}
delete valueCasted[copySymbol];
return copy;
}
else {
return value;
}
}
;