matterbridge
Version:
Matterbridge plugin manager for Matter
75 lines • 2.53 kB
JavaScript
/**
* This file contains the deepCopy function.
*
* @file deepCopy.ts
* @author Luca Liguori
* @date 2025-02-16
* @version 1.0.0
*
* Copyright 2025, 2026, 2027 Luca Liguori.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License. *
*/
/**
* Creates a deep copy of the given value.
*
* @template T - The type of the value being copied.
* @param {T} value - The value to be copied.
* @returns {T} - The deep copy of the value.
*/
export function deepCopy(value) {
if (typeof value !== 'object' || value === null) {
// Primitive value (string, number, boolean, bigint, undefined, symbol) or null
return value;
}
else if (Array.isArray(value)) {
// Array: Recursively copy each element
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return value.map((item) => deepCopy(item));
}
else if (value instanceof Date) {
// Date objects
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return new Date(value.getTime());
}
else if (value instanceof Map) {
// Maps
const mapCopy = new Map();
value.forEach((val, key) => {
mapCopy.set(key, deepCopy(val));
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return mapCopy;
}
else if (value instanceof Set) {
// Sets
const setCopy = new Set();
value.forEach((item) => {
setCopy.add(deepCopy(item));
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return setCopy;
}
else {
// Objects: Create a copy with the same prototype as the original
const proto = Object.getPrototypeOf(value);
const copy = Object.create(proto);
for (const key in value) {
if (Object.prototype.hasOwnProperty.call(value, key)) {
copy[key] = deepCopy(value[key]);
}
}
return copy;
}
}
//# sourceMappingURL=deepCopy.js.map