Path.js.flow 558 B

1234567891011121314151617181920212223242526
  1. // @flow strict
  2. export type Path = {|
  3. +prev: Path | void,
  4. +key: string | number,
  5. |};
  6. /**
  7. * Given a Path and a key, return a new Path containing the new key.
  8. */
  9. export function addPath(prev: $ReadOnly<Path> | void, key: string | number) {
  10. return { prev, key };
  11. }
  12. /**
  13. * Given a Path, return an Array of the path keys.
  14. */
  15. export function pathToArray(path: ?$ReadOnly<Path>): Array<string | number> {
  16. const flattened = [];
  17. let curr = path;
  18. while (curr) {
  19. flattened.push(curr.key);
  20. curr = curr.prev;
  21. }
  22. return flattened.reverse();
  23. }