modern.d.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 `ctx.keys` here to traverse in a custom order.
  89. */
  90. before(callback: (ctx: TraverseContext, value: any) => void): void;
  91. /**
  92. * Call this function after all of the children are traversed.
  93. */
  94. after(callback: (ctx: TraverseContext, value: any) => void): void;
  95. /**
  96. * Call this function before each of the children are traversed.
  97. */
  98. pre(callback: (ctx: TraverseContext, child: any, key: any) => void): void;
  99. /**
  100. * Call this function after each of the children are traversed.
  101. */
  102. post(callback: (ctx: 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. declare class Traverse {
  113. #private;
  114. constructor(obj: any, options?: TraverseOptions);
  115. /**
  116. * Get the element at the array `path`.
  117. */
  118. get(paths: PropertyKey[]): any;
  119. /**
  120. * Return whether the element at the array `path` exists.
  121. */
  122. has(paths: PropertyKey[]): boolean;
  123. /**
  124. * Set the element at the array `path` to `value`.
  125. */
  126. set(path: PropertyKey[], value: any): any;
  127. /**
  128. * 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)`.
  129. */
  130. map(cb: (ctx: TraverseContext, v: any) => void): any;
  131. /**
  132. * Execute `fn` for each node in the object but unlike `.map()`, when `this.update()` is called it updates the object in-place.
  133. */
  134. forEach(cb: (ctx: TraverseContext, v: any) => void): any;
  135. /**
  136. * 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)`.
  137. *
  138. * If `init` isn't specified, `init` is set to the root object for the first step and the root element is skipped.
  139. */
  140. reduce(cb: (ctx: TraverseContext, acc: any, v: any) => void, init?: any): any;
  141. /**
  142. * Return an `Array` of every possible non-cyclic path in the object.
  143. * Paths are `Array`s of string keys.
  144. */
  145. paths(): PropertyKey[][];
  146. /**
  147. * Return an `Array` of every node in the object.
  148. */
  149. nodes(): any[];
  150. /**
  151. * Create a deep clone of the object.
  152. */
  153. clone(): any;
  154. }
  155. export { Traverse, type TraverseContext, type TraverseOptions };