$ cnpm install nlcst-search
nlcst utility to search for phrases in a tree.
This utility can search for phrases (words and phrases) in trees.
This package is a tiny utility that helps when you’re searching for words and phrases.
This package is ESM only. In Node.js (version 16+), install with npm:
npm install nlcst-search
In Deno with esm.sh:
import {search} from 'https://esm.sh/nlcst-search@4'
In browsers with esm.sh:
<script type="module">
import {search} from 'https://esm.sh/nlcst-search@4?bundle'
</script>
import {search} from 'nlcst-search'
import {toString} from 'nlcst-to-string'
const tree = {
type: 'SentenceNode',
children: [
{
type: 'WordNode',
children: [
{type: 'TextNode', value: 'Don'},
{type: 'PunctuationNode', value: '’'},
{type: 'TextNode', value: 't'}
]
},
{type: 'WhiteSpaceNode', value: ' '},
{
type: 'WordNode',
children: [{type: 'TextNode', value: 'do'}]
},
{type: 'WhiteSpaceNode', value: ' '},
{
type: 'WordNode',
children: [
{type: 'TextNode', value: 'Block'},
{type: 'PunctuationNode', value: '-'},
{type: 'TextNode', value: 'level'}
]
}
]
}
search(tree, ['dont'], function(nodes) {
console.log(toString(nodes))
})
// `Don’t`
search(tree, ['do blocklevel'], function(nodes) {
console.log(toString(nodes))
})
// `do Block-level`
This package exports the identifier search.
There is no default export.
search(tree, phrases, handler[, options])Search for phrases in a tree.
Each phrase is a space-separated list of words, where each word will be
normalized to remove casing, apostrophes, and dashes.
Spaces in a pattern mean one or more whitespace nodes in the tree.
Instead of a word with letters, it’s also possible to use a wildcard symbol
(*, an asterisk) which will match any word in a pattern (alpha * charlie).
tree (Node)
— tree to searchphrases (Array<string>)
— phrases to search forhandler (Handler)
— handle a matchoptions (Options)
— configurationNothing (undefined).
HandlerHandle a match (TypeScript type).
nodes (Array<Node>)
— matchindex (number)
— index of first node of nodes in parentparent (Node)
— parent of nodesphrase (string)
— the phrase that matchedNothing (undefined).
OptionsConfiguration (TypeScript type).
allowApostrophes (boolean, default: false)
— passed to nlcst-normalizeallowDashes (boolean, default: false)
— passed to nlcst-normalizeallowLiterals (boolean, default: false)
— include literal phrasesThis package is fully typed with TypeScript.
It exports the additional types Handler and
Options.
Projects maintained by the unified collective are compatible with maintained versions of Node.js.
When we cut a new major release, we drop support for unmaintained versions of
Node.
This means we try to keep the current release line, nlcst-search@^4,
compatible with Node.js 16.
nlcst-normalize
— normalize a word for easier comparisonnlcst-is-literal
— check whether a node is meant literallySee contributing.md in syntax-tree/.github for
ways to get started.
See support.md for ways to get help.
This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.
Copyright 2013 - present © cnpmjs.org | Home |