-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake-tag.js
32 lines (25 loc) · 849 Bytes
/
make-tag.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import mem from "mem"
let keepEscaped = str => str.startsWith("\\") ? str.slice(1) : ""
let dropComments = str => str.replace(/\\?#.*$|\s+/gm, keepEscaped)
let processExt = mem(raw => raw.map(dropComments))
let escapeMeta = str => str.replace(/[|{}[\]()?+*.\\$^]/g, "\\$&")
let toString = val =>
{
if (val == null) return ""
if (val[Symbol.match]) return val.source
return typeof val != "string"
&& typeof val[Symbol.iterator] == "function"
? Array.from(val, toString).join("|")
: escapeMeta(`${val}`)
}
export default mem(flags =>
{
let ext = flags.includes("x")
if (ext) flags = flags.replace("x", "")
return ({ raw }, ...vals) =>
{
if (ext) raw = processExt(raw)
let pattern = String.raw({ raw }, ...vals.map(toString))
return new RegExp(pattern, flags)
}
})