forked from TanStack/table
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsortTypes.js
More file actions
135 lines (107 loc) · 2.82 KB
/
sortTypes.js
File metadata and controls
135 lines (107 loc) · 2.82 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
const reSplitAlphaNumeric = /([0-9]+)/gm
// Mixed sorting is slow, but very inclusive of many edge cases.
// It handles numbers, mixed alphanumeric combinations, and even
// null, undefined, and Infinity
export const alphanumeric = (rowA, rowB, columnId) => {
let [a, b] = getRowValuesByColumnID(rowA, rowB, columnId)
// Force to strings (or "" for unsupported types)
a = toString(a)
b = toString(b)
// Split on number groups, but keep the delimiter
// Then remove falsey split values
a = a.split(reSplitAlphaNumeric).filter(Boolean)
b = b.split(reSplitAlphaNumeric).filter(Boolean)
// While
while (a.length && b.length) {
let aa = a.shift()
let bb = b.shift()
const an = parseInt(aa, 10)
const bn = parseInt(bb, 10)
const combo = [an, bn].sort()
// Both are string
if (isNaN(combo[0])) {
if (aa > bb) {
return 1
}
if (bb > aa) {
return -1
}
continue
}
// One is a string, one is a number
if (isNaN(combo[1])) {
return isNaN(an) ? -1 : 1
}
// Both are numbers
if (an > bn) {
return 1
}
if (bn > an) {
return -1
}
}
return a.length - b.length
}
export function datetime(rowA, rowB, columnId) {
let [a, b] = getRowValuesByColumnID(rowA, rowB, columnId)
a = a.getTime()
b = b.getTime()
return compareBasic(a, b)
}
export function basic(rowA, rowB, columnId) {
let [a, b] = getRowValuesByColumnID(rowA, rowB, columnId)
return compareBasic(a, b)
}
export function string(rowA, rowB, columnId) {
let [a, b] = getRowValuesByColumnID(rowA, rowB, columnId)
a = a.split('').filter(Boolean)
b = b.split('').filter(Boolean)
while (a.length && b.length) {
let aa = a.shift()
let bb = b.shift()
let alower = aa.toLowerCase()
let blower = bb.toLowerCase()
// Case insensitive comparison until characters match
if (alower > blower) {
return 1
}
if (blower > alower) {
return -1
}
// If lowercase characters are identical
if (aa > bb) {
return 1
}
if (bb > aa) {
return -1
}
continue
}
return a.length - b.length
}
export function number(rowA, rowB, columnId) {
let [a, b] = getRowValuesByColumnID(rowA, rowB, columnId)
const replaceNonNumeric = /[^0-9.]/gi
a = Number(String(a).replace(replaceNonNumeric, ''))
b = Number(String(b).replace(replaceNonNumeric, ''))
return compareBasic(a, b)
}
// Utils
function compareBasic(a, b) {
return a === b ? 0 : a > b ? 1 : -1
}
function getRowValuesByColumnID(row1, row2, columnId) {
return [row1.values[columnId], row2.values[columnId]]
}
function toString(a) {
if (typeof a === 'number') {
if (isNaN(a) || a === Infinity || a === -Infinity) {
return ''
}
return String(a)
}
if (typeof a === 'string') {
return a
}
return ''
}