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
106 lines (92 loc) · 2.78 KB
/
parse.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
var strip = require('strip-json-comments');
// Tag matcher
var tag = /^\@self[\.\[]/;
/**
* Check given object is Actual object or not
* @param {any} obj obj to check against
* @return {Boolean} true if object is object
*/
function isObject(obj) {
return obj === 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
*/
function resolvePath(object, reference) {
function arrDeref(o, ref) {
var key = ref.replace(/^['"]|['"\]]+$/g, '');
return !ref ? o : (o[key]);
}
function dotDeref(o, ref) {
return ref.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
*/
function parseTemplate(object, template) {
var html = template || '';
return html.replace(/\{\{\s*(.*?)\s*\}\}/gim, function(all, match) {
return resolvePath(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
*/
function resolve(obj, self) {
var newObj;
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
else if (Array.isArray(obj)) {
newObj = obj.map(function(val) {
return resolve(val, self);
});
}
// if string and starts with the refrence tag
// evaluate and return new value
else if (typeof obj === 'string' && tag.test(obj)) {
newObj = resolvePath(self, obj.replace(tag, ''));
}
// if string check for template tags
else if (typeof obj === 'string') {
newObj = parseTemplate(self, obj);
}
// anything else
else {
newObj = obj;
}
// yes
return newObj;
}
/**
* Parses json string by removing comments and resolving self references
* @param {string} data JSON string
* @return {Object} parsed JSON Object
*/
exports.parse = function jsonPlusParse(data) {
var obj = JSON.parse(strip(data));
obj = resolve(obj);
return obj;
};
exports.resolve = resolve;