forked from manuelstofer/json-pointer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
199 lines (180 loc) · 4.28 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
'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 obj
* @param 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 obj
* @param pointer
* @returns {*}
*/
api.get = function get(obj, pointer) {
var tok,
refTokens = api.parse(pointer);
while (refTokens.length) {
tok = refTokens.shift();
if (!obj.hasOwnProperty(tok)) {
throw new Error('Invalid reference token: ' + tok);
}
obj = obj[tok];
}
return obj;
};
/**
* Sets a value on an object
*
* @param obj
* @param pointer
* @param value
*/
api.set = function set(obj, pointer, value) {
var refTokens = api.parse(pointer),
tok,
nextTok = refTokens[0];
while (refTokens.length > 1) {
tok = refTokens.shift();
nextTok = refTokens[0];
if (!obj.hasOwnProperty(tok)) {
if (nextTok.match(/^\d+$/)) {
obj[tok] = [];
} else {
obj[tok] = {};
}
}
obj = obj[tok];
}
obj[nextTok] = value;
return this;
};
/**
* Removes an attribute
*
* @param obj
* @param pointer
*/
api.remove = function (obj, pointer) {
var refTokens = api.parse(pointer);
var finalToken = refTokens.pop();
if (finalToken === undefined) {
throw new Error('Invalid JSON pointer for remove: "' + pointer + '"');
}
delete api.get(obj, api.compile(refTokens))[finalToken];
};
/**
* Returns a (pointer -> value) dictionary for an object
*
* @param obj
* @returns {{}}
*/
api.dict = function dict(obj) {
var results = {},
refTokens = [],
mapObj = function (cur) {
var type = Object.prototype.toString.call(cur);
if (type === '[object Object]' || ( type === '[object Array]' && cur.length ) ) {
each(cur, function (value, key) {
refTokens.push(String(key));
mapObj(value);
refTokens.pop();
});
} else {
results[api.compile(refTokens)] = cur;
}
};
mapObj(obj);
return results;
};
/**
* Iterates over an object
* Iterator: function (value, pointer) {}
*
* @param obj
* @param iterator
*/
api.walk = function walk(obj, iterator) {
each(api.dict(obj), iterator);
};
/**
* 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.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('/');
};