Repository navigation
Expand file tree
/
Copy pathpatterns.js
More file actions
155 lines (142 loc) · 3.86 KB
/
Copy pathpatterns.js
File metadata and controls
155 lines (142 loc) · 3.86 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
function sanitizeCategory(input) {
const raw = String(input ?? "").trim()
if (!raw) return "TEXT"
const upper = raw.toUpperCase()
const safe = upper.replace(/[^A-Z0-9_]/g, "_").replace(/_+/g, "_")
if (!safe) return "TEXT"
return safe
}
/**
* Lightweight compatibility for Go-style `(?i)` / `(?m)` prefixes (only leading flags).
* @param {string} pattern
* @param {string} flags
*/
function peelInlineFlags(pattern, flags) {
let p = String(pattern ?? "")
let f = String(flags ?? "")
for (;;) {
if (p.startsWith("(?i)")) {
p = p.slice(4)
if (!f.includes("i")) f += "i"
continue
}
if (p.startsWith("(?m)")) {
p = p.slice(4)
if (!f.includes("m")) f += "m"
continue
}
break
}
return { pattern: p, flags: f }
}
/**
* Builtin rules ported from VibeGuard (JS-compatible). Low config effort, broad coverage.
*/
const BUILTIN = new Map([
[
"email",
{
pattern: String.raw`[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}`,
flags: "i",
category: "EMAIL",
},
],
[
"china_phone",
{
// Matches the phone number body directly (lookaround replaces Go capture-group boundary style)
pattern: String.raw`(?<!\d)1[3-9]\d{9}(?!\d)`,
flags: "",
category: "CHINA_PHONE",
},
],
[
"china_id",
{
pattern: String.raw`(?<!\d)\d{17}[\dXx](?!\d)`,
flags: "",
category: "CHINA_ID",
},
],
[
"uuid",
{
pattern: String.raw`[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}`,
flags: "",
category: "UUID",
},
],
[
"ipv4",
{
// Does not validate 0-255 per octet; aims to cover common cases
pattern: String.raw`(?:\d{1,3}\.){3}\d{1,3}`,
flags: "",
category: "IPV4",
},
],
[
"mac",
{
pattern: String.raw`(?:[0-9a-f]{2}:){5}[0-9a-f]{2}`,
flags: "i",
category: "MAC",
},
],
])
function compileRegexRule(pattern, flags, category, errors) {
const peeled = peelInlineFlags(pattern, flags)
const finalFlags = peeled.flags.includes("g") ? peeled.flags : `${peeled.flags}g`
try {
return { regex: new RegExp(peeled.pattern, finalFlags), category }
} catch (error) {
errors.push({
pattern: peeled.pattern,
flags: finalFlags,
message: String(error?.message ?? error),
})
return null
}
}
export function buildPatternSet(patterns) {
const raw = patterns && typeof patterns === "object" ? patterns : {}
const keywords = Array.isArray(raw.keywords) ? raw.keywords : []
const regex = Array.isArray(raw.regex) ? raw.regex : []
const builtin = Array.isArray(raw.builtin) ? raw.builtin : []
const exclude = Array.isArray(raw.exclude) ? raw.exclude : []
const keywordRules = keywords
.map((x) => {
if (!x || typeof x !== "object") return null
const value = String(x.value ?? "").trim()
if (!value) return null
const category = sanitizeCategory(x.category)
return { value, category }
})
.filter(Boolean)
const regexRules = []
const errors = []
for (const x of regex) {
if (!x || typeof x !== "object") continue
const pattern = String(x.pattern ?? "").trim()
if (!pattern) continue
const category = sanitizeCategory(x.category)
const flags = typeof x.flags === "string" ? x.flags : ""
const rule = compileRegexRule(pattern, flags, category, errors)
if (rule) regexRules.push(rule)
}
for (const name of builtin) {
const key = String(name ?? "").trim()
if (!key) continue
const rule = BUILTIN.get(key)
if (!rule) continue
const compiled = compileRegexRule(rule.pattern, rule.flags, rule.category, errors)
if (compiled) regexRules.push(compiled)
}
const excludeSet = new Set(exclude.map((x) => String(x ?? "")))
return {
keywords: keywordRules,
regex: regexRules,
exclude: excludeSet,
errors,
}
}