JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 25801
  • Score
    100M100P100Q151189F
  • License MIT

Search for patterns in an NLCST tree

Package Exports

  • nlcst-search

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (nlcst-search) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

nlcst-search Build Status Coverage Status

Search for patterns in an NLCST tree.

Installation

npm:

npm install nlcst-search

nlcst-search is also available for duo, and as an AMD, CommonJS, and globals module, uncompressed and compressed.

Usage

var search = require('nlcst-search');
var toString = require('nlcst-to-string');

var 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

search(node, patterns, handler)

Search for patterns in an NLCST tree.

Note that the algorithm ignores literal words.

Parameters

  • node (Node) — Tree to search in;

  • patterns (Array.<string> or Object) — Patterns to search for. If an Object, uses its keys. 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.

  • handler (Function) — Patterns to search for. If an Object, uses its keys. Each pattern is a space-delimited list of words, where each word is normalized to remove casing, apostrophes, and dashes.

Throws: Error — When not given node or patterns.

function handler(nodes, index, parent, pattern)

Handler invoked when a match is found.

Parameters

  • nodes (Array.<Node>) — List of siblings which match pattern;

  • index (number) — Position at which the match starts in parent;

  • parent (Node) — Parent node of nodes;

  • pattern (string) — The matched pattern.

License

MIT © Titus Wormer