forked from adamlaska/browser-compat-data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompare-features.js
More file actions
39 lines (33 loc) · 1.1 KB
/
compare-features.js
File metadata and controls
39 lines (33 loc) · 1.1 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
#!/usr/bin/env node
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
'use strict';
/**
*
* Sort a list of features based upon a specific order:
* 1. __compat is always first
* 2. Alphanumerical features starting with an uppercase letter (without symbols aside from - or _)
* 3. Alphanumerical features starting with a lowercase letter (without symbols aside from - or _)
* 4. All other features
*
*/
const compareFeatures = (a, b) => {
if (a == '__compat') return -1;
if (b == '__compat') return 1;
const capsWordA = /^[A-Z](\w|-)*$/.test(a);
const capsWordB = /^[A-Z](\w|-)*$/.test(b);
const wordA = /^[a-zA-Z](\w|-)*$/.test(a);
const wordB = /^[a-zA-Z](\w|-)*$/.test(b);
if (wordA || wordB) {
if (capsWordA || capsWordB) {
if (capsWordA && capsWordB) return a.localeCompare(b, 'en');
if (capsWordA) return -1;
if (capsWordB) return 1;
}
if (wordA && wordB) return a.localeCompare(b, 'en');
if (wordA) return -1;
return 1;
}
return a.localeCompare(b, 'en');
};
module.exports = compareFeatures;