JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 53
  • Score
    100M100P100Q62215F
  • License ISC

Create a regular expression from trie like object

Package Exports

  • trie-regex

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 (trie-regex) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

trie-regex

Create a regular expression from trie like object

install

npm install trie-regex

demo

import trie = require('trie-prefix-tree');
import trieToRegExp from 'trie-regex';

let arr = ['trea', 'tr2a', 'trie', '1', 'foobar', 'foobaz', 'foozap', 'fooza', '$'];

let t1 = trie(arr).tree();

console.log(trieToRegExp(t1, {
    isEndpoint(value, key, trie)
    {
        return (key === '$') && (value === 1);
    }
}));
// => /(?:foo(?:ba[rz]|zap?)|tr(?:2a|ea|ie)|\$|1)/u

let t2 = {
    "1": {
        "$": 1
    },
    "t": {
        "r": {
            "2": {
                "a": {
                    "$": 1
                }
            },
            "e": {
                "a": {
                    "$": 1
                }
            },
            "i": {
                "e": {
                    "$": 1
                }
            }
        }
    },
    "f": {
        "o": {
            "o": {
                "b": {
                    "a": {
                        "r": {
                            "$": 1
                        },
                        "z": {
                            "$": 1
                        }
                    }
                },
                "z": {
                    "a": {
                        "p": {
                            "$": 1
                        },
                        "$": 1
                    }
                }
            }
        }
    },
    "$": {
        "$": 1
    }
};

console.log(trieToRegExp(t2, {
    isEndpoint(value, key, trie)
    {
        return (key === '$') && (value === 1);
    }
}));
// => /(?:foo(?:ba[rz]|zap?)|tr(?:2a|ea|ie)|\$|1)/u