-
Notifications
You must be signed in to change notification settings - Fork 42
/
index.js
232 lines (208 loc) · 5.43 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
'use strict';
var each = require('foreach');
module.exports = api;
/**
* Convenience wrapper around the api.
* Calls `.get` when called with an `object` and a `pointer`.
* Calls `.set` when also called with `value`.
* If only supplied `object`, returns a partially applied function, mapped to the object.
*
* @param {Object} obj
* @param {String|Array} pointer
* @param value
* @returns {*}
*/
function api (obj, pointer, value) {
// .set()
if (arguments.length === 3) {
return api.set(obj, pointer, value);
}
// .get()
if (arguments.length === 2) {
return api.get(obj, pointer);
}
// Return a partially applied function on `obj`.
var wrapped = api.bind(api, obj);
// Support for oo style
for (var name in api) {
if (api.hasOwnProperty(name)) {
wrapped[name] = api[name].bind(wrapped, obj);
}
}
return wrapped;
}
/**
* Lookup a json pointer in an object
*
* @param {Object} obj
* @param {String|Array} pointer
* @returns {*}
*/
api.get = function get (obj, pointer) {
var refTokens = Array.isArray(pointer) ? pointer : api.parse(pointer);
for (var i = 0; i < refTokens.length; ++i) {
var tok = refTokens[i];
if (!(typeof obj == 'object' && tok in obj)) {
throw new Error('Invalid reference token: ' + tok);
}
obj = obj[tok];
}
return obj;
};
/**
* Sets a value on an object
*
* @param {Object} obj
* @param {String|Array} pointer
* @param value
*/
api.set = function set (obj, pointer, value) {
var refTokens = Array.isArray(pointer) ? pointer : api.parse(pointer),
nextTok = refTokens[0];
if (refTokens.length === 0) {
throw Error('Can not set the root object');
}
for (var i = 0; i < refTokens.length - 1; ++i) {
var tok = refTokens[i];
if (typeof tok !== 'string' && typeof tok !== 'number') {
tok = String(tok)
}
if (tok === "__proto__" || tok === "constructor" || tok === "prototype") {
continue
}
if (tok === '-' && Array.isArray(obj)) {
tok = obj.length;
}
nextTok = refTokens[i + 1];
if (!(tok in obj)) {
if (nextTok.match(/^(\d+|-)$/)) {
obj[tok] = [];
} else {
obj[tok] = {};
}
}
obj = obj[tok];
}
if (nextTok === '-' && Array.isArray(obj)) {
nextTok = obj.length;
}
obj[nextTok] = value;
return this;
};
/**
* Removes an attribute
*
* @param {Object} obj
* @param {String|Array} pointer
*/
api.remove = function (obj, pointer) {
var refTokens = Array.isArray(pointer) ? pointer : api.parse(pointer);
var finalToken = refTokens[refTokens.length -1];
if (finalToken === undefined) {
throw new Error('Invalid JSON pointer for remove: "' + pointer + '"');
}
var parent = api.get(obj, refTokens.slice(0, -1));
if (Array.isArray(parent)) {
var index = +finalToken;
if (finalToken === '' && isNaN(index)) {
throw new Error('Invalid array index: "' + finalToken + '"');
}
Array.prototype.splice.call(parent, index, 1);
} else {
delete parent[finalToken];
}
};
/**
* Returns a (pointer -> value) dictionary for an object
*
* @param obj
* @param {function} descend
* @returns {}
*/
api.dict = function dict (obj, descend) {
var results = {};
api.walk(obj, function (value, pointer) {
results[pointer] = value;
}, descend);
return results;
};
/**
* Iterates over an object
* Iterator: function (value, pointer) {}
*
* @param obj
* @param {function} iterator
* @param {function} descend
*/
api.walk = function walk (obj, iterator, descend) {
var refTokens = [];
descend = descend || function (value) {
var type = Object.prototype.toString.call(value);
return type === '[object Object]' || type === '[object Array]';
};
(function next (cur) {
each(cur, function (value, key) {
refTokens.push(String(key));
if (descend(value)) {
next(value);
} else {
iterator(value, api.compile(refTokens));
}
refTokens.pop();
});
}(obj));
};
/**
* Tests if an object has a value for a json pointer
*
* @param obj
* @param pointer
* @returns {boolean}
*/
api.has = function has (obj, pointer) {
try {
api.get(obj, pointer);
} catch (e) {
return false;
}
return true;
};
/**
* Escapes a reference token
*
* @param str
* @returns {string}
*/
api.escape = function escape (str) {
return str.toString().replace(/~/g, '~0').replace(/\//g, '~1');
};
/**
* Unescapes a reference token
*
* @param str
* @returns {string}
*/
api.unescape = function unescape (str) {
return str.replace(/~1/g, '/').replace(/~0/g, '~');
};
/**
* Converts a json pointer into a array of reference tokens
*
* @param pointer
* @returns {Array}
*/
api.parse = function parse (pointer) {
if (pointer === '') { return []; }
if (pointer.charAt(0) !== '/') { throw new Error('Invalid JSON pointer: ' + pointer); }
return pointer.substring(1).split(/\//).map(api.unescape);
};
/**
* Builds a json pointer from a array of reference tokens
*
* @param refTokens
* @returns {string}
*/
api.compile = function compile (refTokens) {
if (refTokens.length === 0) { return ''; }
return '/' + refTokens.map(api.escape).join('/');
};