File: custom-is-mergeable-object.js

package info (click to toggle)
node-deepmerge 4.3.1%2B~1.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 276 kB
  • sloc: javascript: 917; makefile: 4
file content (72 lines) | stat: -rw-r--r-- 1,990 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const merge = require(`..`)
const test = require(`tape`)

test(`isMergeableObject function copying object over object`, t => {
	const src = { key: { isMergeable: false }, baz: `yes` }
	const target = { key: { foo: `wat` }, baz: `whatever` }

	function isMergeableObject(object) {
		return object && typeof value === `object` && object.isMergeable !== false
	}

	const res = merge(target, src, {
		isMergeableObject,
	})

	t.deepEqual(res, { key: { isMergeable: false }, baz: `yes` })
	t.equal(res.key, src.key, `Object has the same identity and was not cloned`)
	t.end()
})

test(`isMergeableObject function copying object over nothing`, t => {
	const src = { key: { isMergeable: false, foo: `bar` }, baz: `yes` }
	const target = { baz: `whatever` }

	function isMergeableObject(object) {
		return object && typeof value === `object` && object.isMergeable !== false
	}

	const res = merge(target, src, {
		isMergeableObject,
	})

	t.deepEqual(res, { key: { isMergeable: false, foo: `bar` }, baz: `yes` })
	t.equal(res.key, src.key, `Object has the same identity and was not cloned`)
	t.end()
})

test(`example from readme`, t => {
	const { isPlainObject } = require(`is-plain-object`)

	function SuperSpecial() {
		this.special = `oh yeah man totally`
	}

	const instantiatedSpecialObject = new SuperSpecial()

	const target = {
		someProperty: {
			cool: `oh for sure`,
		},
	}

	const source = {
		someProperty: instantiatedSpecialObject,
	}

	const defaultOutput = merge(target, source)

	t.equal(defaultOutput.someProperty.cool, `oh for sure`)
	t.equal(defaultOutput.someProperty.special, `oh yeah man totally`)
	t.equal(defaultOutput.someProperty instanceof SuperSpecial, false)

	const customMergeOutput = merge(target, source, {
		isMergeableObject: isPlainObject,
	})

	t.equal(customMergeOutput.someProperty.cool, undefined)
	t.equal(customMergeOutput.someProperty.special, `oh yeah man totally`)
	t.equal(customMergeOutput.someProperty instanceof SuperSpecial, true)

	t.end()
})