tree-visit
Advanced tools
Comparing version 0.1.1 to 0.1.2
@@ -8,3 +8,3 @@ "use strict"; | ||
LinePrefix["LastChild"] = "\u2514\u2500\u2500 "; | ||
LinePrefix["NestedChild"] = "\u2502\u00A0\u00A0 "; | ||
LinePrefix["NestedChild"] = "\u2502 "; | ||
LinePrefix["LastNestedChild"] = " "; | ||
@@ -11,0 +11,0 @@ })(LinePrefix || (LinePrefix = {})); |
{ | ||
"name": "tree-visit", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "A tree traversal library.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -249,23 +249,14 @@ # tree-visit | ||
### `visit` | ||
### `flat` | ||
Visit each node in the tree, calling an optional `onEnter` and `onLeave` for each. | ||
Returns an array containing the root node and all of its descendants. | ||
From `onEnter`: | ||
This is analogous to `Array.prototype.flat` for flattening arrays. | ||
- return nothing or `undefined` to continue | ||
- return `"skip"` to skip the children of that node and the subsequent `onLeave` | ||
- return `"stop"` to end traversal | ||
**Type**: `function flat<T>(node: T, options: BaseOptions<T>): T[]` | ||
From `onLeave`: | ||
- return nothing or `undefined` to continue | ||
- return `"stop"` to end traversal | ||
**Type**: `function visit<T>(node: T, options: VisitOptions<T>): void` | ||
#### Example | ||
```js | ||
import { visit } from 'tree-visit' | ||
import { flat } from 'tree-visit' | ||
@@ -285,9 +276,4 @@ const getChildren = (node) => node.children || [] | ||
visit(rootNode, { | ||
getChildren, | ||
onEnter: (node) => { | ||
console.log(node) | ||
}, | ||
}) | ||
// #=> a, b, c, d | ||
flat(rootNode, { getChildren }).map((node) => node.name) | ||
// #=> ['a', 'b', 'c', 'd'] | ||
``` | ||
@@ -297,14 +283,23 @@ | ||
### `flat` | ||
### `visit` | ||
Returns an array containing the root node and all of its descendants. | ||
Visit each node in the tree, calling an optional `onEnter` and `onLeave` for each. | ||
This is analogous to `Array.prototype.flat` for flattening arrays. | ||
From `onEnter`: | ||
**Type**: `function flat<T>(node: T, options: BaseOptions<T>): T[]` | ||
- return nothing or `undefined` to continue | ||
- return `"skip"` to skip the children of that node and the subsequent `onLeave` | ||
- return `"stop"` to end traversal | ||
From `onLeave`: | ||
- return nothing or `undefined` to continue | ||
- return `"stop"` to end traversal | ||
**Type**: `function visit<T>(node: T, options: VisitOptions<T>): void` | ||
#### Example | ||
```js | ||
import { flat } from 'tree-visit' | ||
import { visit } from 'tree-visit' | ||
@@ -324,4 +319,9 @@ const getChildren = (node) => node.children || [] | ||
flat(rootNode, { getChildren }).map((node) => node.name) | ||
// #=> ['a', 'b', 'c', 'd'] | ||
visit(rootNode, { | ||
getChildren, | ||
onEnter: (node) => { | ||
console.log(node) | ||
}, | ||
}) | ||
// #=> a, b, c, d | ||
``` | ||
@@ -328,0 +328,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
47646