@ditojs/utils 1.14.0 → 1.16.0
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.
- package/package.json +2 -2
- package/src/object/clone.js +21 -6
- package/src/object/clone.test.js +26 -8
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ditojs/utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.16.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Dito.js Utility Functions – Dito.js is a declarative and modern web framework, based on Objection.js, Koa.js and Vue.js",
|
|
6
6
|
"repository": "https://github.com/ditojs/dito/tree/master/packages/utils",
|
|
@@ -35,5 +35,5 @@
|
|
|
35
35
|
"typescript": "^4.9.3"
|
|
36
36
|
},
|
|
37
37
|
"types": "types",
|
|
38
|
-
"gitHead": "
|
|
38
|
+
"gitHead": "e7e7ddb88965dbffc29c0968054015a647844803"
|
|
39
39
|
}
|
package/src/object/clone.js
CHANGED
|
@@ -3,7 +3,17 @@ import {
|
|
|
3
3
|
} from '../base/index.js'
|
|
4
4
|
import { pick } from '../object/index.js'
|
|
5
5
|
|
|
6
|
-
export function clone(arg,
|
|
6
|
+
export function clone(arg, options) {
|
|
7
|
+
const {
|
|
8
|
+
shallow = false,
|
|
9
|
+
enumerable = true,
|
|
10
|
+
processValue = null
|
|
11
|
+
} = isFunction(options)
|
|
12
|
+
? { processValue: options } // TODO: `callback` deprecated in December 2022.
|
|
13
|
+
: options || {}
|
|
14
|
+
|
|
15
|
+
const handleValue = value => shallow ? value : clone(value, options)
|
|
16
|
+
|
|
7
17
|
let copy = arg
|
|
8
18
|
if (isDate(arg)) {
|
|
9
19
|
copy = new arg.constructor(+arg)
|
|
@@ -12,12 +22,12 @@ export function clone(arg, callback = null) {
|
|
|
12
22
|
} else if (isArray(arg)) {
|
|
13
23
|
copy = new arg.constructor(arg.length)
|
|
14
24
|
for (let i = 0, l = arg.length; i < l; i++) {
|
|
15
|
-
copy[i] =
|
|
25
|
+
copy[i] = handleValue(arg[i])
|
|
16
26
|
}
|
|
17
27
|
} else if (isObject(arg)) {
|
|
18
28
|
// Rely on arg.clone() if it exists and assume it creates an actual clone.
|
|
19
29
|
if (isFunction(arg.clone)) {
|
|
20
|
-
copy = arg.clone()
|
|
30
|
+
copy = arg.clone(options)
|
|
21
31
|
} else if (isPromise(arg)) {
|
|
22
32
|
// https://stackoverflow.com/questions/37063293/can-i-clone-a-promise
|
|
23
33
|
copy = arg.then()
|
|
@@ -25,10 +35,15 @@ export function clone(arg, callback = null) {
|
|
|
25
35
|
// Prevent calling the actual constructor since it is not guaranteed to
|
|
26
36
|
// work as intended here, and only clone the non-inherited own properties.
|
|
27
37
|
copy = Object.create(Object.getPrototypeOf(arg))
|
|
28
|
-
|
|
29
|
-
|
|
38
|
+
const keys = enumerable ? Object.keys(arg) : Reflect.ownKeys(arg)
|
|
39
|
+
for (const key of keys) {
|
|
40
|
+
const desc = Reflect.getOwnPropertyDescriptor(arg, key)
|
|
41
|
+
if (desc.value != null) {
|
|
42
|
+
desc.value = handleValue(desc.value)
|
|
43
|
+
}
|
|
44
|
+
Reflect.defineProperty(copy, key, desc)
|
|
30
45
|
}
|
|
31
46
|
}
|
|
32
47
|
}
|
|
33
|
-
return pick(
|
|
48
|
+
return pick(processValue?.(copy), copy)
|
|
34
49
|
}
|
package/src/object/clone.test.js
CHANGED
|
@@ -44,6 +44,12 @@ describe('clone()', () => {
|
|
|
44
44
|
expect(copy.b).not.toBe(object.b)
|
|
45
45
|
})
|
|
46
46
|
|
|
47
|
+
it('should support shallow-cloning', () => {
|
|
48
|
+
const object = { a: [1, 2, 3] }
|
|
49
|
+
const copy = clone(object, { shallow: true })
|
|
50
|
+
expect(object.a).toBe(copy.a)
|
|
51
|
+
})
|
|
52
|
+
|
|
47
53
|
it('should use clone() methods if available', () => {
|
|
48
54
|
const object = {
|
|
49
55
|
a: 1,
|
|
@@ -54,14 +60,24 @@ describe('clone()', () => {
|
|
|
54
60
|
expect(copy).toStrictEqual({ b: 2 })
|
|
55
61
|
})
|
|
56
62
|
|
|
57
|
-
it('should
|
|
63
|
+
it('should handle non-enumerable properties', async () => {
|
|
64
|
+
const object = { a: 1 }
|
|
65
|
+
Object.defineProperty(object, 'b', { value: 2, enumerable: false })
|
|
66
|
+
const copy = clone(object, { enumerable: false })
|
|
67
|
+
expect(copy.a).toEqual(1)
|
|
68
|
+
expect(copy.b).toEqual(2)
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
it('should transform cloned values by `processValue`', () => {
|
|
58
72
|
const object = {
|
|
59
73
|
a: { b: 1, c: 2 },
|
|
60
74
|
d: { e: 3, f: 4 }
|
|
61
75
|
}
|
|
62
|
-
const copy = clone(object,
|
|
63
|
-
|
|
64
|
-
value
|
|
76
|
+
const copy = clone(object, {
|
|
77
|
+
processValue: value => {
|
|
78
|
+
if (typeof value === 'object') {
|
|
79
|
+
value.g = 5
|
|
80
|
+
}
|
|
65
81
|
}
|
|
66
82
|
})
|
|
67
83
|
const expected = {
|
|
@@ -72,14 +88,16 @@ describe('clone()', () => {
|
|
|
72
88
|
expect(copy).toStrictEqual(expected)
|
|
73
89
|
})
|
|
74
90
|
|
|
75
|
-
it('should call `
|
|
91
|
+
it('should call `processValue` after cloning all children', () => {
|
|
76
92
|
const array = [
|
|
77
93
|
{ a: 1, b: 2 },
|
|
78
94
|
{ a: 3, b: 4 }
|
|
79
95
|
]
|
|
80
|
-
const copy = clone(array,
|
|
81
|
-
|
|
82
|
-
|
|
96
|
+
const copy = clone(array, {
|
|
97
|
+
processValue: value => {
|
|
98
|
+
if (typeof value === 'object') {
|
|
99
|
+
delete value.b
|
|
100
|
+
}
|
|
83
101
|
}
|
|
84
102
|
})
|
|
85
103
|
const expected = [
|