This repository was archived by the owner on Jan 28, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonutils.js
More file actions
66 lines (55 loc) · 1.73 KB
/
jsonutils.js
File metadata and controls
66 lines (55 loc) · 1.73 KB
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
(function() {
'use strict';
function JsonUtils () {}
JsonUtils.prototype = (function(){
//
// PRIVATE methods
//
var _goToElement0 = function (json, path, ind, cb) {
// the path is created from splitting a string on PATH_SEPARATOR.
// If that string is empty, then the element in the array
// is an empty string.
if (path.length === ind || path[ind].length === 0) {
cb(json);
} else {
// Walk down the path
if (json.constructor === Object) {
if (!json.hasOwnProperty(path[ind])) {
throw new Error('No property=[' + path[ind] + '] in ' + JSON.stringify(json, null, ' '));
}
_goToElement0(json[path[ind]], path, ind + 1, cb);
} else if (json.constructor === Array) {
var arrayInd = parseInt(path[ind], 10);
_goToElement0(json[arrayInd], path, ind + 1, cb);
} else {
throw new Error('Unexpected JSON type: ' + JSON.stringify(json, null, ' '));
}
}
};
//
// PUBLIC METHODS
//
return {
/**
* Goto goes to the element located at path, and calls the callback
* cb once there with that element as argument.
* The path is an array.
*/
goToElement: function (json, path, cb) {
_goToElement0.call(this, json, path, 0, cb);
}
}
})();
// Make it to work in node and browser and AMD style
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports = new JsonUtils();
} else {
if (typeof define === 'function' && define.amd) {
define([], function () {
return new JsonUtils();
});
} else {
window.jsonUtils = new JsonUtils();
}
}
})();