source: trip-planner-front/node_modules/alphanum-sort/lib/compare.js@ e29cc2e

Last change on this file since e29cc2e was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.8 KB
Line 
1var zero = '0'.charCodeAt(0);
2var plus = '+'.charCodeAt(0);
3var minus = '-'.charCodeAt(0);
4
5function isWhitespace(code) {
6 return code <= 32;
7}
8
9function isDigit(code) {
10 return 48 <= code && code <= 57;
11}
12
13function isSign(code) {
14 return code === minus || code === plus;
15}
16
17module.exports = function (opts, a, b) {
18 var checkSign = opts.sign;
19 var ia = 0;
20 var ib = 0;
21 var ma = a.length;
22 var mb = b.length;
23 var ca, cb; // character code
24 var za, zb; // leading zero count
25 var na, nb; // number length
26 var sa, sb; // number sign
27 var ta, tb; // temporary
28 var bias;
29
30 while (ia < ma && ib < mb) {
31 ca = a.charCodeAt(ia);
32 cb = b.charCodeAt(ib);
33 za = zb = 0;
34 na = nb = 0;
35 sa = sb = true;
36 bias = 0;
37
38 // skip over leading spaces
39 while (isWhitespace(ca)) {
40 ia += 1;
41 ca = a.charCodeAt(ia);
42 }
43 while (isWhitespace(cb)) {
44 ib += 1;
45 cb = b.charCodeAt(ib);
46 }
47
48 // skip and save sign
49 if (checkSign) {
50 ta = a.charCodeAt(ia + 1);
51 if (isSign(ca) && isDigit(ta)) {
52 if (ca === minus) {
53 sa = false;
54 }
55 ia += 1;
56 ca = ta;
57 }
58 tb = b.charCodeAt(ib + 1);
59 if (isSign(cb) && isDigit(tb)) {
60 if (cb === minus) {
61 sb = false;
62 }
63 ib += 1;
64 cb = tb;
65 }
66 }
67
68 // compare digits with other symbols
69 if (isDigit(ca) && !isDigit(cb)) {
70 return -1;
71 }
72 if (!isDigit(ca) && isDigit(cb)) {
73 return 1;
74 }
75
76 // compare negative and positive
77 if (!sa && sb) {
78 return -1;
79 }
80 if (sa && !sb) {
81 return 1;
82 }
83
84 // count leading zeros
85 while (ca === zero) {
86 za += 1;
87 ia += 1;
88 ca = a.charCodeAt(ia);
89 }
90 while (cb === zero) {
91 zb += 1;
92 ib += 1;
93 cb = b.charCodeAt(ib);
94 }
95
96 // count numbers
97 while (isDigit(ca) || isDigit(cb)) {
98 if (isDigit(ca) && isDigit(cb) && bias === 0) {
99 if (sa) {
100 if (ca < cb) {
101 bias = -1;
102 } else if (ca > cb) {
103 bias = 1;
104 }
105 } else {
106 if (ca > cb) {
107 bias = -1;
108 } else if (ca < cb) {
109 bias = 1;
110 }
111 }
112 }
113 if (isDigit(ca)) {
114 ia += 1;
115 na += 1;
116 ca = a.charCodeAt(ia);
117 }
118 if (isDigit(cb)) {
119 ib += 1;
120 nb += 1;
121 cb = b.charCodeAt(ib);
122 }
123 }
124
125 // compare number length
126 if (sa) {
127 if (na < nb) {
128 return -1;
129 }
130 if (na > nb) {
131 return 1;
132 }
133 } else {
134 if (na > nb) {
135 return -1;
136 }
137 if (na < nb) {
138 return 1;
139 }
140 }
141
142 // compare numbers
143 if (bias) {
144 return bias;
145 }
146
147 // compare leading zeros
148 if (sa) {
149 if (za > zb) {
150 return -1;
151 }
152 if (za < zb) {
153 return 1;
154 }
155 } else {
156 if (za < zb) {
157 return -1;
158 }
159 if (za > zb) {
160 return 1;
161 }
162 }
163
164 // compare ascii codes
165 if (ca < cb) {
166 return -1;
167 }
168 if (ca > cb) {
169 return 1;
170 }
171
172 ia += 1;
173 ib += 1;
174 }
175
176 // compare length
177 if (ma < mb) {
178 return -1;
179 }
180 if (ma > mb) {
181 return 1;
182 }
183};
Note: See TracBrowser for help on using the repository browser.