-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
83 lines (74 loc) · 1.8 KB
/
utils.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
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
/* eslint-disable no-control-regex */
import levenshtein from 'js-levenshtein'
/**
* Filter common tag escapes
* @internal
* @param ctx - The string to filter
*/
export const filterEscapes = (ctx: string): string => {
return ctx
.replace(/\\{/g, '\u0012')
.replace(/\\\|/g, '\u0013')
.replace(/\\}/g, '\u0014')
}
/**
* Restore common tag escapes
* @internal
* @param ctx - The string to restore
*/
export const defilterEscapes = (ctx: string): string => {
return ctx
.replace(/\u0012/gu, '\\{')
.replace(/\u0013/gu, '\\|')
.replace(/\u0014/gu, '\\}')
}
/**
* Filter all tag braces
* @internal
* @param ctx - The string to filter
*/
export const filterAll = (ctx: string): string => {
return filterEscapes(ctx)
.replace(/{/g, '\u0015')
.replace(/}/g, '\u0016')
}
/**
* Restore all tag braces
* @internal
* @param ctx - The string to restore
*/
export const defilterAll = (ctx: string): string => {
return defilterEscapes(ctx)
.replace(/\u0015/gu, '{')
.replace(/\u0016/gu, '}')
}
/**
* @internal
*/
export const safeCompare = (item1: any, conditional: any, item2: any): boolean => {
switch (conditional) {
// Built-in
case '>': return item1 > item2
case '<': return item1 < item2
case '=': return item1 === item2
// Custom
case '~': return levenshtein(item1.toLowerCase(), item2.toLowerCase()) <= 2
case '?': return new RegExp(item2, 'g').exec(item1) !== null
default: return false
}
}
/**
* @internal
*/
export const variableStore = new Map()
/**
* @internal
*/
export const clearVariables = (): void => variableStore.clear()
/**
* @internal
*/
export const creationTime = (discordSnowflake: string): string => {
const formatted = (+discordSnowflake / 4194304) + 1420070400000
return new Date(formatted).toUTCString()
}