-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
78 lines (64 loc) · 2.56 KB
/
index.js
File metadata and controls
78 lines (64 loc) · 2.56 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
const getNestedValue = (obj, key) => key.split('.').reduce((o,i) => {
// Check for undefined references
if (!(i in o)) {
throw `Cannot find self refrencing property ${key} in object`;
}
return o[i];
}, obj);
// tagged template literal that checks if the template literal is only a single string.
// Defined in string form to pass into Function constructor
const isSingleTag = 'const isSingleTag = (strings, ...keys) => { return (keys.length === 1 && strings[0] === "" && strings[1] === ""); };'
// tagged template literal that returns the first expression
// Defined in string form to pass into Function constructor
const returnSingleTag = 'const returnSingleTag = (_, ...keys) => { return keys[0]; };'
// if the template literal only contains only a single expression, return a value, otherwise return a string
const evaluateTemplateLiteral = expression => {
const isSingleExpression = Function('"use strict";' + isSingleTag + ' return isSingleTag`' + expression + "`;")();
if (isSingleExpression) {
return Function('"use strict";' + returnSingleTag + ' return returnSingleTag`' + expression + "`;")();
}
return Function('"use strict"; return `' + expression + "`;")();
}
// Uses backtracking DFS to traverse object graph
const sro = object => {
const parsedValues = {};
const checkedReferences = [];
const parseValue= (value) => {
if (typeof value !== 'string') {
return value;
}
// lookup any `this` values
const expression = value.replace(/this\.[A-Za-z0-9_.-]+/g, thisMatch => {
const key = thisMatch.slice(5)
if (!Object.prototype.hasOwnProperty.call(parsedValues, key)) {
parsedValues[key] = lookup(key);
}
return JSON.stringify(parsedValues[key]);
});
return evaluateTemplateLiteral(expression);
}
const lookup = (key) => {
// Check for circular references
if (checkedReferences.indexOf(key) !== -1) {
throw `Circular dependency found in self referencing object: ${JSON.stringify(checkedReferences)}`;
}
// Add the key to the stack to support back tracking
checkedReferences.push(key);
const value = parseValue(getNestedValue(object, key));
// pop the key from the stack to continue back tracking
checkedReferences.pop();
return value;
}
const parseObject = (currentObject) => {
Object.entries(currentObject).forEach(([key, value]) => {
if (typeof value === 'object') {
parseObject(value);
} else {
currentObject[key] = parseValue(value);
}
});
}
parseObject(object);
return object;
};
module.exports = sro;