From 2b11e817daff896dc0732b70ac784173bbb7c9af Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Fri, 27 Aug 2021 11:08:59 +0200 Subject: [PATCH] Use `@types/nlcst` --- index.js | 67 +++++++++++++++++++++++++--------------------------- package.json | 1 + test.js | 5 ++++ 3 files changed, 38 insertions(+), 35 deletions(-) diff --git a/index.js b/index.js index d36728f..56384e7 100644 --- a/index.js +++ b/index.js @@ -1,17 +1,19 @@ /** - * @typedef {import('unist').Node} Node - * @typedef {import('unist').Parent} Parent - * @typedef {import('unist-util-visit').Visitor} 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} Parent * @typedef {import('nlcst-normalize').NormalizeOptions} NormalizeOptions * * @typedef {boolean} AllowApostrophes - * * @typedef {NormalizeOptions & {allowLiterals?: boolean}} SearchOptions * * @typedef {Array.} PhrasesList * @typedef {Object.} PhrasesMap * - * @typedef {(nodes: Array., 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' @@ -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. @@ -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. * diff --git a/package.json b/package.json index 9653537..cb48dfb 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test.js b/test.js index aa4380b..5b0445b 100644 --- a/test.js +++ b/test.js @@ -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: [