forked from launchdarkly/node-server-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
attribute_reference.js
217 lines (197 loc) · 6.11 KB
/
attribute_reference.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/**
* Take a key string and escape the characters to allow it to be used as a reference.
* @param {string} key
* @returns {string} The processed key.
*/
function processEscapeCharacters(key) {
return key.replace(/~/g, '~0').replace(/\//g, '~1');
}
/**
* @param {string} reference The reference to get the components of.
* @returns {string[]} The components of the reference. Escape characters will be converted to their representative values.
*/
function getComponents(reference) {
const referenceWithoutPrefix = reference.startsWith('/') ? reference.substring(1) : reference;
return referenceWithoutPrefix
.split('/')
.map(component => (component.indexOf('~') >= 0 ? component.replace(/~1/g, '/').replace(/~0/g, '~') : component));
}
/**
* @param {string} reference The reference to check if it is a literal.
* @returns true if the reference is a literal.
*/
function isLiteral(reference) {
return !reference.startsWith('/');
}
/**
* Get an attribute value from a literal.
* @param {Object} target
* @param {string} literal
*/
function getFromLiteral(target, literal) {
if (target !== null && target !== undefined && Object.prototype.hasOwnProperty.call(target, literal)) {
return target[literal];
}
}
/**
* Gets the `target` object's value at the `reference`'s location.
*
* This method method follows the rules for accessing attributes for use
* in evaluating clauses.
*
* Accessing the root of the target will always result in undefined.
*
* @param {Object} target
* @param {string} reference
* @returns The `target` object's value at the `reference`'s location.
* Undefined if the field does not exist or if the reference is not valid.
*/
function get(target, reference) {
if (reference === '' || reference === '/') {
return undefined;
}
if (isLiteral(reference)) {
return getFromLiteral(target, reference);
}
const components = getComponents(reference);
let current = target;
for (const component of components) {
if (
current !== null &&
current !== undefined &&
typeof current === 'object' &&
// We do not want to allow indexing into an array.
!Array.isArray(current) &&
// For arrays and strings, in addition to objects, a hasOwnProperty check
// will be true for indexes (as strings or numbers), which are present
// in the object/string/array.
Object.prototype.hasOwnProperty.call(current, component)
) {
current = current[component];
} else {
return undefined;
}
}
return current;
}
/**
* Compare two references and determine if they are equivalent.
* @param {string} a
* @param {string} b
*/
function compare(a, b) {
const aIsLiteral = isLiteral(a);
const bIsLiteral = isLiteral(b);
if (aIsLiteral && bIsLiteral) {
return a === b;
}
if (aIsLiteral) {
const bComponents = getComponents(b);
if (bComponents.length !== 1) {
return false;
}
return a === bComponents[0];
}
if (bIsLiteral) {
const aComponents = getComponents(a);
if (aComponents.length !== 1) {
return false;
}
return b === aComponents[0];
}
return a === b;
}
/**
* @param {string} a
* @param {string} b
* @returns The two strings joined by '/'.
*/
function join(a, b) {
return `${a}/${b}`;
}
/**
* There are cases where a field could have been named with a preceeding '/'.
* If that attribute was private, then the literal would appear to be a reference.
* This method can be used to convert a literal to a reference in such situations.
* @param {string} literal The literal to convert to a reference.
* @returns A literal which has been converted to a reference.
*/
function literalToReference(literal) {
return `/${processEscapeCharacters(literal)}`;
}
/**
* Clone an object excluding the values referenced by a list of references.
* @param {Object} target The object to clone.
* @param {string[]} references A list of references from the cloned object.
* @returns {{cloned: Object, excluded: string[]}} The cloned object and a list of excluded values.
*/
function cloneExcluding(target, references) {
const stack = [];
const cloned = {};
const excluded = [];
stack.push(
...Object.keys(target).map(key => ({
key,
ptr: literalToReference(key),
source: target,
parent: cloned,
visited: [target],
}))
);
while (stack.length) {
const item = stack.pop();
if (!references.some(ptr => compare(ptr, item.ptr))) {
const value = item.source[item.key];
// Handle null because it overlaps with object, which we will want to handle later.
if (value === null) {
item.parent[item.key] = value;
} else if (Array.isArray(value)) {
item.parent[item.key] = [...value];
} else if (typeof value === 'object') {
//Arrays and null must already be handled.
//Prevent cycles by not visiting the same object
//with in the same branch. Parallel branches
//may contain the same object.
if (item.visited.includes(value)) {
continue;
}
item.parent[item.key] = {};
stack.push(
...Object.keys(value).map(key => ({
key,
ptr: join(item.ptr, processEscapeCharacters(key)),
source: value,
parent: item.parent[item.key],
visited: [...item.visited, value],
}))
);
} else {
item.parent[item.key] = value;
}
} else {
excluded.push(item.ptr);
}
}
return { cloned, excluded: excluded.sort() };
}
function isValidReference(reference) {
return !reference.match(/\/\/|(^\/.*~[^0|^1])|~$/);
}
/**
* Check if the given attribute reference is for the "kind" attribute.
* @param {string} reference String containing an attribute reference.
*/
function isKind(reference) {
// There are only 2 valid ways to specify the kind attribute,
// so this just checks them. Given the current flow of evaluation
// this is much less intense a process than doing full validation and parsing.
return reference === 'kind' || reference === '/kind';
}
module.exports = {
cloneExcluding,
compare,
get,
isValidReference,
literalToReference,
isKind,
};