forked from tnptop/array-object-merge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.js
150 lines (134 loc) · 2.77 KB
/
example.js
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
'use strict'
let original = {
roles: [{
key: 'a', value: 'ori'
}, {
key: 'b', value: 'ori'
}],
props: [{
type: 'a', content: 'ori'
}, {
type: 'b', content: 'ori'
}],
dict: { key: 'ori' },
primitives: [ 'old1', 'old2', 1, 2 ]
}
let update = {
field: 'newkey',
roles: [{
key: 'b', value: 'up'
}, {
key: 'c', value: 'ori'
}],
props: [{
type: 'b', content: 'up'
}, {
type: 'c', content: 'ori'
},
'test'],
dict: { key: 'up' },
primitives: [ 'new3', 'new4', 3, 4 ]
}
const merge = require('./index')
/**
* result = {
* field: 'newkey',
* roles: [{
* key: 'a', value: 'ori'
* }, {
* key: 'b', value: 'up'
* }, {
* key: 'c', value: 'ori'
* }],
* props: [{
* type: 'a', content: 'ori'
* }, {
* type: 'b', content: 'up'
* }, {
* type: 'c', content: 'ori'
* },
* 'test'],
* dict: { key: 'up' },
* primitives: [ 'new3', 'new4', 3, 4 ]
* }
*/
let result = merge(original, update, [ 'key', 'type' ])
/**
* Merge two objects with one identifier
*/
let original = {
arr: [{ id: 1, key: 'value' }, { id: 2, key: 'value' }]
}
let update = {
arr: [{ id: 2, key: 'newValue' }, { id: 3, key: 'value' }],
field: 'newkey'
}
/**
* output = {
* arr: [
* { id: 1, key: 'value' },
* { id: 2, key: 'newValue' },
* { id: 3, key: 'value' }
* ],
* field: 'newkey'
* }
*/
let output = merge(original, update, 'id')
/**
* Merge two objects with multiple arrays and identifiers
*/
let original = {
arr1: [{ id: 1, key: 'value' }, { id: 2, key: 'value' }],
arr2: [{ _id: 1, key: 'value' }, { _id: 2, key: 'value' }]
}
let update = {
arr1: [{ id: 2, key: 'newValue' }],
arr2: [{ _id: 1, key: 'newValue' }],
field: 'newkey'
}
/**
* output = {
* arr1: [{ id: 1, key: 'value' }, { id: 2, key: 'newValue' }],
* arr2: [{ _id: 1, key: 'newValue' }, { _id: 2, key: 'value' }],
* field: 'newkey'
* }
*/
let output = merge(original, update, [ 'id', '_id' ])
/**
* Merge two objects with arrays of primitive values
*/
let original = {
arr: [1, 2, 3, 4]
}
let update = {
arr: [5, 6, 7, 8]
}
/**
* Per REST specification: the update array will replace the original one
* output = {
* arr: [5, 6, 7, 8]
* }
*/
let output = merge(original, update)
/**
* Merge two objects with one identifier and primitive values
*/
let original = {
arr: [{ id: 1, key: 'value' }, { id: 2, key: 'value' }, 'shouldbegone']
}
let update = {
arr: [{ id: 2, key: 'newValue' }, { id: 3, key: 'value' }, 'shouldbeincluded'],
field: 'newkey'
}
/**
* output = {
* arr: [
* { id: 1, key: 'value' },
* { id: 2, key: 'newValue' },
* { id: 3, key: 'value' },
* 'shouldbeincluded'
* ],
* field: 'newkey'
* }
*/
let output = merge(original, update, 'id')