natural-compare.js
3.34 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
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
'use strict';
const defaultAlphabetIndexMap = [];
function isNumberCode(code) {
return code >= 48/* '0' */ && code <= 57/* '9' */;
}
function naturalCompare(a, b, opts) {
if (typeof a !== 'string') {
throw new TypeError(`The first argument must be a string. Received type '${typeof a}'`);
}
if (typeof b !== 'string') {
throw new TypeError(`The second argument must be a string. Received type '${typeof b}'`);
}
const lengthA = a.length;
const lengthB = b.length;
let indexA = 0;
let indexB = 0;
let alphabetIndexMap = defaultAlphabetIndexMap;
let firstDifferenceInLeadingZeros = 0;
if (opts) {
if (opts.caseInsensitive) {
a = a.toLowerCase();
b = b.toLowerCase();
}
if (opts.alphabet) {
alphabetIndexMap = buildAlphabetIndexMap(opts.alphabet);
}
}
while (indexA < lengthA && indexB < lengthB) {
let charCodeA = a.charCodeAt(indexA);
let charCodeB = b.charCodeAt(indexB);
if (isNumberCode(charCodeA)) {
if (!isNumberCode(charCodeB)) {
return charCodeA - charCodeB;
}
let numStartA = indexA;
let numStartB = indexB;
while (charCodeA === 48/* '0' */ && ++numStartA < lengthA) {
charCodeA = a.charCodeAt(numStartA);
}
while (charCodeB === 48/* '0' */ && ++numStartB < lengthB) {
charCodeB = b.charCodeAt(numStartB);
}
if (numStartA !== numStartB && firstDifferenceInLeadingZeros === 0) {
firstDifferenceInLeadingZeros = numStartA - numStartB;
}
let numEndA = numStartA;
let numEndB = numStartB;
while (numEndA < lengthA && isNumberCode(a.charCodeAt(numEndA))) {
++numEndA;
}
while (numEndB < lengthB && isNumberCode(b.charCodeAt(numEndB))) {
++numEndB;
}
let difference = numEndA - numStartA - numEndB + numStartB; // numA length - numB length
if (difference !== 0) {
return difference;
}
while (numStartA < numEndA) {
difference = a.charCodeAt(numStartA++) - b.charCodeAt(numStartB++);
if (difference !== 0) {
return difference;
}
}
indexA = numEndA;
indexB = numEndB;
continue;
}
if (charCodeA !== charCodeB) {
if (
charCodeA < alphabetIndexMap.length &&
charCodeB < alphabetIndexMap.length &&
alphabetIndexMap[charCodeA] !== -1 &&
alphabetIndexMap[charCodeB] !== -1
) {
return alphabetIndexMap[charCodeA] - alphabetIndexMap[charCodeB];
}
return charCodeA - charCodeB;
}
++indexA;
++indexB;
}
if (indexA < lengthA) { // `b` is a substring of `a`
return 1;
}
if (indexB < lengthB) { // `a` is a substring of `b`
return -1;
}
return firstDifferenceInLeadingZeros;
}
const alphabetIndexMapCache = {};
function buildAlphabetIndexMap(alphabet) {
const existingMap = alphabetIndexMapCache[alphabet];
if (existingMap !== undefined) {
return existingMap;
}
const indexMap = [];
const maxCharCode = alphabet.split('').reduce((maxCode, char) => {
return Math.max(maxCode, char.charCodeAt(0));
}, 0);
for (let i = 0; i <= maxCharCode; i++) {
indexMap.push(-1);
}
for (let i = 0; i < alphabet.length; i++) {
indexMap[alphabet.charCodeAt(i)] = i;
}
alphabetIndexMapCache[alphabet] = indexMap;
return indexMap;
}
module.exports = naturalCompare;