This repository has been archived by the owner on Dec 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
index.js
70 lines (66 loc) · 2.35 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
function toDot(path) {
return path.replace(/^\//, '').replace(/\//g, '.').replace(/~1/g, '/').replace(/~0/g, '~');
}
module.exports = function(patches){
var update = {};
patches.map(function(p){
switch(p.op) {
case 'add':
var path = toDot(p.path),
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;
update.$push = update.$push || {};
if ($position !== null) {
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) {
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 {
throw new Error("Unsupported Operation! can't use add op without position");
}
break;
case 'remove':
update.$unset = update.$unset || {};
update.$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;
};