forked from dequelabs/axe-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrule-id-sort.mjs
More file actions
36 lines (35 loc) · 880 Bytes
/
Copy pathrule-id-sort.mjs
File metadata and controls
36 lines (35 loc) · 880 Bytes
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
/**
* Stable rule/check id ordering aligned with historical Grunt/glob output:
* when one id equals `otherId + '-' + suffix`, the longer id sorts first
* (e.g. `color-contrast-enhanced` before `color-contrast`).
*
* Unrelated ids use `localeCompare`.
*
* @param {string} a
* @param {string} b
* @returns {number}
*/
export function compareRuleIds(a, b) {
if (a === b) {
return 0;
}
if (a.startsWith(b + '-')) {
return -1;
}
if (b.startsWith(a + '-')) {
return 1;
}
return a.localeCompare(b);
}
/**
* @param {Record<string, unknown>} record
* @returns {Record<string, unknown>}
*/
export function sortRecordKeysByRuleId(record) {
if (!record || typeof record !== 'object' || Array.isArray(record)) {
return record;
}
return Object.fromEntries(
Object.entries(record).sort(([keyA], [keyB]) => compareRuleIds(keyA, keyB))
);
}