forked from mahees-hyuna/SDK-NodeJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphp-sort-tests.js
53 lines (38 loc) · 994 Bytes
/
php-sort-tests.js
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
let gateway = require('./gateway.js');
let testData = {'aA': 'second', 'a[': 'first', 'aB': 'third' };
function phpCompatibleSort(a, b) {
let pos = 0;
let rtn;
do {
// codePointAt helpfully returns undefined if pos > length + 1
achr = a.codePointAt(pos);
bchr = b.codePointAt(pos);
// Swap [ for 0.
if (achr == '['.codePointAt(0)) {
achr = '0'.codePointAt(0);
}
if (bchr == '['.codePointAt(0)) {
bchr = '0'.codePointAt(0);
}
if (achr == undefined) { //We don't need to check b at all.
return -1
}
if (bchr == undefined) {
return 1
}
rtn = achr - bchr;
pos++;
} while (rtn == 0)
return rtn;
}
function phpCompatibleSortTests() {
g = ['a', 'c', 'b']
console.log(g.sort(phpCompatibleSort));
g = ['a', 'aaa', 'aa']
console.log(g.sort(phpCompatibleSort));
g = ['acc', 'ab', 'aa', 'ac', 'aaa']
console.log(g.sort(phpCompatibleSort));
g = ['aa', 'ab', 'aA', 'a[']
console.log(g.sort(phpCompatibleSort));
}
phpCompatibleSortTests();