legacy.d.cts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. interface TraverseOptions {
  2. /**
  3. * If true, does not alter the original object
  4. */
  5. immutable?: boolean;
  6. /**
  7. * If false, removes all symbols from traversed objects
  8. *
  9. * @default false
  10. */
  11. includeSymbols?: boolean;
  12. }
  13. interface TraverseContext {
  14. /**
  15. * The present node on the recursive walk
  16. */
  17. node: any;
  18. /**
  19. * An array of string keys from the root to the present node
  20. */
  21. path: PropertyKey[];
  22. /**
  23. * The context of the node's parent.
  24. * This is `undefined` for the root node.
  25. */
  26. parent: TraverseContext | undefined;
  27. /**
  28. * The contexts of the node's parents.
  29. */
  30. parents: TraverseContext[];
  31. /**
  32. * The name of the key of the present node in its parent.
  33. * This is `undefined` for the root node.
  34. */
  35. key: PropertyKey | undefined;
  36. /**
  37. * Whether the present node is the root node
  38. */
  39. isRoot: boolean;
  40. /**
  41. * Whether the present node is not the root node
  42. */
  43. notRoot: boolean;
  44. /**
  45. * Whether the present node is the last node
  46. */
  47. isLast: boolean;
  48. /**
  49. * Whether the present node is the first node
  50. */
  51. isFirst: boolean;
  52. /**
  53. * Whether or not the present node is a leaf node (has no children)
  54. */
  55. isLeaf: boolean;
  56. /**
  57. * Whether or not the present node is not a leaf node (has children)
  58. */
  59. notLeaf: boolean;
  60. /**
  61. * Depth of the node within the traversal
  62. */
  63. level: number;
  64. /**
  65. * If the node equals one of its parents, the `circular` attribute is set to the context of that parent and the traversal progresses no deeper.
  66. */
  67. circular: TraverseContext | undefined;
  68. /**
  69. * Set a new value for the present node.
  70. *
  71. * All the elements in `value` will be recursively traversed unless `stopHere` is true (false by default).
  72. */
  73. update(value: any, stopHere?: boolean): void;
  74. /**
  75. * Remove the current element from the output. If the node is in an Array it will be spliced off. Otherwise it will be deleted from its parent.
  76. */
  77. remove(stopHere?: boolean): void;
  78. /**
  79. * Delete the current element from its parent in the output. Calls `delete` even on Arrays.
  80. */
  81. delete(stopHere?: boolean): void;
  82. /**
  83. * Object keys of the node.
  84. */
  85. keys: PropertyKey[] | null;
  86. /**
  87. * Call this function before all of the children are traversed.
  88. * You can assign into `this.keys` here to traverse in a custom order.
  89. */
  90. before(callback: (this: TraverseContext, value: any) => void): void;
  91. /**
  92. * Call this function after all of the children are traversed.
  93. */
  94. after(callback: (this: TraverseContext, value: any) => void): void;
  95. /**
  96. * Call this function before each of the children are traversed.
  97. */
  98. pre(callback: (this: TraverseContext, child: any, key: any) => void): void;
  99. /**
  100. * Call this function after each of the children are traversed.
  101. */
  102. post(callback: (this: TraverseContext, child: any) => void): void;
  103. /**
  104. * Stops traversal entirely.
  105. */
  106. stop(): void;
  107. /**
  108. * Prevents traversing descendents of the current node.
  109. */
  110. block(): void;
  111. }
  112. /** @deprecated Import `Traverse` from `neotraverse/modern` instead */
  113. declare class Traverse {
  114. #private;
  115. constructor(obj: any, options?: TraverseOptions);
  116. /**
  117. * Get the element at the array `path`.
  118. */
  119. get(paths: PropertyKey[]): any;
  120. /**
  121. * Return whether the element at the array `path` exists.
  122. */
  123. has(paths: PropertyKey[]): boolean;
  124. /**
  125. * Set the element at the array `path` to `value`.
  126. */
  127. set(path: PropertyKey[], value: any): any;
  128. /**
  129. * Execute `fn` for each node in the object and return a new object with the results of the walk. To update nodes in the result use `this.update(value)`.
  130. */
  131. map(cb: (this: TraverseContext, v: any) => void): any;
  132. /**
  133. * Execute `fn` for each node in the object but unlike `.map()`, when `this.update()` is called it updates the object in-place.
  134. */
  135. forEach(cb: (this: TraverseContext, v: any) => void): any;
  136. /**
  137. * For each node in the object, perform a [left-fold](http://en.wikipedia.org/wiki/Fold_(higher-order_function)) with the return value of `fn(acc, node)`.
  138. *
  139. * If `init` isn't specified, `init` is set to the root object for the first step and the root element is skipped.
  140. */
  141. reduce(cb: (this: TraverseContext, acc: any, v: any) => void, init?: any): any;
  142. /**
  143. * Return an `Array` of every possible non-cyclic path in the object.
  144. * Paths are `Array`s of string keys.
  145. */
  146. paths(): PropertyKey[][];
  147. /**
  148. * Return an `Array` of every node in the object.
  149. */
  150. nodes(): any[];
  151. /**
  152. * Create a deep clone of the object.
  153. */
  154. clone(): any;
  155. }
  156. declare const traverse: {
  157. (obj: any, options?: TraverseOptions): Traverse;
  158. /**
  159. * Get the element at the array `path`.
  160. */
  161. get(obj: any, paths: PropertyKey[], options?: TraverseOptions): any;
  162. /**
  163. * Set the element at the array `path` to `value`.
  164. */
  165. set(obj: any, path: PropertyKey[], value: any, options?: TraverseOptions): any;
  166. /**
  167. * Return whether the element at the array `path` exists.
  168. */
  169. has(obj: any, paths: PropertyKey[], options?: TraverseOptions): boolean;
  170. /**
  171. * Execute `fn` for each node in the object and return a new object with the results of the walk. To update nodes in the result use `this.update(value)`.
  172. */
  173. map(obj: any, cb: (this: TraverseContext, v: any) => void, options?: TraverseOptions): any;
  174. /**
  175. * Execute `fn` for each node in the object but unlike `.map()`, when `this.update()` is called it updates the object in-place.
  176. */
  177. forEach(obj: any, cb: (this: TraverseContext, v: any) => void, options?: TraverseOptions): any;
  178. /**
  179. * For each node in the object, perform a [left-fold](http://en.wikipedia.org/wiki/Fold_(higher-order_function)) with the return value of `fn(acc, node)`.
  180. *
  181. * If `init` isn't specified, `init` is set to the root object for the first step and the root element is skipped.
  182. */
  183. reduce(obj: any, cb: (this: TraverseContext, acc: any, v: any) => void, init?: any, options?: TraverseOptions): any;
  184. /**
  185. * Return an `Array` of every possible non-cyclic path in the object.
  186. * Paths are `Array`s of string keys.
  187. */
  188. paths(obj: any, options?: TraverseOptions): PropertyKey[][];
  189. /**
  190. * Return an `Array` of every node in the object.
  191. */
  192. nodes(obj: any, options?: TraverseOptions): any[];
  193. /**
  194. * Create a deep clone of the object.
  195. */
  196. clone(obj: any, options?: TraverseOptions): any;
  197. };
  198. export { type TraverseContext, type TraverseOptions, traverse as default };