WXL
3 天以前 3bd962a6d7f61239c020e2dbbeb7341e5b842dd1
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
    MIT License http://www.opensource.org/licenses/mit-license.php
    Author Tobias Koppers @sokra
*/
 
"use strict";
 
/**
 * Returns quoted meta.
 * @param {string} str string
 * @returns {string} quoted meta
 */
const quoteMeta = (str) => str.replace(/[-[\]\\/{}()*+?.^$|]/g, "\\$&");
 
/**
 * Quote meta in char class.
 * @param {string} char character to escape for use in character class
 * @returns {string} escaped character
 */
const quoteMetaInCharClass = (char) => {
    // In character class, only these need escaping: ] \ ^ -
    if (char === "]" || char === "\\" || char === "^" || char === "-") {
        return `\\${char}`;
    }
    return char;
};
 
/**
 * Converts an array of single characters into an optimized character class string
 * using ranges where possible. E.g., ["1","2","3","4","a"] => "1-4a"
 * @param {string[]} chars array of single characters (should be sorted)
 * @returns {string} optimized character class content (without the brackets)
 */
const charsToCharClassContent = (chars) => {
    if (chars.length === 0) return "";
    if (chars.length === 1) return quoteMetaInCharClass(chars[0]);
 
    // Sort by char code
    const sorted = [...chars].sort((a, b) => a.charCodeAt(0) - b.charCodeAt(0));
 
    /** @type {string[]} */
    const parts = [];
    let rangeStart = sorted[0];
    let rangeEnd = sorted[0];
 
    for (let i = 1; i < sorted.length; i++) {
        const char = sorted[i];
        const prevCode = rangeEnd.charCodeAt(0);
        const currCode = char.charCodeAt(0);
 
        if (currCode === prevCode + 1) {
            // Extend the range
            rangeEnd = char;
        } else {
            // Flush the current range
            parts.push(formatRange(rangeStart, rangeEnd));
            rangeStart = char;
            rangeEnd = char;
        }
    }
    // Flush the last range
    parts.push(formatRange(rangeStart, rangeEnd));
 
    return parts.join("");
};
 
/**
 * Formats a range of characters for use in a character class
 * @param {string} start start character
 * @param {string} end end character
 * @returns {string} formatted range
 */
const formatRange = (start, end) => {
    const startCode = start.charCodeAt(0);
    const endCode = end.charCodeAt(0);
    const length = endCode - startCode + 1;
 
    if (length === 1) {
        return quoteMetaInCharClass(start);
    }
    if (length === 2) {
        // For 2 chars, just list them (e.g., "ab" instead of "a-b")
        return quoteMetaInCharClass(start) + quoteMetaInCharClass(end);
    }
    // For 3+ chars, use range notation
    return `${quoteMetaInCharClass(start)}-${quoteMetaInCharClass(end)}`;
};
 
/**
 * Returns string.
 * @param {string} str string
 * @returns {string} string
 */
const toSimpleString = (str) => {
    if (`${Number(str)}` === str) {
        return str;
    }
    return JSON.stringify(str);
};
 
/**
 * Compile boolean matcher.
 * @param {Record<string | number, boolean>} map value map
 * @returns {boolean | ((value: string) => string)} true/false, when unconditionally true/false, or a template function to determine the value at runtime
 */
const compileBooleanMatcher = (map) => {
    const positiveItems = Object.keys(map).filter((i) => map[i]);
    const negativeItems = Object.keys(map).filter((i) => !map[i]);
    if (positiveItems.length === 0) return false;
    if (negativeItems.length === 0) return true;
    return compileBooleanMatcherFromLists(positiveItems, negativeItems);
};
 
/**
 * Compile boolean matcher from lists.
 * @param {string[]} positiveItems positive items
 * @param {string[]} negativeItems negative items
 * @returns {(value: string) => string} a template function to determine the value at runtime
 */
const compileBooleanMatcherFromLists = (positiveItems, negativeItems) => {
    if (positiveItems.length === 0) return () => "false";
    if (negativeItems.length === 0) return () => "true";
    if (positiveItems.length === 1) {
        return (value) => `${toSimpleString(positiveItems[0])} == ${value}`;
    }
    if (negativeItems.length === 1) {
        return (value) => `${toSimpleString(negativeItems[0])} != ${value}`;
    }
    const positiveRegexp = itemsToRegexp(positiveItems);
    const negativeRegexp = itemsToRegexp(negativeItems);
    if (positiveRegexp.length <= negativeRegexp.length) {
        return (value) => `/^${positiveRegexp}$/.test(${value})`;
    }
    return (value) => `!/^${negativeRegexp}$/.test(${value})`;
};
 
/** @typedef {string[][]} ListOfCommonItems */
 
/**
 * Returns list of common items.
 * @param {Set<string>} itemsSet items set
 * @param {(str: string) => string | false} getKey get key function
 * @param {(str: string[]) => boolean} condition condition
 * @returns {ListOfCommonItems} list of common items
 */
const popCommonItems = (itemsSet, getKey, condition) => {
    /** @type {Map<string, string[]>} */
    const map = new Map();
    for (const item of itemsSet) {
        const key = getKey(item);
        if (key) {
            let list = map.get(key);
            if (list === undefined) {
                /** @type {string[]} */
                list = [];
                map.set(key, list);
            }
            list.push(item);
        }
    }
    /** @type {ListOfCommonItems} */
    const result = [];
    for (const list of map.values()) {
        if (condition(list)) {
            for (const item of list) {
                itemsSet.delete(item);
            }
            result.push(list);
        }
    }
    return result;
};
 
/**
 * Gets common prefix.
 * @param {string[]} items items
 * @returns {string} common prefix
 */
const getCommonPrefix = (items) => {
    let prefix = items[0];
    for (let i = 1; i < items.length; i++) {
        const item = items[i];
        for (let p = 0; p < prefix.length; p++) {
            if (item[p] !== prefix[p]) {
                prefix = prefix.slice(0, p);
                break;
            }
        }
    }
    return prefix;
};
 
/**
 * Gets common suffix.
 * @param {string[]} items items
 * @returns {string} common suffix
 */
const getCommonSuffix = (items) => {
    let suffix = items[0];
    for (let i = 1; i < items.length; i++) {
        const item = items[i];
        for (let p = item.length - 1, s = suffix.length - 1; s >= 0; p--, s--) {
            if (item[p] !== suffix[s]) {
                suffix = suffix.slice(s + 1);
                break;
            }
        }
    }
    return suffix;
};
 
/**
 * Returns regexp.
 * @param {string[]} itemsArr array of items
 * @returns {string} regexp
 */
const itemsToRegexp = (itemsArr) => {
    if (itemsArr.length === 1) {
        return quoteMeta(itemsArr[0]);
    }
    /** @type {string[]} */
    const finishedItems = [];
 
    // merge single char items: (a|b|c|d|ef) => ([abcd]|ef)
    let countOfSingleCharItems = 0;
    for (const item of itemsArr) {
        if (item.length === 1) {
            countOfSingleCharItems++;
        }
    }
    // special case for only single char items
    if (countOfSingleCharItems === itemsArr.length) {
        return `[${charsToCharClassContent(itemsArr)}]`;
    }
    /** @type {Set<string>} */
    const items = new Set(itemsArr.sort());
    if (countOfSingleCharItems > 2) {
        /** @type {string[]} */
        const singleCharItems = [];
        for (const item of items) {
            if (item.length === 1) {
                singleCharItems.push(item);
                items.delete(item);
            }
        }
        finishedItems.push(`[${charsToCharClassContent(singleCharItems)}]`);
    }
 
    // special case for 2 items with common prefix/suffix
    if (finishedItems.length === 0 && items.size === 2) {
        const prefix = getCommonPrefix(itemsArr);
        const suffix = getCommonSuffix(
            itemsArr.map((item) => item.slice(prefix.length))
        );
        if (prefix.length > 0 || suffix.length > 0) {
            return `${quoteMeta(prefix)}${itemsToRegexp(
                itemsArr.map((i) => i.slice(prefix.length, -suffix.length || undefined))
            )}${quoteMeta(suffix)}`;
        }
    }
 
    // special case for 2 items with common suffix
    if (finishedItems.length === 0 && items.size === 2) {
        /** @type {SetIterator<string>} */
        const it = items[Symbol.iterator]();
        const a = /** @type {string} */ (it.next().value);
        const b = /** @type {string} */ (it.next().value);
        if (a.length > 0 && b.length > 0 && a.slice(-1) === b.slice(-1)) {
            return `${itemsToRegexp([a.slice(0, -1), b.slice(0, -1)])}${quoteMeta(
                a.slice(-1)
            )}`;
        }
    }
 
    // find common prefix: (a1|a2|a3|a4|b5) => (a(1|2|3|4)|b5)
    const prefixed = popCommonItems(
        items,
        (item) => (item.length >= 1 ? item[0] : false),
        (list) => {
            if (list.length >= 3) return true;
            if (list.length <= 1) return false;
            return list[0][1] === list[1][1];
        }
    );
    for (const prefixedItems of prefixed) {
        const prefix = getCommonPrefix(prefixedItems);
        finishedItems.push(
            `${quoteMeta(prefix)}${itemsToRegexp(
                prefixedItems.map((i) => i.slice(prefix.length))
            )}`
        );
    }
 
    // find common suffix: (a1|b1|c1|d1|e2) => ((a|b|c|d)1|e2)
    const suffixed = popCommonItems(
        items,
        (item) => (item.length >= 1 ? item.slice(-1) : false),
        (list) => {
            if (list.length >= 3) return true;
            if (list.length <= 1) return false;
            return list[0].slice(-2) === list[1].slice(-2);
        }
    );
    for (const suffixedItems of suffixed) {
        const suffix = getCommonSuffix(suffixedItems);
        finishedItems.push(
            `${itemsToRegexp(
                suffixedItems.map((i) => i.slice(0, -suffix.length))
            )}${quoteMeta(suffix)}`
        );
    }
 
    /** @type {string[]} */
    const conditional = [...finishedItems, ...Array.from(items, quoteMeta)];
    if (conditional.length === 1) return conditional[0];
    return `(${conditional.join("|")})`;
};
 
compileBooleanMatcher.fromLists = compileBooleanMatcherFromLists;
compileBooleanMatcher.itemsToRegexp = itemsToRegexp;
 
module.exports = compileBooleanMatcher;