Created
June 2, 2018 17:33
-
-
Save chee/6d158dc47048d642a97cefb30585c2d9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| data = require('./data.json') | |
| escape = char => `\\${char}` | |
| special = new RegExp( | |
| '[' + | |
| '( ) { } [ ] | \\ ^ $ * + ? .' | |
| .split`` | |
| .map(escape) | |
| .join('|') + | |
| ']', 'g' | |
| ) | |
| escapeRegExp = string => string.replace(special, '\\$&') | |
| build = data => | |
| new RegExp('(' + data.reduce((items, cookie) => { | |
| const { | |
| pattern, | |
| name | |
| } = cookie | |
| if (pattern) { | |
| return items.concat(pattern) | |
| } | |
| if (name) { | |
| return items.concat(`^${escapeRegExp(name)}$`) | |
| } | |
| return items | |
| }, []).join('|') + ')') | |
| console.log(build(data)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment