NLCST Phrase Search Utility
nlcst-search is a utility designed to locate specific word patterns and phrases within Natural Language Concrete Syntax Tree (NLCST) structures. It provides a programmatic way to search text that has been parsed into an NLCST tree, useful for linguistic analysis, linting, or text transformation tasks. The package currently sits at a stable version 4.0.0 and is actively maintained as part of the `unified` collective. Major versions typically signify significant shifts, such as environment compatibility updates (e.g., Node.js versions) or module system changes (e.g., ESM adoption). Key differentiators include its tight integration with the `nlcst` ecosystem, robust pattern matching with normalization options (casing, apostrophes, dashes), and support for wildcards. It ships with full TypeScript type definitions, ensuring a strong developer experience.
Common errors
-
ERR_REQUIRE_ESM
cause Attempting to `require()` an ESM-only package.fixUse ES module `import` syntax: `import { search } from 'nlcst-search'`. -
TypeError: search is not a function
cause Incorrect import syntax (e.g., trying to use `import search from 'nlcst-search'` for a named export) or package not resolving correctly due to `exports` field issues.fixEnsure you are using named import: `import { search } from 'nlcst-search'`. -
TypeError: options.allowApostrophes is not a boolean
cause Using the old overload syntax for `allowApostrophes` in `nlcst-search@4`.fixPass `allowApostrophes` within the options object: `search(tree, phrases, handler, { allowApostrophes: true })`.
Warnings
- breaking Version 4.0.0 and above require Node.js 16 or newer. Older Node.js versions are not supported.
- breaking Since version 3.0.0, `nlcst-search` is an ESM-only package. CommonJS `require()` syntax is no longer supported for direct import.
- breaking In version 4.0.0, the `allowApostrophes` option can no longer be passed as a separate overload argument. It must now be provided within the `options` object.
- breaking Version 4.0.0 removed the `PhrasesList` type; it is now simply `Array<string>`.
- breaking Version 4.0.0 removed support for passing an object as the `phrases` argument; it now strictly expects an `Array<string>`.
- breaking Version 2.0.0 introduced TypeScript types. Projects not expecting TypeScript or having conflicting type definitions might experience issues.
Install
-
npm install nlcst-search -
yarn add nlcst-search -
pnpm add nlcst-search
Imports
- search
const search = require('nlcst-search')import { search } from 'nlcst-search' - Handler
import type { Handler } from 'nlcst-search' - Options
import type { Options } from 'nlcst-search'
Quickstart
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'}
]
}
]
}
console.log('Searching for "dont":')
search(tree, ['dont'], function(nodes) {
console.log(`Found: "${toString(nodes)}"`);
});
console.log('\nSearching for "do blocklevel" with a wildcard:')
search(tree, ['do *level'], function(nodes) {
console.log(`Found: "${toString(nodes)}"`);
});