forked from mrcranky/jsonpatch-to-mongodb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
115 lines (102 loc) · 3.48 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
function toDot(path) {
return path.replace(/^\//, '').replace(/\//g, '.').replace(/~1/g, '/').replace(/~0/g, '~');
}
let idx = 0;
function translatePath(path, arrayFilters) {
const parts = path.split('/');
parts.shift();
let collected = [];
for (const part of parts) {
if (!part.match(/^\[/)) {
collected.push(part);
} else {
const id = part.match(/^\[(.+)\]$/)[1];
//identifiers must start with a lowercase letter
const filterIdentifier = `a${idx}`;
const newPart = `$[${filterIdentifier}]`
collected.push(newPart);
const filterKey = `${filterIdentifier}.id`;
const filter = { [filterKey]: id };
arrayFilters.add(filter);
idx++;
}
}
return collected.join('/');
}
module.exports = function(patches){
idx = 0;
var update = {};
const unsetUpdate = {};
const unsetArrayFilters = new Set();
const arrayFilters = new Set();
patches.map(function(p){
if (p.op == 'remove') {
p.path = translatePath(p.path, unsetArrayFilters);
} else {
p.path = translatePath(p.path, arrayFilters);
}
switch(p.op) {
case 'add':
const path = toDot(p.path);
const parts = path.split('.');
var positionPart = parts.length > 1 && parts[parts.length - 1];
var addToEnd = positionPart === '-';
var key = parts.slice(0, -1).join('.');
var $position = positionPart && parseInt(positionPart, 10) || null;
if (parseInt(positionPart) == 0) {
$position = 0;
}
if ($position !== null) {
update.$push = update.$push || {};
if (update.$push[key] === undefined) {
update.$push[key] = {
$each: [p.value],
$position: $position
};
} else {
if (update.$push[key] === null || update.$push[key].$position === undefined) {
throw new Error("Unsupported Operation! can't use add op with mixed positions");
}
var posDiff = $position - update.$push[key].$position;
if (posDiff > update.$push[key].$each.length) {
throw new Error("Unsupported Operation! can use add op only with contiguous positions");
}
update.$push[key].$each.splice(posDiff, 0, p.value);
update.$push[key].$position = Math.min($position, update.$push[key].$position);
}
} else if(addToEnd) {
update.$push = update.$push || {};
if (update.$push[key] === undefined) {
update.$push[key] = p.value;
} else {
if (update.$push[key] === null || update.$push[key].$each === undefined) {
update.$push[key] = {
$each: [update.$push[key]]
};
}
if (update.$push[key].$position !== undefined) {
throw new Error("Unsupported Operation! can't use add op with mixed positions");
}
update.$push[key].$each.push(p.value);
}
} else {
update.$set = update.$set || {};
update.$set[toDot(p.path)] = p.value;
}
break;
case 'remove':
unsetUpdate.$unset = unsetUpdate.$unset || {};
unsetUpdate.$unset[toDot(p.path)] = 1;
break;
case 'replace':
update.$set = update.$set || {};
update.$set[toDot(p.path)] = p.value;
break;
case 'test':
break;
default:
throw new Error('Unsupported Operation! op = ' + p.op);
}
});
return { update, unsetUpdate, arrayFilters: Array.from(arrayFilters), unsetArrayFilters: Array.from(unsetArrayFilters) };
};