Giter VIP home page Giter VIP logo

nlcst-search's Introduction

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

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

Parameters
  • tree (Node) — tree to search
  • phrases (Array<string>) — phrases to search for
  • handler (Handler) — handle a match
  • options (Options) — configuration
Returns

Nothing (undefined).

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

Options

Configuration (TypeScript type).

Fields
  • allowApostrophes (boolean, default: false) — passed to nlcst-normalize
  • allowDashes (boolean, default: false) — passed to nlcst-normalize
  • allowLiterals (boolean, default: false) — include literal phrases

Types

This package is fully typed with TypeScript. It exports the additional types Handler and 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

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

nlcst-search's People

Contributors

christianmurphy avatar nwtn avatar tmcw avatar wooorm avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

nlcst-search's Issues

Not bower installable?

Noticed that unist-util-visit was coming back with ENOTFOUND on bower install of nlcst-search:

bower install https://github.com/wooorm/nlcst-search.git

bower nlcst-search#*            cached https://github.com/wooorm/nlcst-search.git#1.1.1
bower nlcst-search#*          validate 1.1.1 against https://github.com/wooorm/nlcst-search.git#*
bower                        ENOTFOUND Package wooorm/unist-util-visit=wooorm/unist-util-visit not found

[Question] - How to use search with allowDashes

Sorry for a noob question but how can I enable dashes in the search function?
I have a function search(node, patterns, handler) how to use allowDashes with it?
I tried couple of combinations but couldn't make it work. Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.