Skip to content

Commit

Permalink
Refactor to move implementation to lib/
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jan 23, 2023
1 parent 60a8424 commit 17e472b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 49 deletions.
54 changes: 5 additions & 49 deletions index.js
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'
55 changes: 55 additions & 0 deletions lib/index.js
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)
}
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"main": "index.js",
"types": "index.d.ts",
"files": [
"lib/",
"index.d.ts",
"index.js"
],
Expand Down

0 comments on commit 17e472b

Please sign in to comment.