Skip to content

Commit

Permalink
Use @types/nlcst
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Aug 27, 2021
1 parent 480ad4a commit 2b11e81
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 35 deletions.
67 changes: 32 additions & 35 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
/**
* @typedef {import('unist').Node} Node
* @typedef {import('unist').Parent} Parent
* @typedef {import('unist-util-visit').Visitor<Node>} Visitor
* @typedef {import('unist').Parent} UnistParent
* @typedef {import('nlcst').Root} Root
* @typedef {import('nlcst').Word} Word
* @typedef {import('nlcst').Content} Content
* @typedef {Root|Content} Node
* @typedef {Extract<Node, UnistParent>} Parent
* @typedef {import('nlcst-normalize').NormalizeOptions} NormalizeOptions
*
* @typedef {boolean} AllowApostrophes
*
* @typedef {NormalizeOptions & {allowLiterals?: boolean}} SearchOptions
*
* @typedef {Array.<string>} PhrasesList
* @typedef {Object.<string, unknown>} PhrasesMap
*
* @typedef {(nodes: Array.<Node>, index: number, parent: Parent, pattern: string) => void} Handler
* @typedef {(nodes: Content[], index: number, parent: Parent, pattern: string) => void} Handler
*/

import {visit} from 'unist-util-visit'
Expand Down Expand Up @@ -62,7 +64,31 @@ export function search(tree, phrases, handler, options) {
}

// Search the tree.
visit(tree, 'WordNode', visitor)
visit(tree, 'WordNode', (node, position, parent_) => {
const parent = /** @type {Parent} */ (parent_)

if (
!parent ||
position === null ||
(!config.allowLiterals && isLiteral(parent, position))
) {
return
}

const word = normalize(node, config)
const phrases = byWord['*'].concat(
own.call(byWord, word) ? byWord[word] : []
)
let index = -1

while (++index < phrases.length) {
const result = test(phrases[index], position, parent)

if (result) {
handler(result, position, parent, phrases[index])
}
}
})

/**
* Test a phrase.
Expand Down Expand Up @@ -106,35 +132,6 @@ export function search(tree, phrases, handler, options) {
return siblings.slice(start, position)
}

/**
* Visitor for `WordNode`s.
*
* @type {Visitor}
*/
function visitor(node, position, parent) {
if (
!parent ||
position === null ||
(!config.allowLiterals && isLiteral(parent, position))
) {
return
}

const word = normalize(node, config)
const phrases = byWord['*'].concat(
own.call(byWord, word) ? byWord[word] : []
)
let index = -1

while (++index < phrases.length) {
const result = test(phrases[index], position, parent)

if (result) {
handler(result, position, parent, phrases[index])
}
}
}

/**
* Handle a phrase.
*
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"index.js"
],
"dependencies": {
"@types/nlcst": "^1.0.0",
"@types/unist": "^2.0.0",
"nlcst-is-literal": "^2.0.0",
"nlcst-normalize": "^3.0.0",
Expand Down
5 changes: 5 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
/**
* @typedef {import('nlcst').Sentence} Sentence
*/

import test from 'tape'
import {search} from './index.js'

/** @type {Sentence} */
const tree = {
type: 'SentenceNode',
children: [
Expand Down

0 comments on commit 2b11e81

Please sign in to comment.