Skip to content

Commit

Permalink
Refactor code-style
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jul 3, 2023
1 parent 1bd27b2 commit 8971e05
Show file tree
Hide file tree
Showing 2 changed files with 215 additions and 193 deletions.
85 changes: 43 additions & 42 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,55 +9,56 @@ import {convert} from 'unist-util-is'
/**
* Find the first node in `parent` after another `node` or after an index,
* that passes `test`.
* @param parent
*
* @template {Node} Kind
* Node type.
*
* @overload
* @param {Parent} parent
* @param {Node | number} index
* @param {import('unist-util-is').PredicateTest<Kind>} test
* @returns {Kind | null}
*
* @overload
* @param {Parent} parent
* @param {Node | number} index
* @param {Test} [test]
* @returns {Node | null}
*
* @param {Parent} parent
* Parent node.
* @param index
* @param {Node | number} index
* Child of `parent` or it’s index.
* @param test
* @param {Test} [test]
* `unist-util-is`-compatible test.
* @returns
* @returns {Node | null}
* Child of `parent` or `null`.
*/
export const findAfter =
/**
* @type {(
* (<T extends Node>(node: Parent, index: Node | number, test: import('unist-util-is').PredicateTest<T>) => T | null) &
* ((node: Parent, index: Node | number, test?: Test) => Node | null)
* )}
*/
(
/**
* @param {Parent} parent
* @param {Node | number} index
* @param {Test} [test]
* @returns {Node | null}
*/
function (parent, index, test) {
const is = convert(test)

if (!parent || !parent.type || !parent.children) {
throw new Error('Expected parent node')
}
// To do: next major: `undefined`.
export function findAfter(parent, index, test) {
const is = convert(test)

if (typeof index === 'number') {
if (index < 0 || index === Number.POSITIVE_INFINITY) {
throw new Error('Expected positive finite number as index')
}
} else {
index = parent.children.indexOf(index)
if (!parent || !parent.type || !parent.children) {
throw new Error('Expected parent node')
}

if (index < 0) {
throw new Error('Expected child node or index')
}
}
if (typeof index === 'number') {
if (index < 0 || index === Number.POSITIVE_INFINITY) {
throw new Error('Expected positive finite number as index')
}
} else {
index = parent.children.indexOf(index)

while (++index < parent.children.length) {
if (is(parent.children[index], index, parent)) {
return parent.children[index]
}
}
if (index < 0) {
throw new Error('Expected child node or index')
}
}

return null
while (++index < parent.children.length) {
if (is(parent.children[index], index, parent)) {
return parent.children[index]
}
)
}

return null
}
Loading

0 comments on commit 8971e05

Please sign in to comment.