@qrvey/utils 1.13.0-5 → 1.13.0-6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
* @param
|
|
4
|
-
* @
|
|
5
|
-
* @returns A new reference of the given argument
|
|
2
|
+
* The `objectCopy` function is a TypeScript function that creates a deep copy of an object or array.
|
|
3
|
+
* @param {T} obj - The `obj` parameter is the object that you want to make a copy of.
|
|
4
|
+
* @returns The function `objectCopy` returns a deep copy of the input object `obj`.
|
|
6
5
|
*/
|
|
7
|
-
export declare function objectCopy<T = any>(
|
|
6
|
+
export declare function objectCopy<T = any>(obj: T): T;
|
|
@@ -2,34 +2,24 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.objectCopy = void 0;
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
6
|
-
* @param
|
|
7
|
-
* @
|
|
8
|
-
* @returns A new reference of the given argument
|
|
5
|
+
* The `objectCopy` function is a TypeScript function that creates a deep copy of an object or array.
|
|
6
|
+
* @param {T} obj - The `obj` parameter is the object that you want to make a copy of.
|
|
7
|
+
* @returns The function `objectCopy` returns a deep copy of the input object `obj`.
|
|
9
8
|
*/
|
|
10
|
-
function objectCopy(
|
|
11
|
-
if (
|
|
12
|
-
return
|
|
13
|
-
const newCache = cache ? cache : new WeakMap();
|
|
14
|
-
if (newCache.has(entity))
|
|
15
|
-
return newCache.get(entity);
|
|
16
|
-
const c = new entity.constructor();
|
|
17
|
-
if (c.name === "Date")
|
|
18
|
-
return new Date(entity);
|
|
19
|
-
if (entity instanceof Map) {
|
|
20
|
-
entity.forEach((value, key) => c.set(objectCopy(key, newCache), objectCopy(value, newCache)));
|
|
9
|
+
function objectCopy(obj) {
|
|
10
|
+
if (obj === null || typeof obj !== "object") {
|
|
11
|
+
return obj;
|
|
21
12
|
}
|
|
22
|
-
if (
|
|
23
|
-
|
|
13
|
+
if (Array.isArray(obj)) {
|
|
14
|
+
const newArray = obj.map((item) => objectCopy(item));
|
|
15
|
+
return newArray;
|
|
24
16
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
17
|
+
const clonedObj = {};
|
|
18
|
+
for (const key in obj) {
|
|
19
|
+
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
20
|
+
clonedObj[key] = objectCopy(obj[key]);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return clonedObj;
|
|
29
24
|
}
|
|
30
25
|
exports.objectCopy = objectCopy;
|
|
31
|
-
function isClonable(entity) {
|
|
32
|
-
const referenceTypes = ["Array", "Object", "Map", "Set", "Date"];
|
|
33
|
-
const entityName = entity && entity.constructor.name;
|
|
34
|
-
return referenceTypes.includes(entityName);
|
|
35
|
-
}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
* @param
|
|
4
|
-
* @
|
|
5
|
-
* @returns A new reference of the given argument
|
|
2
|
+
* The `objectCopy` function is a TypeScript function that creates a deep copy of an object or array.
|
|
3
|
+
* @param {T} obj - The `obj` parameter is the object that you want to make a copy of.
|
|
4
|
+
* @returns The function `objectCopy` returns a deep copy of the input object `obj`.
|
|
6
5
|
*/
|
|
7
|
-
export declare function objectCopy<T = any>(
|
|
6
|
+
export declare function objectCopy<T = any>(obj: T): T;
|
|
@@ -1,31 +1,21 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
* @param
|
|
4
|
-
* @
|
|
5
|
-
* @returns A new reference of the given argument
|
|
2
|
+
* The `objectCopy` function is a TypeScript function that creates a deep copy of an object or array.
|
|
3
|
+
* @param {T} obj - The `obj` parameter is the object that you want to make a copy of.
|
|
4
|
+
* @returns The function `objectCopy` returns a deep copy of the input object `obj`.
|
|
6
5
|
*/
|
|
7
|
-
export function objectCopy(
|
|
8
|
-
if (
|
|
9
|
-
return
|
|
10
|
-
const newCache = cache ? cache : new WeakMap();
|
|
11
|
-
if (newCache.has(entity))
|
|
12
|
-
return newCache.get(entity);
|
|
13
|
-
const c = new entity.constructor();
|
|
14
|
-
if (c.name === "Date")
|
|
15
|
-
return new Date(entity);
|
|
16
|
-
if (entity instanceof Map) {
|
|
17
|
-
entity.forEach((value, key) => c.set(objectCopy(key, newCache), objectCopy(value, newCache)));
|
|
6
|
+
export function objectCopy(obj) {
|
|
7
|
+
if (obj === null || typeof obj !== "object") {
|
|
8
|
+
return obj;
|
|
18
9
|
}
|
|
19
|
-
if (
|
|
20
|
-
|
|
10
|
+
if (Array.isArray(obj)) {
|
|
11
|
+
const newArray = obj.map((item) => objectCopy(item));
|
|
12
|
+
return newArray;
|
|
21
13
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
const entityName = entity && entity.constructor.name;
|
|
30
|
-
return referenceTypes.includes(entityName);
|
|
14
|
+
const clonedObj = {};
|
|
15
|
+
for (const key in obj) {
|
|
16
|
+
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
17
|
+
clonedObj[key] = objectCopy(obj[key]);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return clonedObj;
|
|
31
21
|
}
|