Noiseless Praying Mantis

    nlcst-search
    TypeScript icon, indicating that this package has built-in type declarations

    3.1.1 • Public • Published

    nlcst-search

    Build Coverage Downloads Size Sponsors Backers Chat

    nlcst utility to search for phrases in a tree.

    Contents

    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 only. In Node.js (version 14.14+ and 16.0+), install with npm:

    npm install nlcst-search

    In Deno with esm.sh:

    import {search} from 'https://esm.sh/nlcst-search@3'

    In browsers with esm.sh:

    <script type="module">
      import {search} from 'https://esm.sh/nlcst-search@3?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 search. There is no default export.

    search(tree, phrases, handler[, allowApostrophes|options])

    Search for phrases in a tree.

    Parameters
    • tree (Node) — tree to search
    • phrases (PhrasesList or PhrasesMap) — phrases to search for
    • handler (Handler) — handle a match
    • allowApostrophes (boolean) — shortcut for options of {allowApostrophes: boolean}
    • options (Options) — configuration
    Returns

    Nothing (void).

    Handler

    Handle a match (TypeScript type).

    Parameters
    • nodes (Array<Node>) — match
    • index (number) — index of first node of nodes in parent
    • parent (Node) — parent of nodes
    • phrase (string) — the phrase that matched
    Returns

    Nothing (void).

    Options

    Configuration (TypeScript type).

    Fields
    allowApostrophes

    Passed to nlcst-normalize (boolean, default: false).

    allowDashes

    Passed to nlcst-normalize (boolean, default: false).

    allowLiterals

    Include literal phrases (boolean, default: false).

    PhrasesList

    List of phrases (TypeScript type).

    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).

    Type
    type PhrasesList = Array<string>

    PhrasesMap

    Map of phrases (TypeScript type).

    Type
    type PhrasesMap = Record<string, unknown>

    Types

    This package is fully typed with TypeScript. It exports the additional types Handler, Options, PhrasesList, and PhrasesMap.

    Compatibility

    Projects maintained by the unified collective are compatible with all maintained versions of Node.js. As of now, that is Node.js 14.14+ and 16.0+. Our projects sometimes work with older versions, but this is not guaranteed.

    Related

    Contribute

    See 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.

    License

    MIT © Titus Wormer

    Install

    npm i nlcst-search

    DownloadsWeekly Downloads

    26,369

    Version

    3.1.1

    License

    MIT

    Unpacked Size

    17.4 kB

    Total Files

    7

    Last publish

    Collaborators

    • wooorm
    • kmck