1 | /**
|
2 | * Given a Path and a key, return a new Path containing the new key.
|
3 | */
|
4 | export function addPath(prev, key, typename) {
|
5 | return { prev, key, typename };
|
6 | }
|
7 | /**
|
8 | * Given a Path, return an Array of the path keys.
|
9 | */
|
10 | export function pathToArray(path) {
|
11 | const flattened = [];
|
12 | let curr = path;
|
13 | while (curr) {
|
14 | flattened.push(curr.key);
|
15 | curr = curr.prev;
|
16 | }
|
17 | return flattened.reverse();
|
18 | }
|
19 | /**
|
20 | * Build a string describing the path.
|
21 | */
|
22 | export function printPathArray(path) {
|
23 | return path
|
24 | .map(key => (typeof key === 'number' ? '[' + key.toString() + ']' : '.' + key))
|
25 | .join('');
|
26 | }
|