-
Notifications
You must be signed in to change notification settings - Fork 12
/
jscouch.collator.js
108 lines (94 loc) · 2.75 KB
/
jscouch.collator.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
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
/*
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations under
the License.
Initial implementation:
* http://www.mudynamics.com
* http://labs.mudynamics.com
* http://www.pcapr.net
*/
(function($) {
$.jscouch = $.jscouch || {};
$.jscouch.collator = $.jscouch.collator || {};
$.extend($.jscouch.collator, {
sorter: function(a, b) {
var tA = $.jscouch.collator.type_sort(a);
var tB = $.jscouch.collator.type_sort(b);
if (tA !== tB) {
return tA - tB;
}
return $.jscouch.collator.less_same_type[tA](a, b);
},
type_sort: function(k) {
if (k === undefined || k === null || k === false || k === true) {
return 0;
}
if (typeof(k) === 'number') {
return 1;
}
if (typeof(k) === 'string') {
return 2;
}
if (Object.prototype.toString.apply(k) === '[object Array]') {
return 3;
}
return 4;
},
atom_sort: function(k) {
if (k === undefined) {
return 0;
}
if (k === null) {
return 1;
}
if (k === false) {
return 2;
}
return 3;
},
less_same_type: [
function(atomA, atomB) {
return $.jscouch.collator.atom_sort(atomA) - $.jscouch.collator.atom_sort(atomB);
},
function(numberA, numberB) {
return numberA - numberB;
},
function(stringA, stringB) {
return stringA < stringB ? -1 : stringA > stringB ? 1 : 0;
},
function(arrayA, arrayB) {
for (var i=0; i<arrayA.length; ++i) {
var eA = arrayA[i];
var tA = $.jscouch.collator.type_sort(eA);
var eB = arrayB[i];
var tB = $.jscouch.collator.type_sort(eB);
if (eB === undefined) {
return 1;
}
if (tA === tB) {
var val = $.jscouch.collator.less_same_type[tA](eA, eB);
if (val !== 0) {
return val;
}
} else {
return tA - tB;
}
}
return 0;
},
function(objA, objB) {
var aryA = [];
for (var i in objA) { aryA.push([i, objA[i]]); }
var aryB = [];
for (var j in objB) { aryB.push([j, objB[j]]); }
return $.jscouch.collator.less_same_type[3](aryA, aryB);
}
]
});
})(jQuery);