@ohos-ports/html-minifier-next 8.4.2-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.txt +23 -0
- package/README.md +776 -0
- package/cli.js +1209 -0
- package/dist/types/htmlminifier.d.ts +556 -0
- package/dist/types/htmlminifier.d.ts.map +1 -0
- package/dist/types/htmlparser.d.ts +25 -0
- package/dist/types/htmlparser.d.ts.map +1 -0
- package/dist/types/lib/attributes.d.ts +155 -0
- package/dist/types/lib/attributes.d.ts.map +1 -0
- package/dist/types/lib/constants.d.ts +110 -0
- package/dist/types/lib/constants.d.ts.map +1 -0
- package/dist/types/lib/content.d.ts +51 -0
- package/dist/types/lib/content.d.ts.map +1 -0
- package/dist/types/lib/elements.d.ts +57 -0
- package/dist/types/lib/elements.d.ts.map +1 -0
- package/dist/types/lib/fragments.d.ts +46 -0
- package/dist/types/lib/fragments.d.ts.map +1 -0
- package/dist/types/lib/option-definitions.d.ts +33 -0
- package/dist/types/lib/option-definitions.d.ts.map +1 -0
- package/dist/types/lib/options.d.ts +120 -0
- package/dist/types/lib/options.d.ts.map +1 -0
- package/dist/types/lib/unused-css.d.ts +46 -0
- package/dist/types/lib/unused-css.d.ts.map +1 -0
- package/dist/types/lib/urls.d.ts +15 -0
- package/dist/types/lib/urls.d.ts.map +1 -0
- package/dist/types/lib/utils.d.ts +102 -0
- package/dist/types/lib/utils.d.ts.map +1 -0
- package/dist/types/lib/whitespace.d.ts +45 -0
- package/dist/types/lib/whitespace.d.ts.map +1 -0
- package/dist/types/presets.d.ts +49 -0
- package/dist/types/presets.d.ts.map +1 -0
- package/dist/types/tokenchain.d.ts +26 -0
- package/dist/types/tokenchain.d.ts.map +1 -0
- package/html-minifier-next.schema.json +306 -0
- package/package.json +100 -0
- package/src/htmlminifier.js +2294 -0
- package/src/htmlparser.js +1069 -0
- package/src/lib/attributes.js +928 -0
- package/src/lib/constants.js +285 -0
- package/src/lib/content.js +190 -0
- package/src/lib/elements.js +274 -0
- package/src/lib/file-pool.js +177 -0
- package/src/lib/fragments.js +274 -0
- package/src/lib/option-definitions.js +231 -0
- package/src/lib/options.js +730 -0
- package/src/lib/unused-css.js +379 -0
- package/src/lib/urls.js +155 -0
- package/src/lib/utils.js +861 -0
- package/src/lib/whitespace.js +321 -0
- package/src/presets.js +57 -0
- package/src/tokenchain.js +123 -0
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whitespace trimming and collapsing
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
RE_ALL_WS_NBSP,
|
|
7
|
+
RE_NBSP_LEADING_GROUP,
|
|
8
|
+
RE_NBSP_LEAD_GROUP,
|
|
9
|
+
RE_NBSP_TRAILING_GROUP,
|
|
10
|
+
RE_NBSP_TRAILING_STRIP,
|
|
11
|
+
inlineElementsToKeepWhitespace,
|
|
12
|
+
inlineElementsToKeepWhitespaceWithin,
|
|
13
|
+
formControlElements
|
|
14
|
+
} from './constants.js';
|
|
15
|
+
|
|
16
|
+
const RE_ANY_WS_NBSP = /[ \n\r\t\f\xA0]/;
|
|
17
|
+
const RE_TAB_NBSP = /[\t\xA0]/;
|
|
18
|
+
const RE_ASCII_WS_RUN = /[ \n\r\f]+/g;
|
|
19
|
+
const RE_NEEDS_COLLAPSE = /[\n\r\f]| {2}/;
|
|
20
|
+
const RE_NON_WS = /\S/;
|
|
21
|
+
|
|
22
|
+
// Trim whitespace
|
|
23
|
+
|
|
24
|
+
/** @param {string} str */
|
|
25
|
+
const trimWhitespace = str => {
|
|
26
|
+
if (!str) return str;
|
|
27
|
+
let start = 0;
|
|
28
|
+
let end = str.length;
|
|
29
|
+
while (start < end && isAsciiWs(str.charCodeAt(start))) start++;
|
|
30
|
+
while (end > start && isAsciiWs(str.charCodeAt(end - 1))) end--;
|
|
31
|
+
return start === 0 && end === str.length ? str : str.slice(start, end);
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/** @param {number} code */
|
|
35
|
+
function isAsciiWs(code) {
|
|
36
|
+
return code === 32 || code === 10 || code === 13 || code === 9 || code === 12;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** @param {number} code */
|
|
40
|
+
function isWsOrNbsp(code) {
|
|
41
|
+
return code === 32 || code === 10 || code === 13 || code === 9 || code === 12 || code === 160;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Collapse all whitespace
|
|
45
|
+
|
|
46
|
+
// Module scope so the hot path doesn’t allocate a closure per call
|
|
47
|
+
/** @param {string} spaces */
|
|
48
|
+
function collapseRun(spaces) {
|
|
49
|
+
// Preserve standalone tabs
|
|
50
|
+
if (spaces === '\t') return '\t';
|
|
51
|
+
if (spaces.indexOf('\xA0') === -1) return ' ';
|
|
52
|
+
return spaces.replace(RE_NBSP_LEADING_GROUP, '$1 ');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** @param {string} str */
|
|
56
|
+
function collapseWhitespaceAll(str) {
|
|
57
|
+
if (!str) return str;
|
|
58
|
+
// Fast path: If there are no common whitespace characters, return early
|
|
59
|
+
if (!RE_ANY_WS_NBSP.test(str)) {
|
|
60
|
+
return str;
|
|
61
|
+
}
|
|
62
|
+
return collapseWhitespaceAllKnown(str);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// As `collapseWhitespaceAll`, for callers that already know `str` holds whitespace
|
|
66
|
+
/** @param {string} str */
|
|
67
|
+
function collapseWhitespaceAllKnown(str) {
|
|
68
|
+
// Only a tab or a no-break space makes the replacement depend on the run itself;
|
|
69
|
+
// without either, every run becomes one space and no per-match callback is needed
|
|
70
|
+
if (!RE_TAB_NBSP.test(str)) {
|
|
71
|
+
return RE_NEEDS_COLLAPSE.test(str) ? str.replace(RE_ASCII_WS_RUN, ' ') : str;
|
|
72
|
+
}
|
|
73
|
+
return str.replace(RE_ALL_WS_NBSP, collapseRun);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Collapse whitespace with options
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* @param {string} str
|
|
80
|
+
* @param {{preserveLineBreaks?: boolean | undefined, conservativeCollapse?: boolean | undefined}} options
|
|
81
|
+
* @param {boolean} trimLeft
|
|
82
|
+
* @param {boolean} trimRight
|
|
83
|
+
* @param {boolean} [collapseAll]
|
|
84
|
+
*/
|
|
85
|
+
function collapseWhitespace(str, options, trimLeft, trimRight, collapseAll = false) {
|
|
86
|
+
if (!str) return str;
|
|
87
|
+
|
|
88
|
+
// Fast path: Nothing to do
|
|
89
|
+
if (!trimLeft && !trimRight && !collapseAll && !options.preserveLineBreaks) {
|
|
90
|
+
return str;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Fast path: No whitespace at all
|
|
94
|
+
if (!RE_ANY_WS_NBSP.test(str)) {
|
|
95
|
+
return str;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return collapseWhitespaceKnown(str, options, trimLeft, trimRight, collapseAll);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// As `collapseWhitespace`, for callers that already know `str` is non-empty, holds
|
|
102
|
+
// whitespace, and has something to do
|
|
103
|
+
/**
|
|
104
|
+
* @param {string} str
|
|
105
|
+
* @param {{preserveLineBreaks?: boolean | undefined, conservativeCollapse?: boolean | undefined}} options
|
|
106
|
+
* @param {boolean} trimLeft
|
|
107
|
+
* @param {boolean} trimRight
|
|
108
|
+
* @param {boolean} collapseAll
|
|
109
|
+
*/
|
|
110
|
+
function collapseWhitespaceKnown(str, options, trimLeft, trimRight, collapseAll) {
|
|
111
|
+
let lineBreakBefore = ''; let lineBreakAfter = '';
|
|
112
|
+
|
|
113
|
+
if (options.preserveLineBreaks) {
|
|
114
|
+
// Find leading/trailing whitespace containing line breaks manually
|
|
115
|
+
// (avoids polynomial backtracking with end-anchored lazy quantifiers)
|
|
116
|
+
let leadEnd = 0;
|
|
117
|
+
while (leadEnd < str.length && isAsciiWs(str.charCodeAt(leadEnd))) {
|
|
118
|
+
leadEnd++;
|
|
119
|
+
}
|
|
120
|
+
if (leadEnd > 0) {
|
|
121
|
+
const leading = str.slice(0, leadEnd);
|
|
122
|
+
if (/[\n\r]/.test(leading)) {
|
|
123
|
+
lineBreakBefore = '\n';
|
|
124
|
+
str = str.slice(leadEnd);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
let trailStart = str.length;
|
|
128
|
+
while (trailStart > 0 && isAsciiWs(str.charCodeAt(trailStart - 1))) {
|
|
129
|
+
trailStart--;
|
|
130
|
+
}
|
|
131
|
+
if (trailStart < str.length) {
|
|
132
|
+
const trailing = str.slice(trailStart);
|
|
133
|
+
if (/[\n\r]/.test(trailing)) {
|
|
134
|
+
lineBreakAfter = '\n';
|
|
135
|
+
str = str.slice(0, trailStart);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (trimLeft) {
|
|
141
|
+
// Find the leading whitespace boundary with a loop, so the common case neither
|
|
142
|
+
// allocates a replacer closure nor runs the regex machinery
|
|
143
|
+
let start = 0;
|
|
144
|
+
while (start < str.length && isWsOrNbsp(str.charCodeAt(start))) {
|
|
145
|
+
start++;
|
|
146
|
+
}
|
|
147
|
+
if (start > 0) {
|
|
148
|
+
const spaces = str.slice(0, start);
|
|
149
|
+
const conservative = !lineBreakBefore && options.conservativeCollapse;
|
|
150
|
+
let replacement;
|
|
151
|
+
if (conservative && spaces === '\t') {
|
|
152
|
+
replacement = '\t';
|
|
153
|
+
} else if (spaces.indexOf('\xA0') === -1) {
|
|
154
|
+
// No no-break space: The whole run goes
|
|
155
|
+
replacement = conservative ? ' ' : '';
|
|
156
|
+
} else {
|
|
157
|
+
// No-break space is specifically handled via the nested regexes
|
|
158
|
+
replacement = spaces.replace(/^[^\xA0]+/, '').replace(RE_NBSP_LEAD_GROUP, '$1 ') || (conservative ? ' ' : '');
|
|
159
|
+
}
|
|
160
|
+
str = replacement + str.slice(start);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (trimRight) {
|
|
165
|
+
// Find trailing whitespace boundary manually (avoids polynomial backtracking
|
|
166
|
+
// with `/[ \n\r\t\f\xA0]+$/` on strings with long internal whitespace runs)
|
|
167
|
+
let end = str.length;
|
|
168
|
+
while (end > 0 && isWsOrNbsp(str.charCodeAt(end - 1))) {
|
|
169
|
+
end--;
|
|
170
|
+
}
|
|
171
|
+
if (end < str.length) {
|
|
172
|
+
const spaces = str.slice(end);
|
|
173
|
+
const conservative = !lineBreakAfter && options.conservativeCollapse;
|
|
174
|
+
let replacement;
|
|
175
|
+
if (conservative && spaces === '\t') {
|
|
176
|
+
replacement = '\t';
|
|
177
|
+
} else if (spaces.indexOf('\xA0') === -1) {
|
|
178
|
+
// No no-break space: The whole run goes
|
|
179
|
+
replacement = conservative ? ' ' : '';
|
|
180
|
+
} else {
|
|
181
|
+
// No-break space is specifically handled via the nested regexes
|
|
182
|
+
replacement = spaces.replace(RE_NBSP_TRAILING_GROUP, ' $1').replace(RE_NBSP_TRAILING_STRIP, '') || (conservative ? ' ' : '');
|
|
183
|
+
}
|
|
184
|
+
str = str.slice(0, end) + replacement;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (collapseAll && str) {
|
|
189
|
+
// Strip non-space whitespace then compress spaces to one
|
|
190
|
+
str = collapseWhitespaceAllKnown(str);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// Avoid string concatenation when no line breaks (common case)
|
|
194
|
+
if (!lineBreakBefore && !lineBreakAfter) return str;
|
|
195
|
+
if (!lineBreakBefore) return str + lineBreakAfter;
|
|
196
|
+
if (!lineBreakAfter) return lineBreakBefore + str;
|
|
197
|
+
return lineBreakBefore + str + lineBreakAfter;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// Collapse whitespace smartly based on surrounding tags
|
|
201
|
+
|
|
202
|
+
// Check if an input element has `type="hidden"`; module-scope so the hot path
|
|
203
|
+
// doesn’t allocate a closure per text node
|
|
204
|
+
/**
|
|
205
|
+
* @param {Array<{name: string, value?: string}>} attrs
|
|
206
|
+
*/
|
|
207
|
+
function isHiddenInput(attrs) {
|
|
208
|
+
if (!attrs || !attrs.length) return false;
|
|
209
|
+
for (const attr of attrs) {
|
|
210
|
+
if (attr.name === 'type') {
|
|
211
|
+
return attr.value === 'hidden';
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
return false;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* @param {string} str
|
|
219
|
+
* @param {string} prevTag
|
|
220
|
+
* @param {string} nextTag
|
|
221
|
+
* @param {Array<{name: string, value?: string}>} prevAttrs
|
|
222
|
+
* @param {Array<{name: string, value?: string}>} nextAttrs
|
|
223
|
+
* @param {{preserveLineBreaks?: boolean, conservativeCollapse?: boolean, collapseInlineTagWhitespace?: boolean}} options
|
|
224
|
+
* @param {Set<string>} inlineElements
|
|
225
|
+
* @param {Set<string>} inlineTextSet
|
|
226
|
+
*/
|
|
227
|
+
function collapseWhitespaceSmart(str, prevTag, nextTag, prevAttrs, nextAttrs, options, inlineElements, inlineTextSet) {
|
|
228
|
+
if (!str) return str;
|
|
229
|
+
|
|
230
|
+
// Fast path: No whitespace at all—every decision below would leave `str`
|
|
231
|
+
// untouched (`collapseWhitespace` returns such strings unchanged)
|
|
232
|
+
if (!RE_ANY_WS_NBSP.test(str)) {
|
|
233
|
+
return str;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const inlineOption = Boolean(options.collapseInlineTagWhitespace);
|
|
237
|
+
|
|
238
|
+
let trimLeft = Boolean(prevTag) && !inlineElementsToKeepWhitespace.has(prevTag);
|
|
239
|
+
let trimRight = Boolean(nextTag) && !inlineElementsToKeepWhitespace.has(nextTag);
|
|
240
|
+
|
|
241
|
+
// Every branch below that consults the text’s content needs a side left untrimmed,
|
|
242
|
+
// so the scan for a non-whitespace character is only worth making then
|
|
243
|
+
const isPureWhitespace = (!trimLeft || !trimRight) && !RE_NON_WS.test(str);
|
|
244
|
+
|
|
245
|
+
if (isPureWhitespace) {
|
|
246
|
+
// Smart default behavior: Collapse space around non-rendering elements
|
|
247
|
+
// (`type="hidden"`); this happens even in basic `collapseWhitespace` mode
|
|
248
|
+
if (!trimLeft && prevTag === 'input' && isHiddenInput(prevAttrs)) {
|
|
249
|
+
trimLeft = true;
|
|
250
|
+
}
|
|
251
|
+
if (!trimRight && nextTag === 'input' && isHiddenInput(nextAttrs)) {
|
|
252
|
+
trimRight = true;
|
|
253
|
+
}
|
|
254
|
+
// Aggressive mode: Collapse between all form controls
|
|
255
|
+
if (inlineOption && (!trimLeft || !trimRight) &&
|
|
256
|
+
formControlElements.has(stripSlash(prevTag)) && formControlElements.has(stripSlash(nextTag))) {
|
|
257
|
+
trimLeft = true;
|
|
258
|
+
trimRight = true;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
if (trimLeft) {
|
|
263
|
+
if (inlineOption) {
|
|
264
|
+
// Still preserve whitespace around inline text elements
|
|
265
|
+
if (inlineElementsToKeepWhitespaceWithin.has(stripSlash(prevTag))) {
|
|
266
|
+
trimLeft = false;
|
|
267
|
+
}
|
|
268
|
+
} else {
|
|
269
|
+
trimLeft = prevTag.charCodeAt(0) === 47 /* / */ ? !inlineElements.has(prevTag.slice(1)) : !inlineTextSet.has(prevTag);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
if (trimRight) {
|
|
274
|
+
if (inlineOption) {
|
|
275
|
+
if (inlineElementsToKeepWhitespaceWithin.has(stripSlash(nextTag))) {
|
|
276
|
+
trimRight = false;
|
|
277
|
+
}
|
|
278
|
+
} else {
|
|
279
|
+
trimRight = nextTag.charCodeAt(0) === 47 /* / */ ? !inlineTextSet.has(nextTag.slice(1)) : !inlineElements.has(nextTag);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
const collapseAll = Boolean(prevTag && nextTag);
|
|
284
|
+
if (!trimLeft && !trimRight && !collapseAll && !options.preserveLineBreaks) {
|
|
285
|
+
return str;
|
|
286
|
+
}
|
|
287
|
+
return collapseWhitespaceKnown(str, options, trimLeft, trimRight, collapseAll);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/** @param {string} tag */
|
|
291
|
+
function stripSlash(tag) {
|
|
292
|
+
return tag && tag.charCodeAt(0) === 47 /* / */ ? tag.slice(1) : tag;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
// Collapse/trim whitespace for given tag
|
|
296
|
+
|
|
297
|
+
// `xmp`, `listing`, and `plaintext` render preformatted, as `pre` does
|
|
298
|
+
// https://html.spec.whatwg.org/multipage/rendering.html#the-page
|
|
299
|
+
const noCollapseWhitespaceTags = new Set(['script', 'style', 'pre', 'textarea', 'xmp', 'listing', 'plaintext']);
|
|
300
|
+
const noTrimWhitespaceTags = new Set(['pre', 'textarea', 'xmp', 'listing', 'plaintext']);
|
|
301
|
+
|
|
302
|
+
/** @param {string} tag */
|
|
303
|
+
function canCollapseWhitespace(tag) {
|
|
304
|
+
return !noCollapseWhitespaceTags.has(tag);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/** @param {string} tag */
|
|
308
|
+
function canTrimWhitespace(tag) {
|
|
309
|
+
return !noTrimWhitespaceTags.has(tag);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
// Exports
|
|
313
|
+
|
|
314
|
+
export {
|
|
315
|
+
trimWhitespace,
|
|
316
|
+
collapseWhitespaceAll,
|
|
317
|
+
collapseWhitespace,
|
|
318
|
+
collapseWhitespaceSmart,
|
|
319
|
+
canCollapseWhitespace,
|
|
320
|
+
canTrimWhitespace
|
|
321
|
+
};
|
package/src/presets.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Preset configurations
|
|
3
|
+
*
|
|
4
|
+
* Presets provide curated option sets for common use cases:
|
|
5
|
+
* - `conservative`: Safe minification suitable for most projects
|
|
6
|
+
* - `comprehensive`: Aggressive minification for maximum file size reduction
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export const presets = {
|
|
10
|
+
conservative: {
|
|
11
|
+
caseSensitive: true,
|
|
12
|
+
collapseBooleanAttributes: true,
|
|
13
|
+
collapseWhitespace: true,
|
|
14
|
+
conservativeCollapse: true,
|
|
15
|
+
preserveLineBreaks: true,
|
|
16
|
+
removeComments: true,
|
|
17
|
+
removeDefaultTypeAttributes: true,
|
|
18
|
+
useShortDoctype: true
|
|
19
|
+
},
|
|
20
|
+
comprehensive: {
|
|
21
|
+
collapseAttributeWhitespace: true,
|
|
22
|
+
collapseBooleanAttributes: true,
|
|
23
|
+
collapseWhitespace: true,
|
|
24
|
+
decodeEntities: true,
|
|
25
|
+
mergeScripts: true,
|
|
26
|
+
minifyCSS: true,
|
|
27
|
+
minifyJS: true,
|
|
28
|
+
minifySVG: true,
|
|
29
|
+
minifyURLs: true,
|
|
30
|
+
removeAttributeQuotes: true,
|
|
31
|
+
removeComments: true,
|
|
32
|
+
removeDefaultTypeAttributes: true,
|
|
33
|
+
removeEmptyAttributes: true,
|
|
34
|
+
removeOptionalTags: true,
|
|
35
|
+
removeRedundantAttributes: true,
|
|
36
|
+
useShortDoctype: true
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Get preset configuration by name.
|
|
42
|
+
* @param {string} name - Preset name (“conservative” or “comprehensive”)
|
|
43
|
+
* @returns {object|null} Preset options object or null if not found
|
|
44
|
+
*/
|
|
45
|
+
export function getPreset(name) {
|
|
46
|
+
if (!name) return null;
|
|
47
|
+
const normalizedName = name.toLowerCase();
|
|
48
|
+
return /** @type {Record<string, object>} */ (presets)[normalizedName] || null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Get list of available preset names
|
|
53
|
+
* @returns {string[]} Array of preset names
|
|
54
|
+
*/
|
|
55
|
+
export function getPresetNames() {
|
|
56
|
+
return Object.keys(presets);
|
|
57
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
class Sorter {
|
|
2
|
+
constructor() {
|
|
3
|
+
// Rank per token, in the order the keys were added—looked up per token, so a
|
|
4
|
+
// token list sorts in one pass instead of one scan per known key
|
|
5
|
+
/** @type {Map<string, number>} */
|
|
6
|
+
this.rank = new Map();
|
|
7
|
+
/** @type {Map<string, Sorter>} */
|
|
8
|
+
this.sorterMap = new Map();
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @param {string[]} tokens
|
|
13
|
+
* @param {number} fromIndex
|
|
14
|
+
* @returns {string[]}
|
|
15
|
+
*/
|
|
16
|
+
sort(tokens, fromIndex = 0) {
|
|
17
|
+
// The present token with the lowest rank comes first—the same choice scanning
|
|
18
|
+
// the keys in order would make
|
|
19
|
+
let best = null;
|
|
20
|
+
let bestRank = Infinity;
|
|
21
|
+
for (let j = fromIndex; j < tokens.length; j++) {
|
|
22
|
+
const rank = this.rank.get(/** @type {string} */ (tokens[j]));
|
|
23
|
+
if (rank !== undefined && rank < bestRank) {
|
|
24
|
+
bestRank = rank;
|
|
25
|
+
best = /** @type {string} */ (tokens[j]);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
if (best === null) return tokens;
|
|
29
|
+
|
|
30
|
+
// Single pass: Count matches and collect non-matches
|
|
31
|
+
let matchCount = 0;
|
|
32
|
+
const others = [];
|
|
33
|
+
for (let j = fromIndex; j < tokens.length; j++) {
|
|
34
|
+
const t = /** @type {string} */ (tokens[j]);
|
|
35
|
+
if (t === best) {
|
|
36
|
+
matchCount++;
|
|
37
|
+
} else {
|
|
38
|
+
others.push(t);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Rebuild: `matchCount` instances of the best token first, then others
|
|
43
|
+
let writeIdx = fromIndex;
|
|
44
|
+
for (let j = 0; j < matchCount; j++) {
|
|
45
|
+
tokens[writeIdx++] = best;
|
|
46
|
+
}
|
|
47
|
+
for (const other of others) {
|
|
48
|
+
tokens[writeIdx++] = other;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return this.sorterMap.get(best)?.sort(tokens, fromIndex + matchCount) ?? tokens;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
class TokenChain {
|
|
56
|
+
constructor() {
|
|
57
|
+
/** @type {Map<string, {arrays: string[][], processed: number}>} */
|
|
58
|
+
this.map = new Map();
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** @param {string[]} tokens */
|
|
62
|
+
add(tokens) {
|
|
63
|
+
tokens.forEach((token) => {
|
|
64
|
+
let entry = this.map.get(token);
|
|
65
|
+
if (!entry) {
|
|
66
|
+
entry = { arrays: [], processed: 0 };
|
|
67
|
+
this.map.set(token, entry);
|
|
68
|
+
}
|
|
69
|
+
entry.arrays.push(tokens);
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
createSorter() {
|
|
74
|
+
const sorter = new Sorter();
|
|
75
|
+
|
|
76
|
+
// Convert map entries to array and sort by frequency (descending), then alphabetically
|
|
77
|
+
const entries = Array.from(this.map.entries()).sort((a, b) => {
|
|
78
|
+
const m = a[1].arrays.length;
|
|
79
|
+
const n = b[1].arrays.length;
|
|
80
|
+
// Sort by length descending (larger first)
|
|
81
|
+
const lengthDiff = n - m;
|
|
82
|
+
if (lengthDiff !== 0) return lengthDiff;
|
|
83
|
+
// If lengths equal, sort by key ascending
|
|
84
|
+
return a[0].localeCompare(b[0]);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
entries.forEach(([token, data]) => {
|
|
88
|
+
if (data.processed < data.arrays.length) {
|
|
89
|
+
const chain = new TokenChain();
|
|
90
|
+
|
|
91
|
+
data.arrays.forEach((tokens) => {
|
|
92
|
+
// Build new array without the current token instead of splicing
|
|
93
|
+
/** @type {string[]} */
|
|
94
|
+
const filtered = [];
|
|
95
|
+
for (const t of tokens) {
|
|
96
|
+
if (t !== token) {
|
|
97
|
+
filtered.push(t);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Mark remaining tokens as processed
|
|
102
|
+
filtered.forEach((t) => {
|
|
103
|
+
const tData = this.map.get(t);
|
|
104
|
+
if (tData) {
|
|
105
|
+
tData.processed++;
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
if (filtered.length > 0) {
|
|
110
|
+
chain.add(filtered);
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
sorter.rank.set(token, sorter.rank.size);
|
|
115
|
+
sorter.sorterMap.set(token, chain.createSorter());
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
return sorter;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export default TokenChain;
|