Skip to content

Commit 34ef983

Browse files
committed
Refactor code-style
* Add more docs to JSDoc * Add support for `null` in input of API types * Refactor a bunch of things
1 parent 691f44d commit 34ef983

File tree

1 file changed

+57
-28
lines changed

1 file changed

+57
-28
lines changed

lib/index.js

+57-28
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,34 @@
11
/**
22
* @typedef {import('unist').Parent} UnistParent
33
* @typedef {import('nlcst').Root} Root
4-
* @typedef {import('nlcst').Word} Word
54
* @typedef {import('nlcst').Content} Content
6-
* @typedef {Root|Content} Node
5+
* @typedef {import('nlcst-normalize').Options} NormalizeOptions
6+
*/
7+
8+
/**
9+
* @typedef {Root | Content} Node
710
* @typedef {Extract<Node, UnistParent>} Parent
8-
* @typedef {import('nlcst-normalize').NormalizeOptions} NormalizeOptions
911
*
10-
* @typedef {boolean} AllowApostrophes
1112
* @typedef {NormalizeOptions & {allowLiterals?: boolean}} Options
1213
* Configuration (optional).
14+
*
1315
* @typedef {Array<string>} PhrasesList
1416
* List of phrases.
1517
* @typedef {Record<string, unknown>} PhrasesMap
1618
* Map where the keys are phrases.
17-
* @typedef {(nodes: Array<Content>, index: number, parent: Parent, pattern: string) => void} Handler
19+
*
20+
* @callback Handler
1821
* Function called when a pattern matches.
22+
* @param {Array<Content>} nodes
23+
* Match.
24+
* @param {number} index
25+
* Index of first node of `nodes` in `parent.
26+
* @param {Parent} parent
27+
* Parent of `nodes`.
28+
* @param {string} pattern
29+
* The pattern that matched.
30+
* @returns {void}
31+
* Nothing.
1932
*/
2033

2134
import {visit} from 'unist-util-visit'
@@ -25,25 +38,28 @@ import {isLiteral} from 'nlcst-is-literal'
2538
const own = {}.hasOwnProperty
2639

2740
/**
41+
* Search for patterns a tree.
42+
*
2843
* @param {Node} tree
29-
* @param {PhrasesList|PhrasesMap} phrases
44+
* Tree to search.
45+
* @param {PhrasesList | PhrasesMap} phrases
46+
* Patterns.
3047
* @param {Handler} handler
31-
* @param {AllowApostrophes|Options} [options=false]
48+
* Handler.
49+
* @param {boolean | Options} [options=false]
50+
* Configuration.
51+
* @returns {void}
52+
* Nothing.
3253
*/
54+
// To do: next major: remove boolean overload.
55+
// To do: next major: remove `PhrasesMap` support.
3356
export function search(tree, phrases, handler, options) {
34-
/** @type {Record<string, Array<string>>} */
35-
const byWord = {'*': []}
36-
let index = -1
37-
/** @type {string} */
38-
let key
39-
/** @type {Options} */
40-
let config
41-
42-
if (typeof options === 'boolean') {
43-
config = options ? {allowApostrophes: true} : {}
44-
} else {
45-
config = options || {}
46-
}
57+
const config =
58+
typeof options === 'boolean'
59+
? options
60+
? {allowApostrophes: true}
61+
: {}
62+
: options || {}
4763

4864
if (!tree || !tree.type) {
4965
throw new Error('Expected node')
@@ -53,11 +69,18 @@ export function search(tree, phrases, handler, options) {
5369
throw new TypeError('Expected object for phrases')
5470
}
5571

72+
/** @type {Record<string, Array<string>>} */
73+
const byWord = {'*': []}
74+
5675
if (Array.isArray(phrases)) {
76+
let index = -1
5777
while (++index < phrases.length) {
5878
handlePhrase(phrases[index])
5979
}
6080
} else {
81+
/** @type {string} */
82+
let key
83+
6184
for (key in phrases) {
6285
if (own.call(phrases, key)) {
6386
handlePhrase(key)
@@ -66,9 +89,7 @@ export function search(tree, phrases, handler, options) {
6689
}
6790

6891
// Search the tree.
69-
visit(tree, 'WordNode', (node, position, parent_) => {
70-
const parent = /** @type {Parent} */ (parent_)
71-
92+
visit(tree, 'WordNode', (node, position, parent) => {
7293
if (
7394
!parent ||
7495
position === null ||
@@ -78,9 +99,9 @@ export function search(tree, phrases, handler, options) {
7899
}
79100

80101
const word = normalize(node, config)
81-
const phrases = byWord['*'].concat(
82-
own.call(byWord, word) ? byWord[word] : []
83-
)
102+
const phrases = own.call(byWord, word)
103+
? [...byWord['*'], ...byWord[word]]
104+
: byWord['*']
84105
let index = -1
85106

86107
while (++index < phrases.length) {
@@ -93,11 +114,16 @@ export function search(tree, phrases, handler, options) {
93114
})
94115

95116
/**
96-
* Test a phrase.
117+
* Test a phrase (the first word already matched).
97118
*
98119
* @param {string} phrase
120+
* Normalized phrase.
99121
* @param {number} position
122+
* Index in `parent`.
100123
* @param {Parent} parent
124+
* Parent node.
125+
* @returns {Array<Content> | void}
126+
* Match, if found.
101127
*/
102128
function test(phrase, position, parent) {
103129
const siblings = parent.children
@@ -135,9 +161,12 @@ export function search(tree, phrases, handler, options) {
135161
}
136162

137163
/**
138-
* Handle a phrase.
164+
* Index a phrase.
139165
*
140166
* @param {string} phrase
167+
* Raw phrase.
168+
* @returns {void}
169+
* Nothing.
141170
*/
142171
function handlePhrase(phrase) {
143172
const firstWord = normalize(phrase.split(' ', 1)[0], config)

0 commit comments

Comments
 (0)