-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathutils.ts
146 lines (131 loc) · 3.81 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
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
import { LangConfig } from './extension';
export interface Options {
shouldRemoveDuplicates: boolean;
shouldPrependCustomClasses: boolean;
customTailwindPrefix: string;
separator?: RegExp;
replacement?: string;
}
/**
* Sorts a string of CSS classes according to a predefined order.
* @param classString The string to sort
* @param sortOrder The default order to sort the array at
*
* @returns The sorted string
*/
export const sortClassString = (
classString: string,
sortOrder: string[],
options: Options
): string => {
let classArray = classString.split(options.separator || /\s+/g);
if (options.shouldRemoveDuplicates) {
classArray = removeDuplicates(classArray);
}
// prepend custom tailwind prefix to all tailwind sortOrder-classes
const sortOrderClone = [...sortOrder];
if (options.customTailwindPrefix.length > 0) {
for (var i = 0; i < sortOrderClone.length; i++) {
sortOrderClone[i] = options.customTailwindPrefix + sortOrderClone[i];
}
}
classArray = sortClassArray(
classArray,
sortOrderClone,
options.shouldPrependCustomClasses
);
return classArray.join(options.replacement || ' ').trim();
};
const sortClassArray = (
classArray: string[],
sortOrder: string[],
shouldPrependCustomClasses: boolean
): string[] => [
...classArray.filter(
(el) => shouldPrependCustomClasses && sortOrder.indexOf(el) === -1
), // append the classes that were not in the sort order if configured this way
...classArray
.filter((el) => sortOrder.indexOf(el) !== -1) // take the classes that are in the sort order
.sort((a, b) => sortOrder.indexOf(a) - sortOrder.indexOf(b)), // and sort them
...classArray.filter(
(el) => !shouldPrependCustomClasses && sortOrder.indexOf(el) === -1
), // prepend the classes that were not in the sort order if configured this way
];
const removeDuplicates = (classArray: string[]): string[] => [
...new Set(classArray),
];
function isArrayOfStrings(value: unknown): value is string[] {
return (
Array.isArray(value) && value.every((item) => typeof item === 'string')
);
}
export type Matcher = {
regex: RegExp[];
separator?: RegExp;
replacement?: string;
};
function buildMatcher(value: LangConfig): Matcher {
if (typeof value === 'string') {
return {
regex: [new RegExp(value, 'gi')],
};
} else if (isArrayOfStrings(value)) {
return {
regex: value.map((v) => new RegExp(v, 'gi')),
};
} else if (value == undefined) {
return {
regex: [],
};
} else {
return {
regex:
typeof value.regex === 'string'
? [new RegExp(value.regex, 'gi')]
: isArrayOfStrings(value.regex)
? value.regex.map((v) => new RegExp(v, 'gi'))
: [],
separator:
typeof value.separator === 'string'
? new RegExp(value.separator, 'g')
: undefined,
replacement: value.replacement || value.separator,
};
}
}
export function buildMatchers(value: LangConfig | LangConfig[]): Matcher[] {
if (value == undefined) {
return [];
} else if (Array.isArray(value)) {
if (!value.length) {
return [];
} else if (!isArrayOfStrings(value)) {
return value.map((v) => buildMatcher(v));
}
}
return [buildMatcher(value)];
}
export function getTextMatch(
regexes: RegExp[],
text: string,
callback: (text: string, startPosition: number) => void,
startPosition: number = 0
): void {
if (regexes.length >= 1) {
let wrapper: RegExpExecArray | null;
while ((wrapper = regexes[0].exec(text)) !== null) {
const wrapperMatch = wrapper[0];
const valueMatchIndex = wrapper.findIndex(
(match, idx) => idx !== 0 && match
);
const valueMatch = wrapper[valueMatchIndex];
const newStartPosition =
startPosition + wrapper.index + wrapperMatch.lastIndexOf(valueMatch);
if (regexes.length === 1) {
callback(valueMatch, newStartPosition);
} else {
getTextMatch(regexes.slice(1), valueMatch, callback, newStartPosition);
}
}
}
}