Skip to content

Instantly share code, notes, and snippets.

@chee
Created June 2, 2018 17:33
Show Gist options
  • Select an option

  • Save chee/6d158dc47048d642a97cefb30585c2d9 to your computer and use it in GitHub Desktop.

Select an option

Save chee/6d158dc47048d642a97cefb30585c2d9 to your computer and use it in GitHub Desktop.
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