Natty Precise Maverick

    trie-regex
    TypeScript icon, indicating that this package has built-in type declarations

    1.1.5 • Public • Published

    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

    Install

    npm i trie-regex

    DownloadsWeekly Downloads

    48

    Version

    1.1.5

    License

    ISC

    Unpacked Size

    20.1 kB

    Total Files

    12

    Last publish

    Collaborators

    • bluelovers