8e7e72c7709f833310e46548cd5b7d22a72dd33005baad6576470265c1aca2dc8fee038d496b9097a3e674bf293a54b1e84d5631742b9a09bde7fb6fce79e8 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. # traverse <sup>[![Version Badge][npm-version-svg]][package-url]</sup>
  2. [![github actions][actions-image]][actions-url]
  3. [![coverage][codecov-image]][codecov-url]
  4. [![License][license-image]][license-url]
  5. [![Downloads][downloads-image]][downloads-url]
  6. [![npm badge][npm-badge-png]][package-url]
  7. Traverse and transform objects by visiting every node on a recursive walk.
  8. # examples
  9. ## transform negative numbers in-place
  10. negative.js
  11. ````javascript
  12. var traverse = require('traverse');
  13. var obj = [ 5, 6, -3, [ 7, 8, -2, 1 ], { f : 10, g : -13 } ];
  14. traverse(obj).forEach(function (x) {
  15. if (x < 0) this.update(x + 128);
  16. });
  17. console.dir(obj);
  18. ````
  19. Output:
  20. [ 5, 6, 125, [ 7, 8, 126, 1 ], { f: 10, g: 115 } ]
  21. ## collect leaf nodes
  22. leaves.js
  23. ````javascript
  24. var traverse = require('traverse');
  25. var obj = {
  26. a : [1,2,3],
  27. b : 4,
  28. c : [5,6],
  29. d : { e : [7,8], f : 9 },
  30. };
  31. var leaves = traverse(obj).reduce(function (acc, x) {
  32. if (this.isLeaf) acc.push(x);
  33. return acc;
  34. }, []);
  35. console.dir(leaves);
  36. ````
  37. Output:
  38. [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
  39. ## scrub circular references
  40. scrub.js:
  41. ````javascript
  42. var traverse = require('traverse');
  43. var obj = { a : 1, b : 2, c : [ 3, 4 ] };
  44. obj.c.push(obj);
  45. var scrubbed = traverse(obj).map(function (x) {
  46. if (this.circular) this.remove()
  47. });
  48. console.dir(scrubbed);
  49. ````
  50. output:
  51. { a: 1, b: 2, c: [ 3, 4 ] }
  52. # methods
  53. Each method that takes an `fn` uses the context documented below in the context
  54. section.
  55. ## .map(fn)
  56. Execute `fn` for each node in the object and return a new object with the
  57. results of the walk. To update nodes in the result use `this.update(value)`.
  58. ## .forEach(fn)
  59. Execute `fn` for each node in the object but unlike `.map()`, when
  60. `this.update()` is called it updates the object in-place.
  61. ## .reduce(fn, acc)
  62. For each node in the object, perform a
  63. [left-fold](http://en.wikipedia.org/wiki/Fold_(higher-order_function))
  64. with the return value of `fn(acc, node)`.
  65. If `acc` isn't specified, `acc` is set to the root object for the first step
  66. and the root element is skipped.
  67. ## .paths()
  68. Return an `Array` of every possible non-cyclic path in the object.
  69. Paths are `Array`s of string keys.
  70. ## .nodes()
  71. Return an `Array` of every node in the object.
  72. ## .clone()
  73. Create a deep clone of the object.
  74. ## .get(path)
  75. Get the element at the array `path`.
  76. ## .set(path, value)
  77. Set the element at the array `path` to `value`.
  78. ## .has(path)
  79. Return whether the element at the array `path` exists.
  80. # context
  81. Each method that takes a callback has a context (its `this` object) with these
  82. attributes:
  83. ## this.node
  84. The present node on the recursive walk
  85. ## this.path
  86. An array of string keys from the root to the present node
  87. ## this.parent
  88. The context of the node's parent.
  89. This is `undefined` for the root node.
  90. ## this.key
  91. The name of the key of the present node in its parent.
  92. This is `undefined` for the root node.
  93. ## this.isRoot, this.notRoot
  94. Whether the present node is the root node
  95. ## this.isLeaf, this.notLeaf
  96. Whether or not the present node is a leaf node (has no children)
  97. ## this.level
  98. Depth of the node within the traversal
  99. ## this.circular
  100. If the node equals one of its parents, the `circular` attribute is set to the
  101. context of that parent and the traversal progresses no deeper.
  102. ## this.update(value, stopHere=false)
  103. Set a new value for the present node.
  104. All the elements in `value` will be recursively traversed unless `stopHere` is
  105. true.
  106. ## this.remove(stopHere=false)
  107. Remove the current element from the output. If the node is in an Array it will
  108. be spliced off. Otherwise it will be deleted from its parent.
  109. ## this.delete(stopHere=false)
  110. Delete the current element from its parent in the output. Calls `delete` even on
  111. Arrays.
  112. ## this.before(fn)
  113. Call this function before any of the children are traversed.
  114. You can assign into `this.keys` here to traverse in a custom order.
  115. ## this.after(fn)
  116. Call this function after any of the children are traversed.
  117. ## this.pre(fn)
  118. Call this function before each of the children are traversed.
  119. ## this.post(fn)
  120. Call this function after each of the children are traversed.
  121. # install
  122. Using [npm](http://npmjs.org) do:
  123. $ npm install traverse
  124. # license
  125. MIT
  126. [package-url]: https://npmjs.org/package/traverse
  127. [npm-version-svg]: https://versionbadg.es/ljharb/traverse.svg
  128. [deps-svg]: https://david-dm.org/ljharb/traverse.svg
  129. [deps-url]: https://david-dm.org/ljharb/traverse
  130. [dev-deps-svg]: https://david-dm.org/ljharb/traverse/dev-status.svg
  131. [dev-deps-url]: https://david-dm.org/ljharb/traverse#info=devDependencies
  132. [npm-badge-png]: https://nodei.co/npm/traverse.png?downloads=true&stars=true
  133. [license-image]: https://img.shields.io/npm/l/traverse.svg
  134. [license-url]: LICENSE
  135. [downloads-image]: https://img.shields.io/npm/dm/traverse.svg
  136. [downloads-url]: https://npm-stat.com/charts.html?package=traverse
  137. [codecov-image]: https://codecov.io/gh/ljharb/traverse/branch/main/graphs/badge.svg
  138. [codecov-url]: https://app.codecov.io/gh/ljharb/traverse/
  139. [actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/ljharb/traverse
  140. [actions-url]: https://github.com/ljharb/traverse/actions