forked from frameable/pigeon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreverse.js
47 lines (34 loc) · 877 Bytes
/
reverse.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
const { _clone, _objId } = require('./helpers');
function reverse(changes) {
const reversed = _clone(changes).reverse();
for (const change of reversed) {
if (change.op == 'add') {
change.op = 'remove';
const id = _objId(change.value);
if (id) {
change._index = change.path.split('/').pop();
change.path = change.path.replace(/\d+$/, `[${id}]`);
}
} else if (change.op == 'remove') {
change.op = 'add';
}
if ('_prev' in change) {
var _prev = change._prev;
}
if ('value' in change) {
var _value = change.value;
}
if (_prev === undefined) {
delete change.value;
} else {
change.value = _prev;
}
if (_value === undefined) {
delete change._prev;
} else {
change._prev = _value;
}
}
return reversed;
}
module.exports = reverse;