-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
314 lines (269 loc) · 7.61 KB
/
index.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/**
{
force:true,//Whether to force creation of nested path if it does not fully exist
update: {
set:[
{
_path: ["arr1",{y:{x:{'>':3},f:{'<=':5}},z:23},"arr2",{a:2,f:4},"x"],
_value: 6.5
},
{
key:a//edge a
},
{//aray with prorties x = 4,y=8
x:4,
y:8
}
],
pull:[
{
yy:6.5
},
{
arr1:{y:23,z:32}
}
],
push:[
{
y:5
},
{
arr_z:{answer:"42"}
},
{//For nested path through arrays
_path: ["arr1",{y:{'=':{x:{'>':3}},f:{'<=':5}},z:23},"arr2",{a:2,f:4},"x"],
_value: 6.5
}
]
}
}
*/
//var longjon = require('longjohn')
var _ = require('lodash')
var get = require('./get');
var deepEqual = require('deep-equal');
module.exports = function(params) {
var doc = params.doc
if (!doc) {
throw new Error('update is missing the "doc" parameter')
}
var update = params.update
if (!update) {
return
}
var force = params.force || true
var operations = Object.keys(update)
if (!operations) {
return
}
for (var i in operations) {
var operation = operations[i]
//execute the operation, whether $push or $pull with its parameters
var op_params = update[operation]
if (op_params instanceof Array) {
for (var i = 0; i < op_params.length; i++) {
module.exports[operation](doc,op_params[i],force)
}
} else if (op_params instanceof Object) {
module.exports[operation](doc,op_params, force)
} else {
throw new Error('update param must be Array of instructions OR a single instruction Object')
}
}
}
/**
{
_path: ["arr1",{y:{x:{'>':3},f:{'<=':5}},z:23},"arr2",{a:2,f:4},"x"],
_value: 6.5
}
@param doc
@param params They can come in two forms
{nested,value} : The nested path of keys will be traversed within the doc to set the value at leaf
(keyVal pairs} : All the attributes in attrs Object are directly copied to doc
*/
var set = function(doc,params,force) {
if (params._path) {//is array of keys (path) to traverse depth wise in the doc
var keys = params._path
var nested = get.last_parent(doc, keys, force)
var lastKey = keys[keys.length - 1].key || keys[keys.length -1]
nested[lastKey] = params._value
} else {//is (potentially multiple) attributes to copy
var keys = Object.keys(params)
for (var i in keys) {
doc[keys[i]] = params[keys[i]]
}
}
return doc
}
/**
@param doc
@param params They can come in two forms
_path : The nested path of keys will be traversed within the doc to unset the leaf key
[attrs] : Array of the top level fields, if top level fields are to unset
*/
var unset = function(doc,params) {
if (params._path) {//is array of keys (path) to traverse depth wise in the doc
var keys = params._path
var nested
try {
nested = get.last_parent(doc,keys)
} catch(err) {
return
}
var lastKey = keys[keys.length - 1].key || keys[keys.length -1]
delete nested[lastKey]
} else {
params = _.flatten([params])
_.each(params, function(field) {
delete doc[field]
})
}
return doc
}
/**
* @param doc the doc to be updated
* @param params Of three kinds
* {_path,_value} //The value to be pulled at nested path
* {_path,_values}//Each value in values is pulled
* {attrs}//Key value pairs. The values for a key will be pulled from arrays at each key
*/
var pull = function(doc,params) {
if (params._path) {
var keys = params._path
var nested
try {
nested = get.last_parent(doc,keys)
} catch(err) {
return
}
var lastKey = keys[keys.length - 1].key || keys[keys.length - 1]
if (!nested || !nested[lastKey]) {
return
}
if (params._value) {//Push the single element
nested[lastKey] = filterOut(nested[lastKey], params._value)
}
else if (params._values) {//Push the array of elements
_.each(params._values, function(value) {
nested[lastKey] = filterOut(nested[lastKey], value)
})
}
} else { //These are single depth keys/attrs to be set in the doc
_.each(_.keys(params), function(field) {
if (!_.isArray(params[field])) {
doc[field] = filterOut(doc[field], params[field])
} else {
_.each(params[field], function(value) {
doc[field] = filterOut(doc[field], value)
})
}
})
}
return doc
}
function filterOut(list, toPull) {
return _.filter(list, function(item) {
var checks = [_.isNumber, _.isBoolean, _.isString]
for (var check in checks) {
if (checks[check](toPull) || checks[check](item)) {
return toPull !== item
}
};
const toMatchFromItem = _.pick(item, Object.keys(toPull));
return !deepEqual(toMatchFromItem, toPull);
})
}
function addToSet(doc, params, force) {
return push(doc, params, force, true)
}
/**
* @param doc the doc to be updated
* @param params Of three kinds
* {_path,value} //The value to be pushed at nested path
* {_path,values}//Each value in values is pushed
* {attrs}//Key value pairs to be pushed. If vals are arrays, they will be concatenated. The keys should be at top level of doc
*/
var push = function(doc,params,force, setUnique) {
if (params._path) {
var keys = params._path
var nested = get.last_parent(doc,keys,force)
var lastKey = keys[keys.length - 1].key || keys[keys.length - 1]
if (!nested[lastKey]) {
nested[lastKey] = []
}
if (params._value) {//Push the single element
if (setUnique && find(nested[lastKey], params._value)) {
return doc//Don't do anything since value exists
}
nested[lastKey].push(params._value)
}
else if (params._values) {//Push the array of elements
for (var i in params._values) {
if (setUnique && find(nested[lastKey], params._values[i])) {
continue //Don't do anything since value exists
}
nested[lastKey].push(params._values[i])
}
}
} else { //These are single depth keys/attrs to be set in the doc
pushAttrs(doc,params, setUnique)
}
return doc
}
var pushAttrs = function(doc, attrs, setUnique) {
var keys = Object.keys(attrs)
for (var i in keys) {
var key = keys[i]
if (!doc[key]) {
doc[key] = []
}
if (!_.isArray(doc[key])) {
doc[key] = [doc[key]]
}
if (!_.isArray(attrs[key])) {
if (setUnique && find(doc[key], attrs[key])) {
return doc//Don't do anything since value exists
}
doc[key].push(attrs[key])
} else {
for (var j in attrs[key]) {
if (setUnique && find(doc[key], attrs[key][j])) {
continue //Don't do anything since value exists
}
doc[key].push(attrs[key][j])
}
}
}
}
function find(arr, element) {
if (_.isObject(element)) {
return _.find(arr, element)
} else {
return arr.indexOf(element) > -1
}
}
//EXPORTS
module.exports.push = push
module.exports.addToSet = addToSet
module.exports.set = set
module.exports.pull = pull
module.exports.unset = unset
//END OF EXPORTS
if (require.main === module) {
console.log(
JSON.stringify(
addToSet(
{_source: {}},
{
_source: ['2', '4', '1']
},
true
)
)
)
}
//console.log(JSON.stringify(push([{a:3,b:4},{c:3,b:5,d:{f:[{a:2},{a:3,b:{v:2}}]}}],{_path:[{b:5},'d','f',{a:3},'c'],values:[2,3]})))
//console.log(push({x:2},{x: [5, 3]}))
//console.log(unset({x:{y:2,g:{}}, y: 2}, 'x'))
//console.log(JSON.stringify(push({x:{y:2,g:{}}},{_path:['x','g','h'],values:[3,2]})))
//console.log(push({x:{y:2,g:[]}},{_path:['x','g'],value:3}))