-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
33 lines (28 loc) · 911 Bytes
/
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
var traverse = require('traverse');
module.exports.escape = getPatchFunction(escapeKey);
module.exports.unescape = getPatchFunction(unescapeKey);
module.exports.getPatchFunction = getPatchFunction;
module.exports.escapeKey = escapeKey;
module.exports.unescapeKey = unescapeKey;
function getPatchFunction(escapeFunction) {
return function patch(o) {
var t = traverse(o);
var output = Array.isArray(o) ? [] : {};
var tOutput = traverse(output);
t.forEach(function (value) {
if (this.path.length>0 && Array.isArray(value)) {
tOutput.set(this.path.map(escapeFunction), []);
}
if (this.path.length>0 && this.isLeaf) {
tOutput.set(this.path.map(escapeFunction), value);
}
});
return output;
};
}
function escapeKey(key) {
return key.replace(/\$/g, '\uff04').replace(/\./g, '\uff0e');
}
function unescapeKey(key) {
return key.replace(/\uff04/g, '$').replace(/\uff0e/g, '.');
}