-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
159 lines (151 loc) · 5.1 KB
/
test.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
151
152
153
154
155
156
157
158
159
//In this example we will mutate a single object in steps, demonstrating each update method
//Note: all the different kind of update commands can be executed in one single API call also
//You can copy paste and run this yourself
var updater = require('/home/master/work/code/js-object-updater')
var _ = require('lodash')
var objectToUpdate = { //Can be an array or object
aTopLevelField: 'foo',
topLevelArray: [2, 4, {a: 3}],
anotherTopLevelArray: [
{
someField: 3,
arr2: [
{
a: 2,
f: 4,
x: {
fieldA: 'someRandomValue'
}
}
]
},
{
someField: 4,
arr2: [
{
a: 23,
f: 42,
x: {
fieldA: 'some other random value',
arrC: [42, 56]
},
}
]
}
]
}
//Unset
updater(
{
doc: objectToUpdate,
force:true,// In unset, force true or false does not make any difference
update: {
unset: [
{
_path: ["anotherTopLevelArray", {someField: 3}, "arr2", {a: 2, f: 4}, "x"]
},
['aTopLevelField'] //The array of top level fields
],
}
}
)
console.log('after unset', JSON.stringify(objectToUpdate))
//{"topLevelArray":[2,4,{"a":3}],"anotherTopLevelArray":[{"someField":3,"arr2":[{"a":2,"f":4}]},{"someField":4,"arr2":[{"a":23,"f":42,"x":{"fieldA":"some other random value","arrC":[42,56]}}]}]}
//Set
updater(
{
doc: objectToUpdate,
force:true,//Whether to force creation of nested path if it does not fully exist
update: {
set:[
{
_path: ["anotherTopLevelArray", {someField: 4}, "arr2", {a:23, f:42}, "foo"],
_value: [6.5, 2.3]
},
{
someOtherTopLevelField: 42,
anotherTopLevelField: [42, 65]
}
],
}
}
)
console.log('after set', JSON.stringify(objectToUpdate))
//{"topLevelArray":[2,4,{"a":3}],"anotherTopLevelArray":[{"someField":3,"arr2":[{"a":2,"f":4}]},{"someField":4,"arr2":[{"a":23,"f":42,"x":{"fieldA":"some other random value","arrC":[42,56]},"foo":6.5}]}],"someOtherTopLevelField":42,"anotherTopLevelField":[42,65]}
//Pull
updater(
{
doc: objectToUpdate,
force:true, // In pull, force true or false does not make any difference
update: {
pull:[
{
_path: ["anotherTopLevelArray", {someField:3}, "arr2"],
_value: {a: 2} //Remove the objects from nested path where a:2}
},
{
_path: ["anotherTopLevelArray", {someField: 3}, "arr2", {a: 23,f: 42},"arrC"],
_values: [42, 56] //Should have no effect since these do not exist at x in the deep path specified above
},
{
topLevelArray: 2 //Pull just 3 from topLevelArray
},
{
topLevelArray: [{a: 3}, 4] //Pull out one object where a:23 and one object where the number is 4
},
],
}
}
)
console.log('after pull', JSON.stringify(objectToUpdate))
//{"topLevelArray":[],"anotherTopLevelArray":[{"someField":3,"arr2":[]},{"someField":4,"arr2":[{"a":23,"f":42,"x":{"fieldA":"some other random value","arrC":[42,56]},"foo":6.5}]}],"someOtherTopLevelField":42,"anotherTopLevelField":[]}
//Push
updater(
{
doc: objectToUpdate,
force:true,
update: {
push:[
{
topLevelArray: 21 //Will concat 21 to list of vlaues at topLevelArray. If topLevelArrat does not exist or is not an array, will make it an array
},
{
topLevelArray: [5, 3] //Will push 5 and 3 to topLevelArray
},
{
_path: ["anotherTopLevelArray",{someField:3},"arr2",{a:2,f:4},"x"], //Do a deep push at x after traversing two arrays
_values: [6.5, 3.4] //Push both these values one by one
},
{
_path: ["anotherTopLevelArray",{someField:3},"arr2",{a:2,f:4}, "arrC"], //Do a deep push at x after traversing two arrays
_value: 6565 //Set a single value
}
]
}
}
)
console.log('after push', JSON.stringify(objectToUpdate))
//{"topLevelArray":[21,5,3],"anotherTopLevelArray":[{"someField":3,"arr2":[{"a":2,"f":4,"x":[6.5,3.4],"arrC":[6565]}]},{"someField":4,"arr2":[{"a":23,"f":42,"x":{"fieldA":"some other random value","arrC":[42,56]},"foo":6.5}]}],"someOtherTopLevelField":42,"anotherTopLevelField":[42,65]}
//addToSet
updater(
{
doc: objectToUpdate,
force:true,
update: {
addToSet:[
{
topLevelArray: 21 //Will not concat 21 to list of vlaues at topLevelArray as the value already exists
},
{
topLevelArray: [5, 32] //Will push 32 to topLevelArray. 5 will be ignored
},
{
_path: ["anotherTopLevelArray",{someField:3},"arr2",{a:2,f:4}, "arrC"],
_value: [6565, 45] //Ignored
}
]
}
}
)
console.log('after addToSet', JSON.stringify(objectToUpdate))
//{"topLevelArray":[21,5,3,32],"anotherTopLevelArray":[{"someField":3,"arr2":[{"a":2,"f":4,"x":[6.5,3.4],"arrC":[6565]}]},{"someField":4,"arr2":[{"a":23,"f":42,"x":{"fieldA":"some other random value","arrC":[42,56]},"foo":6.5}]}],"someOtherTopLevelField":42,"anotherTopLevelField":[42,65]}