unist-util-visit-parents
Advanced tools
Comparing version 2.0.1 to 2.1.0
32
index.js
@@ -26,8 +26,9 @@ 'use strict' | ||
function one(node, index, parents) { | ||
var result | ||
var result = [] | ||
var subresult | ||
if (!test || is(test, node, index, parents[parents.length - 1] || null)) { | ||
result = visitor(node, parents) | ||
result = toResult(visitor(node, parents)) | ||
if (result === EXIT) { | ||
if (result[0] === EXIT) { | ||
return result | ||
@@ -37,4 +38,5 @@ } | ||
if (node.children && result !== SKIP) { | ||
return all(node.children, parents.concat(node)) === EXIT ? EXIT : result | ||
if (node.children && result[0] !== SKIP) { | ||
subresult = toResult(all(node.children, parents.concat(node))) | ||
return subresult[0] === EXIT ? subresult : result | ||
} | ||
@@ -50,16 +52,26 @@ | ||
var index = (reverse ? children.length : min) + step | ||
var child | ||
var result | ||
while (index > min && index < children.length) { | ||
child = children[index] | ||
result = child && one(child, index, parents) | ||
result = one(children[index], index, parents) | ||
if (result === EXIT) { | ||
if (result[0] === EXIT) { | ||
return result | ||
} | ||
index = typeof result === 'number' ? result : index + step | ||
index = typeof result[1] === 'number' ? result[1] : index + step | ||
} | ||
} | ||
} | ||
function toResult(value) { | ||
if (value !== null && typeof value === 'object' && 'length' in value) { | ||
return value | ||
} | ||
if (typeof value === 'number') { | ||
return [CONTINUE, value] | ||
} | ||
return [value] | ||
} |
{ | ||
"name": "unist-util-visit-parents", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"description": "Recursively walk over unist nodes, with ancestral information", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -83,4 +83,18 @@ # unist-util-visit-parents [![Build Status][build-badge]][build-page] [![Coverage Status][coverage-badge]][coverage-page] | ||
###### Returns | ||
##### Returns | ||
The return value can have the following forms: | ||
* [`index`][index] (`number`) — Treated as a tuple of `[CONTINUE, index]` | ||
* `action` (`*`) — Treated as a tuple of `[action]` | ||
* `tuple` (`Array.<*>`) — List with one or two values, the first an `action`, | ||
the second and `index`. | ||
Note that passing a tuple only makes sense if the `action` is `SKIP`. | ||
If the `action` is `EXIT`, that action can be returned. | ||
If the `action` is `CONTINUE`, `index` can be returned. | ||
###### `action` | ||
An action can have the following values: | ||
* `visit.EXIT` (`false`) — Stop traversing immediately | ||
@@ -90,11 +104,13 @@ * `visit.CONTINUE` (`true`) — Continue traversing as normal (same behaviour | ||
* `visit.SKIP` (`'skip'`) — Do not traverse this node’s children; continue | ||
with the next sibling | ||
* [`index`][index] (`number`) — Move to the sibling at `index` next (after | ||
`node` itself is completely traversed). | ||
Useful if mutating the tree, such as removing the node the visitor is | ||
currently on, or any of its previous siblings (or next siblings, in case of | ||
`reverse`) | ||
Results less than `0` or greater than or equal to `children.length` stop | ||
traversing the parent | ||
with the specified index | ||
###### `index` | ||
[`index`][index] (`number`) — Move to the sibling at `index` next (after `node` | ||
itself is completely traversed). | ||
Useful if mutating the tree, such as removing the node the visitor is currently | ||
on, or any of its previous siblings (or next siblings, in case of `reverse`) | ||
Results less than `0` or greater than or equal to `children.length` stop | ||
traversing the parent | ||
## Related | ||
@@ -101,0 +117,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
10743
56
187