@ditojs/utils 1.15.0 → 1.17.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.15.0",
3
+ "version": "1.17.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": "848aed21b94e81ac68f089c4a6bd335b1a97073d"
38
+ "gitHead": "45d692909e0ed637fdabe4568f971aeed7e84388"
39
39
  }
@@ -4,11 +4,19 @@ import {
4
4
  import { pick } from '../object/index.js'
5
5
 
6
6
  export function clone(arg, options) {
7
- const { shallow = false, processValue = null } = isFunction(options)
7
+ const {
8
+ shallow = false,
9
+ enumerable = true,
10
+ descriptors = false,
11
+ transferables = null,
12
+ processValue = null
13
+ } = isFunction(options)
8
14
  ? { processValue: options } // TODO: `callback` deprecated in December 2022.
9
15
  : options || {}
10
16
 
11
- const handleValue = value => shallow ? value : clone(value, options)
17
+ const handleValue = value => shallow || transferables?.includes(value)
18
+ ? value
19
+ : clone(value, options)
12
20
 
13
21
  let copy = arg
14
22
  if (isDate(arg)) {
@@ -31,12 +39,17 @@ export function clone(arg, options) {
31
39
  // Prevent calling the actual constructor since it is not guaranteed to
32
40
  // work as intended here, and only clone the non-inherited own properties.
33
41
  copy = Object.create(Object.getPrototypeOf(arg))
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)
42
+ const keys = enumerable ? Object.keys(arg) : Reflect.ownKeys(arg)
43
+ for (const key of keys) {
44
+ if (descriptors || !enumerable) {
45
+ const desc = Reflect.getOwnPropertyDescriptor(arg, key)
46
+ if (desc.value != null) {
47
+ desc.value = handleValue(desc.value)
48
+ }
49
+ Reflect.defineProperty(copy, key, desc)
50
+ } else {
51
+ copy[key] = handleValue(arg[key])
38
52
  }
39
- Reflect.defineProperty(copy, key, desc)
40
53
  }
41
54
  }
42
55
  }
@@ -63,7 +63,7 @@ describe('clone()', () => {
63
63
  it('should handle non-enumerable properties', async () => {
64
64
  const object = { a: 1 }
65
65
  Object.defineProperty(object, 'b', { value: 2, enumerable: false })
66
- const copy = clone(object)
66
+ const copy = clone(object, { enumerable: false })
67
67
  expect(copy.a).toEqual(1)
68
68
  expect(copy.b).toEqual(2)
69
69
  })