-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
99 lines (80 loc) · 1.7 KB
/
index.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/**
* Created by user on 2018/4/24/024.
*/
import * as XRegExp from 'xregexp';
import cjkConv from 'cjk-conv';
export type IOptions = {
scope?: string,
flags?: string,
};
export const _CACHE = new Set();
export const X_REGEX_DATA = 'xregexp';
export function addSupportToXRegExp<T extends typeof XRegExp>(xr: T = XRegExp,
options: IOptions = {}
): T & typeof XRegExp
{
if (xr === null)
{
xr = XRegExp;
}
if (isInstalled(xr))
{
console.warn(`this plugin already installed.`);
}
else
{
xr.addToken(/[\u4E00-\u9FFFの]/, (match, scope) =>
{
//console.log(match, scope);
let a = cjkConv.zhTable.auto(match[0]);
// console.log(match, scope, a);
if (a.length)
{
return scope == 'class' ? a.join('') : '[' + a.join('') + ']';
}
else if (1)
{
return match[0];
}
throw new SyntaxError(`Invalid escape ${match[0]}`);
},
{
scope: options.scope || 'default',
//leadChar: '\\',
//reparse: true,
// @ts-ignore
flag: options.flags || undefined,
}
);
_CACHE.add(xr);
}
return xr;
}
export function isXRegExp<T extends RegExp & {
[X_REGEX_DATA],
}>(xr: T)
{
return xr[X_REGEX_DATA] ? true : false;
}
export function createXRegExp(pattern: string | RegExp | XRegExp, flags?: string, xr: XRegExp = XRegExp)
{
if (typeof pattern == 'string')
{
return xr(pattern, flags);
}
else if (isXRegExp(pattern))
{
return xr(pattern);
}
return xr(pattern.source, typeof flags == 'string' ? flags : pattern.flags);
}
export const install = addSupportToXRegExp;
export function isInstalled<T extends typeof XRegExp>(xr: T = XRegExp)
{
if (xr === null)
{
xr = XRegExp;
}
return _CACHE.has(xr);
}
export default addSupportToXRegExp;