-
Notifications
You must be signed in to change notification settings - Fork 59
/
checkName.ts
45 lines (38 loc) · 1.13 KB
/
checkName.ts
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
import { clean, isZalgo } from 'unzalgo'
import { Logger } from '@/types/Logger'
import { RuntimeConfigurationSchema } from '@/utils/RuntimeConfigurationSchema'
import { compiled } from './nameCleansing'
type Pattern = RuntimeConfigurationSchema['nameChecker']['patterns'][number]
export function checkNameAgainstPatterns(
name: string,
patterns: Pattern[],
log: Logger = console,
): boolean | undefined {
for (const pattern of patterns) {
try {
let regexp = compiled.get(pattern.regexp)
if (!regexp) {
regexp = new RegExp(pattern.regexp, 'i')
compiled.set(pattern.regexp, regexp)
}
if (regexp.test(name) || regexp.test(name.replaceAll(/\s+/g, ''))) {
return true
}
} catch (error) {
log.error(
`Unable to process pattern "${pattern}" against name "${name}"`,
error,
)
}
}
return false
}
export function checkZalgo(name: string) {
return isZalgo(name.trim())
}
export function checkDehoisted(name: string) {
return clean(name.trim()).startsWith('!')
}
export function checkExclamationMark(name: string) {
return clean(name.trim()) === '!'
}