@ditojs/utils 1.3.0 → 1.4.3
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 +4 -4
- package/src/object/clone.js +5 -4
- package/src/object/clone.test.js +6 -0
- package/src/object/merge.test.js +12 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ditojs/utils",
|
|
3
|
-
"version": "1.3
|
|
3
|
+
"version": "1.4.3",
|
|
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",
|
|
@@ -14,11 +14,11 @@
|
|
|
14
14
|
"src/"
|
|
15
15
|
],
|
|
16
16
|
"engines": {
|
|
17
|
-
"node": ">=
|
|
17
|
+
"node": ">= 16.0.0",
|
|
18
18
|
"yarn": ">= 1.0.0"
|
|
19
19
|
},
|
|
20
20
|
"browserslist": [
|
|
21
|
-
"node >=
|
|
21
|
+
"node >= 16",
|
|
22
22
|
"> 1%",
|
|
23
23
|
"last 2 versions",
|
|
24
24
|
"ie >= 11",
|
|
@@ -27,5 +27,5 @@
|
|
|
27
27
|
"dependencies": {
|
|
28
28
|
"punycode": "^2.1.1"
|
|
29
29
|
},
|
|
30
|
-
"gitHead": "
|
|
30
|
+
"gitHead": "f2a544aef8ab0a12c7019650cd86558f8365b007"
|
|
31
31
|
}
|
package/src/object/clone.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import {
|
|
2
|
-
isArray, isObject, isDate, isRegExp, isFunction
|
|
2
|
+
isArray, isObject, isDate, isRegExp, isFunction, isPromise
|
|
3
3
|
} from '../base/index.js'
|
|
4
4
|
import { pick } from '../object/index.js'
|
|
5
5
|
|
|
6
6
|
export function clone(arg, callback = null) {
|
|
7
|
-
let copy
|
|
7
|
+
let copy = arg
|
|
8
8
|
if (isDate(arg)) {
|
|
9
9
|
copy = new arg.constructor(+arg)
|
|
10
10
|
} else if (isRegExp(arg)) {
|
|
@@ -18,6 +18,9 @@ export function clone(arg, callback = null) {
|
|
|
18
18
|
// Rely on arg.clone() if it exists and assume it creates an actual clone.
|
|
19
19
|
if (isFunction(arg.clone)) {
|
|
20
20
|
copy = arg.clone()
|
|
21
|
+
} else if (isPromise(arg)) {
|
|
22
|
+
// https://stackoverflow.com/questions/37063293/can-i-clone-a-promise
|
|
23
|
+
copy = arg.then()
|
|
21
24
|
} else {
|
|
22
25
|
// Prevent calling the actual constructor since it is not guaranteed to
|
|
23
26
|
// work as intended here, and only clone the non-inherited own properties.
|
|
@@ -26,8 +29,6 @@ export function clone(arg, callback = null) {
|
|
|
26
29
|
copy[key] = clone(arg[key], callback)
|
|
27
30
|
}
|
|
28
31
|
}
|
|
29
|
-
} else {
|
|
30
|
-
copy = arg
|
|
31
32
|
}
|
|
32
33
|
return pick(callback?.(copy), copy)
|
|
33
34
|
}
|
package/src/object/clone.test.js
CHANGED
package/src/object/merge.test.js
CHANGED
|
@@ -110,4 +110,16 @@ describe('merge()', () => {
|
|
|
110
110
|
const expected = { a: new Date(2021, 5, 9) }
|
|
111
111
|
expect(merge({}, source1, source2)).toStrictEqual(expected)
|
|
112
112
|
})
|
|
113
|
+
|
|
114
|
+
it('should be fine with nested promises', async () => {
|
|
115
|
+
const promise1 = (async () => 1)()
|
|
116
|
+
const promise2 = (async () => 2)()
|
|
117
|
+
const source1 = { nested: { promise1 } }
|
|
118
|
+
const source2 = { nested: { promise2 } }
|
|
119
|
+
const expected = { nested: { promise1, promise2 } }
|
|
120
|
+
const result = merge({}, source1, source2)
|
|
121
|
+
expect(result).toStrictEqual(expected)
|
|
122
|
+
expect(await result.nested.promise1).toStrictEqual(1)
|
|
123
|
+
expect(await result.nested.promise2).toStrictEqual(2)
|
|
124
|
+
})
|
|
113
125
|
})
|