-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor to move implementation to
lib/
- Loading branch information
Showing
3 changed files
with
61 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,11 @@ | ||
/** | ||
* @typedef {import('unist').Parent} UnistParent | ||
* @template {import('unist').Parent} Kind | ||
* @typedef {import('./lib/index.js').Visitor<Kind>} Visitor | ||
*/ | ||
|
||
/** | ||
* @template {UnistParent} Parent | ||
* @callback Visitor | ||
* Callback called for each `child` in `parent` later given to `visit`. | ||
* @param {Parent['children'][number]} node | ||
* Child of `parent`. | ||
* @param {number} index | ||
* Position of `child` in `parent`. | ||
* @param {Parent} parent | ||
* Parent node. | ||
* @returns {void} | ||
* Nothing. | ||
* @template {import('unist').Parent} Kind | ||
* @typedef {import('./lib/index.js').Visit<Kind>} Visit | ||
*/ | ||
|
||
/** | ||
* @template {UnistParent} Parent | ||
* @callback Visit | ||
* Function to call the bound `visitor` for each child in `parent`. | ||
* @param {Parent} node | ||
* Parent node. | ||
* @returns {void} | ||
* Nothing. | ||
*/ | ||
|
||
/** | ||
* Wrap `visitor` to be called for each child in the nodes later given to | ||
* `visit`. | ||
* | ||
* @template {UnistParent} Parent | ||
* @param {Visitor<Parent>} visitor | ||
* Callback called for each `child` in `parent` later given to `visit`. | ||
* @returns {Visit<Parent>} | ||
* Function to call the bound `visitor` for each child in `parent`. | ||
*/ | ||
export function visitChildren(visitor) { | ||
return visit | ||
|
||
/** @type {Visit<Parent>} */ | ||
function visit(parent) { | ||
const children = parent && parent.children | ||
let index = -1 | ||
|
||
if (!children) { | ||
throw new Error('Missing children in `parent` for `visit`') | ||
} | ||
|
||
while (++index in children) { | ||
visitor(children[index], index, parent) | ||
} | ||
} | ||
} | ||
export {visitChildren} from './lib/index.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* @typedef {import('unist').Parent} UnistParent | ||
*/ | ||
|
||
/** | ||
* @template {UnistParent} Parent | ||
* @callback Visitor | ||
* Callback called for each `child` in `parent` later given to `visit`. | ||
* @param {Parent['children'][number]} node | ||
* Child of `parent`. | ||
* @param {number} index | ||
* Position of `child` in `parent`. | ||
* @param {Parent} parent | ||
* Parent node. | ||
* @returns {void} | ||
* Nothing. | ||
*/ | ||
|
||
/** | ||
* @template {UnistParent} Parent | ||
* @callback Visit | ||
* Function to call the bound `visitor` for each child in `parent`. | ||
* @param {Parent} node | ||
* Parent node. | ||
* @returns {void} | ||
* Nothing. | ||
*/ | ||
|
||
/** | ||
* Wrap `visitor` to be called for each child in the nodes later given to | ||
* `visit`. | ||
* | ||
* @template {UnistParent} Parent | ||
* @param {Visitor<Parent>} visitor | ||
* Callback called for each `child` in `parent` later given to `visit`. | ||
* @returns {Visit<Parent>} | ||
* Function to call the bound `visitor` for each child in `parent`. | ||
*/ | ||
export function visitChildren(visitor) { | ||
return visit | ||
|
||
/** @type {Visit<Parent>} */ | ||
function visit(parent) { | ||
const children = parent && parent.children | ||
let index = -1 | ||
|
||
if (!children) { | ||
throw new Error('Missing children in `parent` for `visit`') | ||
} | ||
|
||
while (++index in children) { | ||
visitor(children[index], index, parent) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ | |
"main": "index.js", | ||
"types": "index.d.ts", | ||
"files": [ | ||
"lib/", | ||
"index.d.ts", | ||
"index.js" | ||
], | ||
|