|
| 1 | +/** |
| 2 | + * Take a key string and escape the characters to allow it to be used as a reference. |
| 3 | + * @param {string} key |
| 4 | + * @returns {string} The processed key. |
| 5 | + */ |
| 6 | +function processEscapeCharacters(key) { |
| 7 | + return key.replace(/~/g, '~0').replace(/\//g, '~1'); |
| 8 | +} |
| 9 | + |
| 10 | +/** |
| 11 | + * @param {string} reference The reference to get the components of. |
| 12 | + * @returns {string[]} The components of the reference. Escape characters will be converted to their representative values. |
| 13 | + */ |
| 14 | +function getComponents(reference) { |
| 15 | + const referenceWithoutPrefix = reference.startsWith('/') ? reference.substring(1) : reference; |
| 16 | + return referenceWithoutPrefix |
| 17 | + .split('/') |
| 18 | + .map(component => (component.indexOf('~') >= 0 ? component.replace(/~1/g, '/').replace(/~0/g, '~') : component)); |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * @param {string} reference The reference to check if it is a literal. |
| 23 | + * @returns true if the reference is a literal. |
| 24 | + */ |
| 25 | +function isLiteral(reference) { |
| 26 | + return !reference.startsWith('/'); |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Get an attribute value from a literal. |
| 31 | + * @param {Object} target |
| 32 | + * @param {string} literal |
| 33 | + */ |
| 34 | +function getFromLiteral(target, literal) { |
| 35 | + if (target !== null && target !== undefined && Object.prototype.hasOwnProperty.call(target, literal)) { |
| 36 | + return target[literal]; |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Gets the `target` object's value at the `reference`'s location. |
| 42 | + * |
| 43 | + * This method method follows the rules for accessing attributes for use |
| 44 | + * in evaluating clauses. |
| 45 | + * |
| 46 | + * Accessing the root of the target will always result in undefined. |
| 47 | + * |
| 48 | + * @param {Object} target |
| 49 | + * @param {string} reference |
| 50 | + * @returns The `target` object's value at the `reference`'s location. |
| 51 | + * Undefined if the field does not exist or if the reference is not valid. |
| 52 | + */ |
| 53 | +function get(target, reference) { |
| 54 | + if (reference === '' || reference === '/') { |
| 55 | + return undefined; |
| 56 | + } |
| 57 | + |
| 58 | + if (isLiteral(reference)) { |
| 59 | + return getFromLiteral(target, reference); |
| 60 | + } |
| 61 | + |
| 62 | + const components = getComponents(reference); |
| 63 | + let current = target; |
| 64 | + for (const component of components) { |
| 65 | + if ( |
| 66 | + current !== null && |
| 67 | + current !== undefined && |
| 68 | + typeof current === 'object' && |
| 69 | + // We do not want to allow indexing into an array. |
| 70 | + !Array.isArray(current) && |
| 71 | + // For arrays and strings, in addition to objects, a hasOwnProperty check |
| 72 | + // will be true for indexes (as strings or numbers), which are present |
| 73 | + // in the object/string/array. |
| 74 | + Object.prototype.hasOwnProperty.call(current, component) |
| 75 | + ) { |
| 76 | + current = current[component]; |
| 77 | + } else { |
| 78 | + return undefined; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + return current; |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * Compare two references and determine if they are equivalent. |
| 87 | + * @param {string} a |
| 88 | + * @param {string} b |
| 89 | + */ |
| 90 | +function compare(a, b) { |
| 91 | + const aIsLiteral = isLiteral(a); |
| 92 | + const bIsLiteral = isLiteral(b); |
| 93 | + if (aIsLiteral && bIsLiteral) { |
| 94 | + return a === b; |
| 95 | + } |
| 96 | + if (aIsLiteral) { |
| 97 | + const bComponents = getComponents(b); |
| 98 | + if (bComponents.length !== 1) { |
| 99 | + return false; |
| 100 | + } |
| 101 | + return a === bComponents[0]; |
| 102 | + } |
| 103 | + if (bIsLiteral) { |
| 104 | + const aComponents = getComponents(a); |
| 105 | + if (aComponents.length !== 1) { |
| 106 | + return false; |
| 107 | + } |
| 108 | + return b === aComponents[0]; |
| 109 | + } |
| 110 | + return a === b; |
| 111 | +} |
| 112 | + |
| 113 | +/** |
| 114 | + * @param {string} a |
| 115 | + * @param {string} b |
| 116 | + * @returns The two strings joined by '/'. |
| 117 | + */ |
| 118 | +function join(a, b) { |
| 119 | + return `${a}/${b}`; |
| 120 | +} |
| 121 | + |
| 122 | +/** |
| 123 | + * There are cases where a field could have been named with a preceeding '/'. |
| 124 | + * If that attribute was private, then the literal would appear to be a reference. |
| 125 | + * This method can be used to convert a literal to a reference in such situations. |
| 126 | + * @param {string} literal The literal to convert to a reference. |
| 127 | + * @returns A literal which has been converted to a reference. |
| 128 | + */ |
| 129 | +function literalToReference(literal) { |
| 130 | + return `/${processEscapeCharacters(literal)}`; |
| 131 | +} |
| 132 | + |
| 133 | +/** |
| 134 | + * Clone an object excluding the values referenced by a list of references. |
| 135 | + * @param {Object} target The object to clone. |
| 136 | + * @param {string[]} references A list of references from the cloned object. |
| 137 | + * @returns {{cloned: Object, excluded: string[]}} The cloned object and a list of excluded values. |
| 138 | + */ |
| 139 | +function cloneExcluding(target, references) { |
| 140 | + const stack = []; |
| 141 | + const cloned = {}; |
| 142 | + const excluded = []; |
| 143 | + |
| 144 | + stack.push( |
| 145 | + ...Object.keys(target).map(key => ({ |
| 146 | + key, |
| 147 | + ptr: literalToReference(key), |
| 148 | + source: target, |
| 149 | + parent: cloned, |
| 150 | + visited: [target], |
| 151 | + })) |
| 152 | + ); |
| 153 | + |
| 154 | + while (stack.length) { |
| 155 | + const item = stack.pop(); |
| 156 | + if (!references.some(ptr => compare(ptr, item.ptr))) { |
| 157 | + const value = item.source[item.key]; |
| 158 | + |
| 159 | + // Handle null because it overlaps with object, which we will want to handle later. |
| 160 | + if (value === null) { |
| 161 | + item.parent[item.key] = value; |
| 162 | + } else if (Array.isArray(value)) { |
| 163 | + item.parent[item.key] = [...value]; |
| 164 | + } else if (typeof value === 'object') { |
| 165 | + //Arrays and null must already be handled. |
| 166 | + |
| 167 | + //Prevent cycles by not visiting the same object |
| 168 | + //with in the same branch. Parallel branches |
| 169 | + //may contain the same object. |
| 170 | + if (item.visited.includes(value)) { |
| 171 | + continue; |
| 172 | + } |
| 173 | + |
| 174 | + item.parent[item.key] = {}; |
| 175 | + |
| 176 | + stack.push( |
| 177 | + ...Object.keys(value).map(key => ({ |
| 178 | + key, |
| 179 | + ptr: join(item.ptr, processEscapeCharacters(key)), |
| 180 | + source: value, |
| 181 | + parent: item.parent[item.key], |
| 182 | + visited: [...item.visited, value], |
| 183 | + })) |
| 184 | + ); |
| 185 | + } else { |
| 186 | + item.parent[item.key] = value; |
| 187 | + } |
| 188 | + } else { |
| 189 | + excluded.push(item.ptr); |
| 190 | + } |
| 191 | + } |
| 192 | + return { cloned, excluded: excluded.sort() }; |
| 193 | +} |
| 194 | + |
| 195 | +function isValidReference(reference) { |
| 196 | + return !reference.match(/\/\/|(^\/.*~[^0|^1])|~$/); |
| 197 | +} |
| 198 | + |
| 199 | +/** |
| 200 | + * Check if the given attribute reference is for the "kind" attribute. |
| 201 | + * @param {string} reference String containing an attribute reference. |
| 202 | + */ |
| 203 | +function isKind(reference) { |
| 204 | + // There are only 2 valid ways to specify the kind attribute, |
| 205 | + // so this just checks them. Given the current flow of evaluation |
| 206 | + // this is much less intense a process than doing full validation and parsing. |
| 207 | + return reference === 'kind' || reference === '/kind'; |
| 208 | +} |
| 209 | + |
| 210 | +module.exports = { |
| 211 | + cloneExcluding, |
| 212 | + compare, |
| 213 | + get, |
| 214 | + isValidReference, |
| 215 | + literalToReference, |
| 216 | + isKind, |
| 217 | +}; |
0 commit comments