@tim-code/my-util 0.5.3 → 0.5.4
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 +1 -1
- package/src/object.js +4 -6
- package/src/object.test.js +51 -16
package/package.json
CHANGED
package/src/object.js
CHANGED
|
@@ -135,16 +135,14 @@ export function deepMerge(target, ...sources) {
|
|
|
135
135
|
}
|
|
136
136
|
|
|
137
137
|
/**
|
|
138
|
-
*
|
|
138
|
+
* Merges a deep copy of each source object into target. See deepCopy() and deepMerge() documentation for caveats.
|
|
139
|
+
* @param {Object} target The target object that will receive the merged properties.
|
|
139
140
|
* @param {...Object} sources The source objects whose properties will be merged into the returned object
|
|
140
141
|
* @returns {Object}
|
|
141
142
|
*/
|
|
142
|
-
export function deepMergeCopy(...sources) {
|
|
143
|
-
if (!sources.length) {
|
|
144
|
-
return {}
|
|
145
|
-
}
|
|
143
|
+
export function deepMergeCopy(target, ...sources) {
|
|
146
144
|
const copies = sources.map(deepCopy)
|
|
147
|
-
const result = deepMerge(...copies)
|
|
145
|
+
const result = deepMerge(target, ...copies)
|
|
148
146
|
return result
|
|
149
147
|
}
|
|
150
148
|
|
package/src/object.test.js
CHANGED
|
@@ -423,35 +423,70 @@ describe("deepMerge", () => {
|
|
|
423
423
|
|
|
424
424
|
// --- deepMergeCopy ---
|
|
425
425
|
describe("deepMergeCopy", () => {
|
|
426
|
-
it("deeply merges deep copies of sources", () => {
|
|
427
|
-
const
|
|
428
|
-
const
|
|
429
|
-
const
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
expect(merged).
|
|
426
|
+
it("deeply merges deep copies of sources into the target", () => {
|
|
427
|
+
const target = { a: { b: 1 } }
|
|
428
|
+
const s1 = { a: { c: 2 } }
|
|
429
|
+
const s2 = { a: { d: 3 } }
|
|
430
|
+
const origTarget = JSON.stringify(target)
|
|
431
|
+
const merged = deepMergeCopy(target, s1, s2)
|
|
432
|
+
expect(merged).toEqual({ a: { b: 1, c: 2, d: 3 } })
|
|
433
|
+
expect(merged).toBe(target)
|
|
433
434
|
expect(merged.a).not.toBe(s1.a)
|
|
434
435
|
expect(merged.a).not.toBe(s2.a)
|
|
436
|
+
expect(JSON.stringify(target)).not.toBe(origTarget)
|
|
437
|
+
expect(s1).toEqual({ a: { c: 2 } })
|
|
438
|
+
expect(s2).toEqual({ a: { d: 3 } })
|
|
435
439
|
})
|
|
436
440
|
|
|
437
441
|
it("does not mutate source objects", () => {
|
|
438
|
-
const
|
|
439
|
-
const
|
|
442
|
+
const target = { a: 1 }
|
|
443
|
+
const s1 = { b: 2 }
|
|
444
|
+
const s2 = { c: 3 }
|
|
440
445
|
const orig1 = JSON.stringify(s1)
|
|
441
446
|
const orig2 = JSON.stringify(s2)
|
|
442
|
-
deepMergeCopy(s1, s2)
|
|
447
|
+
deepMergeCopy(target, s1, s2)
|
|
443
448
|
expect(JSON.stringify(s1)).toBe(orig1)
|
|
444
449
|
expect(JSON.stringify(s2)).toBe(orig2)
|
|
445
450
|
})
|
|
446
451
|
|
|
447
|
-
it("handles arrays and primitives", () => {
|
|
448
|
-
const
|
|
449
|
-
const
|
|
450
|
-
expect(deepMergeCopy(
|
|
452
|
+
it("handles arrays and primitives in sources", () => {
|
|
453
|
+
const target = { a: [1, 2] }
|
|
454
|
+
const s1 = { a: [3, 4], b: 5 }
|
|
455
|
+
expect(deepMergeCopy(target, s1)).toEqual({ a: [3, 4], b: 5 })
|
|
451
456
|
})
|
|
452
457
|
|
|
453
|
-
it("returns
|
|
454
|
-
|
|
458
|
+
it("returns the target object", () => {
|
|
459
|
+
const target = { x: 1 }
|
|
460
|
+
const s1 = { y: 2 }
|
|
461
|
+
expect(deepMergeCopy(target, s1)).toBe(target)
|
|
462
|
+
})
|
|
463
|
+
|
|
464
|
+
it("merges nothing if no sources provided (returns target as is)", () => {
|
|
465
|
+
const target = { foo: 1 }
|
|
466
|
+
expect(deepMergeCopy(target)).toBe(target)
|
|
467
|
+
expect(target).toEqual({ foo: 1 })
|
|
468
|
+
})
|
|
469
|
+
|
|
470
|
+
it("returns target if called with no arguments", () => {
|
|
471
|
+
expect(deepMergeCopy({})).toEqual({})
|
|
472
|
+
expect(deepMergeCopy()).toEqual(undefined)
|
|
473
|
+
})
|
|
474
|
+
|
|
475
|
+
it("does not mutate the target if no sources are provided", () => {
|
|
476
|
+
const target = { z: 9 }
|
|
477
|
+
const result = deepMergeCopy(target)
|
|
478
|
+
expect(result).toBe(target)
|
|
479
|
+
expect(result).toEqual({ z: 9 })
|
|
480
|
+
})
|
|
481
|
+
|
|
482
|
+
it("does not merge inherited properties from sources", () => {
|
|
483
|
+
const target = {}
|
|
484
|
+
const proto = { x: 1 }
|
|
485
|
+
const s1 = Object.create(proto)
|
|
486
|
+
s1.a = 2
|
|
487
|
+
deepMergeCopy(target, s1)
|
|
488
|
+
expect(target).toEqual({ a: 2 })
|
|
489
|
+
expect("x" in target).toBe(false)
|
|
455
490
|
})
|
|
456
491
|
})
|
|
457
492
|
|