nlcst-search

nlcst utility to search for patterns in a tree

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
nlcst-search
1604.0.09 months ago8 years agoMinified + gzip package size for nlcst-search in KB

Readme

nlcst-search
!Buildbuild-badgebuild !Coveragecoverage-badgecoverage !Downloadsdownloads-badgedownloads !Sizesize-badgesize !Sponsorssponsors-badgecollective !Backersbackers-badgecollective !Chatchat-badgechat
nlcst utility to search for phrases in a tree.

Contents

*   [`search(tree, phrases, handler[, options])`](#searchtree-phrases-handler-options)
*   [`Handler`](#handler)
*   [`Options`](#options)

What is this?

This utility can search for phrases (words and phrases) in trees.

When should I use this?

This package is a tiny utility that helps when you’re searching for words and phrases.

Install

This package is ESM onlyesm. In Node.js (version 16+), install with npm:
npm install nlcst-search

In Deno with esm.shesmsh:
import {search} from 'https://esm.sh/nlcst-search@4'

In browsers with esm.shesmsh:
<script type="module">
  import {search} from 'https://esm.sh/nlcst-search@4?bundle'
</script>

Use

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`

API

This package exports the identifier searchapi-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 normalizednlcst-normalize 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).
Parameters
— tree to search
  • phrases (Array<string>)
— phrases to search for
— handle a match
— configuration
Returns
Nothing (undefined).

Handler

Handle a match (TypeScript type).
Parameters
— match
  • index (number)
— index of first node of `nodes` in `parent`
— parent of `nodes`
  • phrase (string)
— the phrase that matched
Returns
Nothing (undefined).

Options

Configuration (TypeScript type).
Fields
  • allowApostrophes (boolean, default: false)
— passed to [`nlcst-normalize`][nlcst-normalize]
  • allowDashes (boolean, default: false)
— passed to [`nlcst-normalize`][nlcst-normalize]
  • allowLiterals (boolean, default: false)
— include [literal][] phrases

Types

This package is fully typed with TypeScript. It exports the additional types Handlerapi-handler and Optionsapi-options.

Compatibility

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.

Related

— normalize a word for easier comparison
— check whether a node is meant literally

Contribute

See contributing.mdcontributing in syntax-tree/.githubhealth for ways to get started. See support.mdsupport for ways to get help.
This project has a code of conductcoc. By interacting with this repository, organization, or community you agree to abide by its terms.

License

MITlicense © Titus Wormerauthor