UNPKG

866 BJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.printPathArray = exports.pathToArray = exports.addPath = void 0;
4/**
5 * Given a Path and a key, return a new Path containing the new key.
6 */
7function addPath(prev, key, typename) {
8 return { prev, key, typename };
9}
10exports.addPath = addPath;
11/**
12 * Given a Path, return an Array of the path keys.
13 */
14function pathToArray(path) {
15 const flattened = [];
16 let curr = path;
17 while (curr) {
18 flattened.push(curr.key);
19 curr = curr.prev;
20 }
21 return flattened.reverse();
22}
23exports.pathToArray = pathToArray;
24/**
25 * Build a string describing the path.
26 */
27function printPathArray(path) {
28 return path
29 .map(key => (typeof key === 'number' ? '[' + key.toString() + ']' : '.' + key))
30 .join('');
31}
32exports.printPathArray = printPathArray;