$ cnpm install nlcst-search
nlcst utility to search for patterns in a tree.
This package is ESM only:
Node 12+ is needed to use it and it must be imported instead of required.
npm:
npm install nlcst-search
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 following identifiers: search.
There is no default export.
search(tree, patterns, handler[, allowApostrophes|options])Search for patterns a tree.
Error — When not given node or patterns.
nodepatternsPatterns to search for (Array.<string> or Object).
If an Object, uses its keys as patterns.
Each pattern is a space-delimited list of words, where each word is
normalized to remove casing, apostrophes, and dashes.
Spaces in a pattern mean zero or more white space nodes in the tree.
Instead of a word, it’s also possible to use a wildcard symbol (*, an
asterisk), that matches any word in a pattern (alpha * charlie).
handlerHandler invoked when a match is found (Function).
allowApostrophesTreated as options.allowApostrophes.
options.allowApostrophesPassed to nlcst-normalize (boolean, default: false).
options.allowDashesPassed to nlcst-normalize (boolean, default: false).
options.allowLiteralsInclude literal phrases (boolean, default: false).
function handler(nodes, index, parent, pattern)Handler invoked when a match is found.
nodesList of siblings that match pattern (Array.<Node>).
indexIndex where the match starts in parent (number).
parentpatternThe matched pattern (string).
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 |