Uh oh!
There was an error while loading. Please reload this page.
forked from serkanyersen/jsonplus
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.js
More file actions
Latest commit
106 lines (92 loc) · 2.78 KB
/
Copy pathparse.js
File metadata and controls
106 lines (92 loc) · 2.78 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
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
'use strict';
// Helper to strip comments
varstrip=require('strip-json-comments');
// Tag matcher
vartag=/^\@self[\.\[]/;
/**
* Check given object is Actual object or not
* @param {any} obj obj to check against
* @return {Boolean} true if object is object
*/
functionisObject(obj){
returnobj===Object(obj)&&!Array.isArray(obj);
}
/**
* Resolves JS array notation in string to actual value
* @param {Object} object Actual Object
* @param {string} reference path defined in string
* @return {any} matched value or undefined when not found
*/
functionresolvePath(object,reference){
functionarrDeref(o,ref){
varkey=ref.replace(/^['"]|['"\]]+$/g,'');
return!ref ? o : (o[key]);
}
functiondotDeref(o,ref){
returnref.split('[').reduce(arrDeref,o);
}
return!reference ? object : reference.split('.').reduce(dotDeref,object);
}
/**
* Simple template tag matcher, finds tags and
* evalues values
* @param {object} object Object itself to resolve tags agains
* @param {string} template String to look for tags
* @return {string} Evaluated string
*/
functionparseTemplate(object,template){
varhtml=template||'';
returnhtml.replace(/\{\{\s*(.*?)\s*\}\}/gim,function(all,match){
returnresolvePath(object,match.replace(tag,''))||all;
});
}
/**
* Recursively goes through JSON object and resolves
* all self references
* @param {Object} obj JSON Object part
* @param {Object} self Full object
* @return {Object} evaluated JSON Object part
*/
functionresolve(obj,self){
varnewObj;
self=self||obj;
// If object go through each value and call self again
if(isObject(obj)){
newObj={};
Object.keys(obj).forEach(function(key){
newObj[key]=resolve(obj[key],self);
});
}
// if array go through each item and call self
elseif(Array.isArray(obj)){
newObj=obj.map(function(val){
returnresolve(val,self);
});
}
// if string and starts with the refrence tag
// evaluate and return new value
elseif(typeofobj==='string'&&tag.test(obj)){
newObj=resolvePath(self,obj.replace(tag,''));
}
// if string check for template tags
elseif(typeofobj==='string'){
newObj=parseTemplate(self,obj);
}
// anything else
else{
newObj=obj;
}
// yes
returnnewObj;
}
/**
* Parses json string by removing comments and resolving self references
* @param {string} data JSON string
* @return {Object} parsed JSON Object
*/
exports.parse=functionjsonPlusParse(data){
varobj=JSON.parse(strip(data));
obj=resolve(obj);
returnobj;
};
exports.resolve=resolve;