@ditojs/utils 1.13.1 → 1.15.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ditojs/utils",
3
- "version": "1.13.1",
3
+ "version": "1.15.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",
@@ -32,8 +32,8 @@
32
32
  "punycode": "^2.1.1"
33
33
  },
34
34
  "devDependencies": {
35
- "typescript": "^4.8.4"
35
+ "typescript": "^4.9.3"
36
36
  },
37
37
  "types": "types",
38
- "gitHead": "0c6a508e04ca915691c1fb0f3706827c8ecfe0ab"
38
+ "gitHead": "848aed21b94e81ac68f089c4a6bd335b1a97073d"
39
39
  }
@@ -3,7 +3,13 @@ import {
3
3
  } from '../base/index.js'
4
4
  import { pick } from '../object/index.js'
5
5
 
6
- export function clone(arg, callback = null) {
6
+ export function clone(arg, options) {
7
+ const { shallow = false, processValue = null } = isFunction(options)
8
+ ? { processValue: options } // TODO: `callback` deprecated in December 2022.
9
+ : options || {}
10
+
11
+ const handleValue = value => shallow ? value : clone(value, options)
12
+
7
13
  let copy = arg
8
14
  if (isDate(arg)) {
9
15
  copy = new arg.constructor(+arg)
@@ -12,12 +18,12 @@ export function clone(arg, callback = null) {
12
18
  } else if (isArray(arg)) {
13
19
  copy = new arg.constructor(arg.length)
14
20
  for (let i = 0, l = arg.length; i < l; i++) {
15
- copy[i] = clone(arg[i], callback)
21
+ copy[i] = handleValue(arg[i])
16
22
  }
17
23
  } else if (isObject(arg)) {
18
24
  // Rely on arg.clone() if it exists and assume it creates an actual clone.
19
25
  if (isFunction(arg.clone)) {
20
- copy = arg.clone()
26
+ copy = arg.clone(options)
21
27
  } else if (isPromise(arg)) {
22
28
  // https://stackoverflow.com/questions/37063293/can-i-clone-a-promise
23
29
  copy = arg.then()
@@ -25,10 +31,14 @@ export function clone(arg, callback = null) {
25
31
  // Prevent calling the actual constructor since it is not guaranteed to
26
32
  // work as intended here, and only clone the non-inherited own properties.
27
33
  copy = Object.create(Object.getPrototypeOf(arg))
28
- for (const key of Object.keys(arg)) {
29
- copy[key] = clone(arg[key], callback)
34
+ for (const key of Reflect.ownKeys(arg)) {
35
+ const desc = Reflect.getOwnPropertyDescriptor(arg, key)
36
+ if (desc.value != null) {
37
+ desc.value = handleValue(desc.value)
38
+ }
39
+ Reflect.defineProperty(copy, key, desc)
30
40
  }
31
41
  }
32
42
  }
33
- return pick(callback?.(copy), copy)
43
+ return pick(processValue?.(copy), copy)
34
44
  }
@@ -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 transform cloned values by `callback`', () => {
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)
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, value => {
63
- if (typeof value === 'object') {
64
- value.g = 5
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 `callback` after cloning all children', () => {
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, value => {
81
- if (typeof value === 'object') {
82
- delete value.b
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 = [