@ditojs/utils 1.4.1 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ditojs/utils",
3
- "version": "1.4.1",
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",
@@ -27,5 +27,5 @@
27
27
  "dependencies": {
28
28
  "punycode": "^2.1.1"
29
29
  },
30
- "gitHead": "0e8a22ea0cb68eec3df1b6236cf77a85f5a2c836"
30
+ "gitHead": "f2a544aef8ab0a12c7019650cd86558f8365b007"
31
31
  }
@@ -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
  }
@@ -88,4 +88,10 @@ describe('clone()', () => {
88
88
  ]
89
89
  expect(copy).toStrictEqual(expected)
90
90
  })
91
+
92
+ it('should handle promises', async () => {
93
+ const promise = (async () => 1)()
94
+ const result = clone(promise)
95
+ expect(await result).toStrictEqual(1)
96
+ })
91
97
  })
@@ -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
  })