-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort-ordered-attributes.ts
More file actions
98 lines (83 loc) · 2.96 KB
/
Copy pathsort-ordered-attributes.ts
File metadata and controls
98 lines (83 loc) · 2.96 KB
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
/**
* @typedef {import("../types.js").AttrMapComparator} AttrMapComparator
* @typedef {import("../types.js").AttrOrder} AttrOrder
*/
import type {
AttrName,
AttrPair,
AttrMapComparator,
AttrOrder,
} from '../types.js';
/**
* Creates a comparison function to sort attributes
* with support for regular expressions and a wildcard.
*
* A standalone `*` represents all unspecified attributes,
* useful for inserting these at a specific index among
* the ordered attributes.
*
* @param {AttrOrder[]} expectedOrder - The order to sort attributes into.
* @throws {TypeError} If the expectedOrder is empty.
* @returns {AttrMapComparator}
*/
export function createOrderedAttributesComparator(expectedOrder: AttrOrder[]): AttrMapComparator
{
if (!Array.isArray(expectedOrder) || !expectedOrder.length) {
throw new TypeError(
`createOrderedAttributesComparator expected an array with at least 1 attribute name, pattern, or wildcard`
);
}
const restIndexInOrderFlag = expectedOrder.indexOf('*');
const restInOrderFlag = (restIndexInOrderFlag > -1);
/**
* @param {AttrOrder} order
* @param {AttrName} name
* @returns {boolean}
*/
function isInExpectedOrder(order: AttrOrder, name: AttrName): boolean
{
// Ignore the standalone wildcard to allow
// other ordered attributes to match first.
if (order === '*') {
return false;
}
if (order instanceof RegExp) {
return order.test(name);
}
return order === name;
}
/**
* @type {AttrMapComparator}
*/
function orderedAttributesComparator([ aName ]: AttrPair, [ bName ]: AttrPair): number
{
let aIndexInOrderFlag = expectedOrder.findIndex((order) => isInExpectedOrder(order, aName));
let bIndexInOrderFlag = expectedOrder.findIndex((order) => isInExpectedOrder(order, bName));
if (restInOrderFlag) {
if (aIndexInOrderFlag === -1) {
aIndexInOrderFlag = restIndexInOrderFlag;
}
if (bIndexInOrderFlag === -1) {
bIndexInOrderFlag = restIndexInOrderFlag;
}
}
const aInOrderFlag = aIndexInOrderFlag > -1 ? 1 : 0;
const bInOrderFlag = bIndexInOrderFlag > -1 ? 1 : 0;
// sort by position in order parameter
if (aInOrderFlag === 1 && bInOrderFlag === 1) {
// sort alphabetically if they match the same position
if (aIndexInOrderFlag === bIndexInOrderFlag) {
return aName < bName ? -1 : 1;
}
return aIndexInOrderFlag - bIndexInOrderFlag;
}
// put attributes from order parameter before others
const priorityOrder = bInOrderFlag - aInOrderFlag;
if (priorityOrder !== 0) {
return priorityOrder;
}
// sort alphabetically
return aName < bName ? -1 : 1;
}
return orderedAttributesComparator;
}