@ditojs/utils 2.100.0 → 3.1.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.
Files changed (48) hide show
  1. package/package.json +5 -9
  2. package/src/array/shuffle.js +1 -1
  3. package/src/string/formatPlainDate.js +14 -0
  4. package/src/string/index.js +2 -0
  5. package/src/string/isDomain.js +10 -3
  6. package/src/string/parsePlainDate.js +18 -0
  7. package/types/index.d.ts +13 -0
  8. package/src/__snapshots__/index.test.js.snap +0 -88
  9. package/src/array/flattten.test.js +0 -29
  10. package/src/array/shuffle.test.js +0 -25
  11. package/src/base/base.test.js +0 -590
  12. package/src/class/mixin.test.js +0 -70
  13. package/src/dataPath/getEntriesAtDataPath.test.js +0 -204
  14. package/src/dataPath/getValueAtDataPath.test.js +0 -140
  15. package/src/dataPath/normalizeDataPath.test.js +0 -36
  16. package/src/dataPath/parseDataPath.test.js +0 -67
  17. package/src/dataPath/setDataPathEntries.test.js +0 -36
  18. package/src/dataPath/setValueAtDataPath.test.js +0 -34
  19. package/src/function/toAsync.test.js +0 -31
  20. package/src/function/toCallback.test.js +0 -29
  21. package/src/function/toPromiseCallback.test.js +0 -24
  22. package/src/html/escapeHtml.test.js +0 -19
  23. package/src/html/stripHtml.test.js +0 -37
  24. package/src/index.test.js +0 -7
  25. package/src/object/clone.test.js +0 -131
  26. package/src/object/equals.test.js +0 -278
  27. package/src/object/groupBy.test.js +0 -70
  28. package/src/object/mapKeys.test.js +0 -16
  29. package/src/object/mapValues.test.js +0 -38
  30. package/src/object/mergeDeeply.test.js +0 -152
  31. package/src/object/pick.test.js +0 -23
  32. package/src/object/pickBy.test.js +0 -48
  33. package/src/string/camelize.test.js +0 -37
  34. package/src/string/capitalize.test.js +0 -33
  35. package/src/string/decamelize.test.js +0 -83
  36. package/src/string/deindent.test.js +0 -181
  37. package/src/string/format.test.js +0 -196
  38. package/src/string/formatDate.test.js +0 -28
  39. package/src/string/getCommonPrefix.test.js +0 -23
  40. package/src/string/isAbsoluteUrl.test.js +0 -15
  41. package/src/string/isCreditCard.test.js +0 -50
  42. package/src/string/isDomain.test.js +0 -15
  43. package/src/string/isEmail.test.js +0 -37
  44. package/src/string/isHostname.test.js +0 -12
  45. package/src/string/isUrl.test.js +0 -1595
  46. package/src/string/labelize.test.js +0 -39
  47. package/src/timer/debounce.test.js +0 -101
  48. package/src/timer/debounceAsync.test.js +0 -143
@@ -1,131 +0,0 @@
1
- import { clone } from './clone.js'
2
- import { vi } from 'vitest'
3
-
4
- describe('clone()', () => {
5
- it('should clone objects', () => {
6
- const object = { a: 1, b: 2 }
7
- const copy = clone(object)
8
- expect(copy).toEqual(object)
9
- expect(copy).not.toBe(object)
10
- })
11
-
12
- it('should clone arrays', () => {
13
- const array = [1, 2, 3]
14
- const copy = clone(array)
15
- expect(copy).toStrictEqual(array)
16
- expect(copy).not.toBe(array)
17
- })
18
-
19
- it('should clone dates', () => {
20
- const date = new Date(2012, 5, 9)
21
- const copy = clone(date)
22
- expect(copy).toStrictEqual(date)
23
- expect(copy).not.toBe(date)
24
- })
25
-
26
- it('should clone regular expressions', () => {
27
- const regexp = /regexp/gi
28
- const copy = clone(regexp)
29
- expect(copy).toStrictEqual(regexp)
30
- expect(copy).not.toBe(regexp)
31
- })
32
-
33
- it('should return functions unmodified', () => {
34
- const func = () => {}
35
- expect(clone(func)).toBe(func)
36
- })
37
-
38
- it('should clone nested objects and arrays', () => {
39
- const object = { a: [1, 2, 3], b: { c: 4, d: 5 } }
40
- const copy = clone(object)
41
- expect(copy).toStrictEqual(object)
42
- expect(copy).not.toBe(object)
43
- expect(copy.a).not.toBe(object.a)
44
- expect(copy.b).not.toBe(object.b)
45
- })
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
-
53
- it('should use clone() methods if available', () => {
54
- const object = {
55
- a: 1,
56
- clone: vi.fn(() => ({ b: 2 }))
57
- }
58
- const copy = clone(object)
59
- expect(object.clone).toBeCalledTimes(1)
60
- expect(copy).toStrictEqual({ b: 2 })
61
- })
62
-
63
- it('should preserve identity', async () => {
64
- const object = { a: { b: 1 } }
65
- object.c = object.a
66
- const copy = clone(object)
67
- expect(copy).toEqual(object)
68
- expect(copy.c).toBe(copy.a)
69
- })
70
-
71
- it('should handle circular references', async () => {
72
- const object = { a: { b: 1 } }
73
- object.c = object
74
- const copy = clone(object)
75
- expect(copy).toEqual(object)
76
- expect(copy.c).toBe(copy)
77
- })
78
-
79
- it('should handle non-enumerable properties', async () => {
80
- const object = { a: 1 }
81
- Object.defineProperty(object, 'b', { value: 2, enumerable: false })
82
- const copy = clone(object, { enumerable: false })
83
- expect(copy.a).toEqual(1)
84
- expect(copy.b).toEqual(2)
85
- })
86
-
87
- it('should transform cloned values by `processValue`', () => {
88
- const object = {
89
- a: { b: 1, c: 2 },
90
- d: { e: 3, f: 4 }
91
- }
92
- const copy = clone(object, {
93
- processValue: value => {
94
- if (typeof value === 'object') {
95
- value.g = 5
96
- }
97
- }
98
- })
99
- const expected = {
100
- a: { b: 1, c: 2, g: 5 },
101
- d: { e: 3, f: 4, g: 5 },
102
- g: 5
103
- }
104
- expect(copy).toStrictEqual(expected)
105
- })
106
-
107
- it('should call `processValue` after cloning all children', () => {
108
- const array = [
109
- { a: 1, b: 2 },
110
- { a: 3, b: 4 }
111
- ]
112
- const copy = clone(array, {
113
- processValue: value => {
114
- if (typeof value === 'object') {
115
- delete value.b
116
- }
117
- }
118
- })
119
- const expected = [
120
- { a: 1 },
121
- { a: 3 }
122
- ]
123
- expect(copy).toStrictEqual(expected)
124
- })
125
-
126
- it('should handle promises', async () => {
127
- const promise = (async () => 1)()
128
- const result = clone(promise)
129
- expect(await result).toStrictEqual(1)
130
- })
131
- })
@@ -1,278 +0,0 @@
1
- import { equals } from './equals.js'
2
-
3
- describe('equals()', () => {
4
- const symbol1 = Symbol('a')
5
- const symbol2 = Symbol('b')
6
- describe.each([
7
- [1, 1, true],
8
- [1, Object(1), true],
9
- [1, '1', false],
10
- [1, 2, false],
11
- [-0, -0, true],
12
- [0, 0, true],
13
- [0, Object(0), true],
14
- [Object(0), Object(0), true],
15
- [-0, 0, true],
16
- [0, '0', false],
17
- [0, null, false],
18
-
19
- [NaN, NaN, true],
20
- [NaN, Object(NaN), true],
21
- [Object(NaN), Object(NaN), true],
22
- [NaN, 'a', false],
23
- [NaN, Infinity, false],
24
-
25
- ['a', 'a', true],
26
- ['a', Object('a'), true],
27
- [Object('a'), Object('a'), true],
28
- ['a', 'b', false],
29
- ['a', ['a'], false],
30
-
31
- [true, true, true],
32
- [true, Object(true), true],
33
- [Object(true), Object(true), true],
34
- [true, 1, false],
35
- [true, 'a', false],
36
- [false, false, true],
37
- [false, Object(false), true],
38
- [Object(false), Object(false), true],
39
- [false, 0, false],
40
- [false, '', false],
41
-
42
- [symbol1, symbol1, true],
43
- // TODO: Uncomment once vitest can format boxed symbols in `%o` titles,
44
- // and remove 'should compare boxed symbols' below:
45
- // https://github.com/vitest-dev/vitest/issues/11445
46
- // [symbol1, Object(symbol1), true],
47
- // [Object(symbol1), Object(symbol1), true],
48
- [symbol1, symbol2, false],
49
-
50
- [null, null, true],
51
- [null, undefined, false],
52
- [null, {}, false],
53
- [null, '', false],
54
- [undefined, undefined, true],
55
- [undefined, null, false],
56
- [undefined, '', false]
57
- ])(
58
- 'equals(%o, %o) (should compare primitives)',
59
- (a, b, expected) => {
60
- it(`returns ${expected}`, () => {
61
- expect(equals(a, b)).toBe(expected)
62
- })
63
- }
64
- )
65
-
66
- // TODO: Remove once the boxed symbol rows above are uncommented.
67
- it('should compare boxed symbols', () => {
68
- expect(equals(symbol1, Object(symbol1))).toBe(true)
69
- expect(equals(Object(symbol1), Object(symbol1))).toBe(true)
70
- })
71
-
72
- it('should compare arrays', () => {
73
- let array1 = [true, null, 1, 'a', undefined]
74
- let array2 = [true, null, 1, 'a', undefined]
75
-
76
- expect(equals(array1, array2)).toBe(true)
77
-
78
- array1 = [[1, 2, 3], new Date(2012, 4, 23), /x/, { e: 1 }]
79
- array2 = [[1, 2, 3], new Date(2012, 4, 23), /x/, { e: 1 }]
80
-
81
- expect(equals(array1, array2)).toBe(true)
82
-
83
- array1 = [1]
84
- array1[2] = 3
85
-
86
- array2 = [1]
87
- array2[1] = undefined
88
- array2[2] = 3
89
-
90
- expect(equals(array1, array2)).toBe(true)
91
-
92
- array1 = [
93
- Object(1),
94
- false,
95
- Object('a'),
96
- /x/,
97
- new Date(2012, 4, 23),
98
- ['a', 'b', [Object('c')]],
99
- { a: 1 }
100
- ]
101
- array2 = [
102
- 1,
103
- Object(false),
104
- 'a',
105
- /x/,
106
- new Date(2012, 4, 23),
107
- ['a', Object('b'), ['c']],
108
- { a: 1 }
109
- ]
110
-
111
- expect(equals(array1, array2)).toBe(true)
112
-
113
- array1 = [1, 2, 3]
114
- array2 = [3, 2, 1]
115
-
116
- expect(equals(array1, array2)).toBe(false)
117
-
118
- array1 = [1, 2]
119
- array2 = [1, 2, 3]
120
-
121
- expect(equals(array1, array2)).toBe(false)
122
- })
123
-
124
- it(`should treat arrays with identical values but different non-index properties as equal`, () => {
125
- let array1 = [1, 2, 3]
126
- let array2 = [1, 2, 3]
127
-
128
- array1.every =
129
- array1.filter =
130
- array1.forEach =
131
- array1.indexOf =
132
- array1.lastIndexOf =
133
- array1.map =
134
- array1.some =
135
- array1.reduce =
136
- array1.reduceRight =
137
- null
138
-
139
- array2.concat =
140
- array2.join =
141
- array2.pop =
142
- array2.reverse =
143
- array2.shift =
144
- array2.slice =
145
- array2.sort =
146
- array2.splice =
147
- array2.unshift =
148
- null
149
-
150
- expect(equals(array1, array2)).toBe(true)
151
-
152
- array1 = [1, 2, 3]
153
- array1.a = 1
154
-
155
- array2 = [1, 2, 3]
156
- array2.b = 1
157
-
158
- expect(equals(array1, array2)).toBe(true)
159
-
160
- array1 = /c/.exec('abcde')
161
- array2 = ['c']
162
-
163
- expect(equals(array1, array2)).toBe(true)
164
- })
165
-
166
- it('should compare sparse arrays', () => {
167
- const array = Array(1)
168
- expect(equals(array, Array(1))).toBe(true)
169
- expect(equals(array, [undefined])).toBe(true)
170
- expect(equals(array, Array(2))).toBe(false)
171
- })
172
-
173
- it('should compare plain objects', () => {
174
- let object1 = { a: true, b: null, c: 1, d: 'a', e: undefined }
175
- let object2 = { a: true, b: null, c: 1, d: 'a', e: undefined }
176
-
177
- expect(equals(object1, object2)).toBe(true)
178
-
179
- object1 = { a: [1, 2, 3], b: new Date(2012, 4, 23), c: /x/, d: { e: 1 } }
180
- object2 = { a: [1, 2, 3], b: new Date(2012, 4, 23), c: /x/, d: { e: 1 } }
181
-
182
- expect(equals(object1, object2)).toBe(true)
183
-
184
- object1 = { a: 1, b: 2, c: 3 }
185
- object2 = { a: 3, b: 2, c: 1 }
186
-
187
- expect(equals(object1, object2)).toBe(false)
188
-
189
- object1 = { a: 1, b: 2, c: 3 }
190
- object2 = { d: 1, e: 2, f: 3 }
191
-
192
- expect(equals(object1, object2)).toBe(false)
193
-
194
- object1 = { a: 1, b: 2 }
195
- object2 = { a: 1, b: 2, c: 3 }
196
-
197
- expect(equals(object1, object2)).toBe(false)
198
- })
199
-
200
- it('should compare objects regardless of key order', () => {
201
- const object1 = { a: 1, b: 2, c: 3 }
202
- const object2 = { c: 3, a: 1, b: 2 }
203
-
204
- expect(equals(object1, object2)).toBe(true)
205
- })
206
-
207
- it('should compare nested objects', () => {
208
- function noop() {}
209
- const object1 = {
210
- a: [1, 2, 3],
211
- b: true,
212
- c: Object(1),
213
- d: 'a',
214
- e: {
215
- f: ['a', Object('b'), 'c'],
216
- g: Object(false),
217
- h: new Date(2012, 4, 23),
218
- i: noop,
219
- j: 'a'
220
- }
221
- }
222
-
223
- const object2 = {
224
- a: [1, Object(2), 3],
225
- b: Object(true),
226
- c: 1,
227
- d: Object('a'),
228
- e: {
229
- f: ['a', 'b', 'c'],
230
- g: false,
231
- h: new Date(2012, 4, 23),
232
- i: noop,
233
- j: 'a'
234
- }
235
- }
236
-
237
- expect(equals(object1, object2)).toBe(true)
238
- })
239
-
240
- it('should compare objects with shared property values', () => {
241
- const object1 = {
242
- a: [1, 2]
243
- }
244
-
245
- const object2 = {
246
- a: [1, 2],
247
- b: [1, 2]
248
- }
249
-
250
- object1.b = object1.a
251
-
252
- expect(equals(object1, object2)).toBe(true)
253
- })
254
-
255
- it('should avoid common type coercions', () => {
256
- expect(equals(true, Object(false))).toBe(false)
257
- expect(equals(Object(false), Object(0))).toBe(false)
258
- expect(equals(false, Object(''))).toBe(false)
259
- expect(equals(Object(36), Object('36'))).toBe(false)
260
- expect(equals(0, '')).toBe(false)
261
- expect(equals(1, true)).toBe(false)
262
- expect(equals(1337756400000, new Date(2012, 4, 23))).toBe(false)
263
- expect(equals('36', 36)).toBe(false)
264
- expect(equals(36, '36')).toBe(false)
265
- })
266
-
267
- it('should compare functions', () => {
268
- function a() {
269
- return 1 + 2
270
- }
271
- function b() {
272
- return 1 + 2
273
- }
274
-
275
- expect(equals(a, a)).toBe(true)
276
- expect(equals(a, b)).toBe(false)
277
- })
278
- })
@@ -1,70 +0,0 @@
1
- import { groupBy } from './groupBy.js'
2
-
3
- describe('groupBy()', () => {
4
- const array = [6.1, 4.2, 6.3]
5
-
6
- it('should group entries by the result of `callback`', () => {
7
- const actual1 = groupBy(array, Math.floor)
8
- expect(actual1).toStrictEqual({ 4: [4.2], 6: [6.1, 6.3] })
9
-
10
- const actual2 = groupBy(['one', 'two', 'three'], value => value.length)
11
- expect(actual2).toStrictEqual({ 3: ['one', 'two'], 5: ['three'] })
12
- })
13
-
14
- it('should group by numbers and strings', () => {
15
- const array = [
16
- [1, 'a'],
17
- [2, 'a'],
18
- [2, 'b']
19
- ]
20
- expect(groupBy(array, value => value[0])).toStrictEqual({
21
- 1: [[1, 'a']],
22
- 2: [[2, 'a'], [2, 'b']]
23
- })
24
- expect(groupBy(array, value => value[1])).toStrictEqual({
25
- a: [[1, 'a'], [2, 'a']],
26
- b: [[2, 'b']]
27
- })
28
- })
29
-
30
- it('should work with an object for `collection`', () => {
31
- const actual = groupBy({ a: 6.1, b: 4.2, c: 6.3 }, Math.floor)
32
- expect(actual).toStrictEqual({ 4: [4.2], 6: [6.1, 6.3] })
33
- })
34
-
35
- it('should support string property accessor', () => {
36
- const array = [
37
- { category: 'fruit', name: 'apple' },
38
- { category: 'vegetable', name: 'carrot' },
39
- { category: 'fruit', name: 'banana' }
40
- ]
41
- const actual = groupBy(array, 'category')
42
- expect(actual).toStrictEqual({
43
- fruit: [
44
- { category: 'fruit', name: 'apple' },
45
- { category: 'fruit', name: 'banana' }
46
- ],
47
- vegetable: [
48
- { category: 'vegetable', name: 'carrot' }
49
- ]
50
- })
51
- })
52
-
53
- it('should group by string property with numbers', () => {
54
- const array = [
55
- { id: 1, value: 'a' },
56
- { id: 2, value: 'b' },
57
- { id: 1, value: 'c' }
58
- ]
59
- const actual = groupBy(array, 'id')
60
- expect(actual).toStrictEqual({
61
- 1: [
62
- { id: 1, value: 'a' },
63
- { id: 1, value: 'c' }
64
- ],
65
- 2: [
66
- { id: 2, value: 'b' }
67
- ]
68
- })
69
- })
70
- })
@@ -1,16 +0,0 @@
1
- import { mapKeys } from './mapKeys.js'
2
-
3
- describe('mapKeys()', () => {
4
- const array = [1, 2]
5
- const object = { a: 1, b: 2 }
6
-
7
- it('should map keys in `object` to a new object', () => {
8
- const actual = mapKeys(object, (key, value) => String(value))
9
- expect(actual).toStrictEqual({ 1: 1, 2: 2 })
10
- })
11
-
12
- it('should treat arrays like objects', () => {
13
- const actual = mapKeys(array, (key, value) => String(value))
14
- expect(actual).toStrictEqual({ 1: 1, 2: 2 })
15
- })
16
- })
@@ -1,38 +0,0 @@
1
- import { mapValues } from './mapValues.js'
2
-
3
- describe('mapValues()', () => {
4
- const array = [1, 2]
5
- const object = { a: 1, b: 2 }
6
-
7
- it('should map values in `object` to a new object', () => {
8
- const actual = mapValues(object, String)
9
- expect(actual).toStrictEqual({ a: '1', b: '2' })
10
- })
11
-
12
- it('should treat arrays like objects', () => {
13
- const actual = mapValues(array, String)
14
- expect(actual).toStrictEqual({ 0: '1', 1: '2' })
15
- })
16
-
17
- it('should support string property accessor', () => {
18
- const users = {
19
- user1: { id: 1, name: 'Alice', email: 'alice@example.com' },
20
- user2: { id: 2, name: 'Bob', email: 'bob@example.com' }
21
- }
22
- const actual = mapValues(users, 'email')
23
- expect(actual).toStrictEqual({
24
- user1: 'alice@example.com',
25
- user2: 'bob@example.com'
26
- })
27
- })
28
-
29
- it('should extract nested properties with string accessor', () => {
30
- const data = {
31
- a: { value: 10 },
32
- b: { value: 20 },
33
- c: { value: 30 }
34
- }
35
- const actual = mapValues(data, 'value')
36
- expect(actual).toStrictEqual({ a: 10, b: 20, c: 30 })
37
- })
38
- })
@@ -1,152 +0,0 @@
1
- import { mergeDeeply, assignDeeply } from './mergeDeeply.js'
2
-
3
- describe('mergeDeeply()', () => {
4
- it('should merge nested objects', () => {
5
- const source1 = { a: { b: [] } }
6
- const source2 = { a: { c: [] } }
7
- const expected = { a: { b: [], c: [] } }
8
- expect(mergeDeeply({}, source1, source2)).toStrictEqual(expected)
9
- expect(mergeDeeply({}, source2, source1)).toStrictEqual(expected)
10
- })
11
-
12
- it('should override keys in target with different types', () => {
13
- const source1 = { a: { b: 1 } }
14
- const source2 = { a: { b: [] } }
15
- expect(mergeDeeply({}, source1, source2)).toStrictEqual(source2)
16
- expect(mergeDeeply({}, source2, source1)).toStrictEqual(source1)
17
- })
18
-
19
- it('should merge objects at the same indices inside arrays', () => {
20
- const source1 = [{ a: 1 }, { b: 2 }]
21
- const source2 = [{ c: 3 }, { d: 4 }]
22
- const expected = [{ a: 1, c: 3 }, { b: 2, d: 4 }]
23
- expect(mergeDeeply([], source1, source2)).toStrictEqual(expected)
24
- })
25
-
26
- it('should merge `source` into `object`', () => {
27
- const names = {
28
- characters: [
29
- { name: 'barney' },
30
- { name: 'fred' }
31
- ]
32
- }
33
-
34
- const ages = {
35
- characters: [
36
- { age: 36 },
37
- { age: 40 }
38
- ]
39
- }
40
-
41
- const heights = {
42
- characters: [
43
- { height: '5\'4"' },
44
- { height: '5\'5"' }
45
- ]
46
- }
47
-
48
- const expected = {
49
- characters: [
50
- { name: 'barney', age: 36, height: '5\'4"' },
51
- { name: 'fred', age: 40, height: '5\'5"' }
52
- ]
53
- }
54
-
55
- expect(mergeDeeply(names, ages, heights)).toEqual(expected)
56
- })
57
-
58
- it('should handle plain values', () => {
59
- expect(mergeDeeply(true, false)).toEqual(false)
60
- expect(mergeDeeply(10, 0)).toEqual(0)
61
- expect(mergeDeeply(0, 10)).toEqual(10)
62
- expect(mergeDeeply('foo', 'bar')).toEqual('bar')
63
- })
64
-
65
- it('should not override values with nullish values at root level', () => {
66
- expect(mergeDeeply({}, null)).toEqual({})
67
- expect(mergeDeeply([], null)).toEqual([])
68
- expect(mergeDeeply(10, null)).toEqual(10)
69
- expect(mergeDeeply(false, null)).toEqual(false)
70
- })
71
-
72
- it('should work with multiple arguments', () => {
73
- const expected = { a: 4 }
74
- const actual = mergeDeeply({ a: 1 }, { a: 2 }, { a: 3 }, expected)
75
-
76
- expect(actual).toStrictEqual(expected)
77
- })
78
-
79
- it('should not augment source objects', () => {
80
- const source1 = { a: [{ a: 1 }] }
81
- const source2 = { a: [{ b: 2 }] }
82
- const actual1 = mergeDeeply({}, source1, source2)
83
-
84
- expect(source1.a).toStrictEqual([{ a: 1 }])
85
- expect(source2.a).toStrictEqual([{ b: 2 }])
86
- expect(actual1.a).toStrictEqual([{ a: 1, b: 2 }])
87
-
88
- const source3 = { a: [[1, 2, 3]] }
89
- const source4 = { a: [[4, 5]] }
90
- const actual2 = mergeDeeply({}, source3, source4)
91
-
92
- expect(source3.a).toStrictEqual([[1, 2, 3]])
93
- expect(source4.a).toStrictEqual([[4, 5]])
94
- expect(actual2.a).toStrictEqual([[1, 2, 3, 4, 5]])
95
- })
96
-
97
- it('should overwrite existing values with `undefined` in objects', () => {
98
- const result = mergeDeeply({ a: 1 }, { a: undefined, b: undefined })
99
- expect(result).toStrictEqual({ a: undefined, b: undefined })
100
- })
101
-
102
- it('should not overwrite existing values with `undefined` in arrays', () => {
103
- const result1 = mergeDeeply([4, 5, 6], [1, undefined, 3])
104
- expect(result1).toStrictEqual([4, 5, 6, 1, undefined, 3])
105
-
106
- // eslint-disable-next-line no-sparse-arrays
107
- const result2 = mergeDeeply([4, 5, 6], [1, , 3])
108
- expect(result2).toStrictEqual([4, 5, 6, 1, 3])
109
- })
110
-
111
- it('should merge regexps', () => {
112
- const source1 = { a: /1/ }
113
- const source2 = { a: /2/ }
114
- const expected = { a: /2/ }
115
- expect(mergeDeeply({}, source1, source2)).toStrictEqual(expected)
116
- })
117
-
118
- it('should merge dates', () => {
119
- const source1 = { a: new Date(2012, 5, 9) }
120
- const source2 = { a: new Date(2021, 5, 9) }
121
- const expected = { a: new Date(2021, 5, 9) }
122
- expect(mergeDeeply({}, source1, source2)).toStrictEqual(expected)
123
- })
124
-
125
- it('should be fine with nested promises', async () => {
126
- const promise1 = (async () => 1)()
127
- const promise2 = (async () => 2)()
128
- const source1 = { nested: { promise1 } }
129
- const source2 = { nested: { promise2 } }
130
- const expected = { nested: { promise1, promise2 } }
131
- const result = mergeDeeply({}, source1, source2)
132
- expect(result).toStrictEqual(expected)
133
- expect(await result.nested.promise1).toStrictEqual(1)
134
- expect(await result.nested.promise2).toStrictEqual(2)
135
- })
136
- })
137
-
138
- describe('assignDeeply()', () => {
139
- it('should not concat-merge arrays', () => {
140
- const result = assignDeeply({}, { a: [[1, 2, 3]] }, { a: [[4, 5]] })
141
- expect(result).toStrictEqual({ a: [[4, 5, 3]] })
142
- })
143
-
144
- it('should overwrite existing values with `undefined` in arrays', () => {
145
- const result1 = assignDeeply([4, 5, 6], [1, undefined, 3])
146
- expect(result1).toStrictEqual([1, undefined, 3])
147
-
148
- // eslint-disable-next-line no-sparse-arrays
149
- const result2 = assignDeeply([4, 5, 6], [1, , 3])
150
- expect(result2).toStrictEqual([1, 5, 3])
151
- })
152
- })
@@ -1,23 +0,0 @@
1
- import { pick } from './pick.js'
2
-
3
- // Partially based on:
4
- // https://github.com/lodash/lodash/blob/4.17.11/test/test.js
5
-
6
- describe('pick()', () => {
7
- it(`should return the first value that isn't undefined`, () => {
8
- expect(pick(10)).toBe(10)
9
- expect(pick(10, undefined)).toBe(10)
10
- expect(pick(undefined, 10)).toBe(10)
11
- expect(pick(undefined, undefined, 10)).toBe(10)
12
- })
13
-
14
- it('should consider null as defined', () => {
15
- expect(pick(null)).toBeNull()
16
- expect(pick(undefined, null)).toBeNull()
17
- })
18
-
19
- it('should return undefined if nothing is defined', () => {
20
- expect(pick()).toBeUndefined()
21
- expect(pick(undefined)).toBeUndefined()
22
- })
23
- })