Path.js 551 B

123456789101112131415161718192021222324252627282930313233
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true,
  4. });
  5. exports.addPath = addPath;
  6. exports.pathToArray = pathToArray;
  7. /**
  8. * Given a Path and a key, return a new Path containing the new key.
  9. */
  10. function addPath(prev, key, typename) {
  11. return {
  12. prev,
  13. key,
  14. typename,
  15. };
  16. }
  17. /**
  18. * Given a Path, return an Array of the path keys.
  19. */
  20. function pathToArray(path) {
  21. const flattened = [];
  22. let curr = path;
  23. while (curr) {
  24. flattened.push(curr.key);
  25. curr = curr.prev;
  26. }
  27. return flattened.reverse();
  28. }