Created
June 2, 2018 17:16
-
-
Save chee/de533b3cfbb0eb473b9b19900add334e 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 => | |
| '(' + 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