-
Notifications
You must be signed in to change notification settings - Fork 182
/
sort.ts
58 lines (49 loc) · 1.96 KB
/
sort.ts
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
/*
* Copyright 2021 IBM Corporation
*
* 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.
*/
import { Row } from '@kui-shell/core'
import { SortByDirection } from '@patternfly/react-table'
const byte = 1
const kilobyte = 1024 * byte
const megabyte = 1024 * kilobyte
const gigabyte = 1024 * megabyte
const terabyte = 1024 * gigabyte
const petabyte = 1024 * terabyte
const exabyte = 1024 * petabyte
const sizes = { B: byte, K: kilobyte, M: megabyte, G: gigabyte, T: terabyte, P: petabyte, E: exabyte }
function toSize(val: string): number {
const last = val.charAt(val.length - 1)
const mult = sizes[last] || exabyte
return mult * parseInt(val.slice(0, val.length - 2), 10)
}
function sortBySize(rowA: Row, rowB: Row, cidx: number, dir: number): number {
const cellA = rowA.attributes[cidx - 1].value
const cellB = rowB.attributes[cidx - 1].value
return (toSize(cellA) - toSize(cellB)) * dir
}
function sortRowWithDir(rowA: Row, rowB: Row, key: string, cidx: number, direction: SortByDirection): number {
const dir = direction === 'asc' ? 1 : -1
if (key === 'SIZE') {
return sortBySize(rowA, rowB, cidx, dir)
} else {
return rowA.name.localeCompare(rowB.name) * dir
}
}
export default function sortRow(rowA: Row, rowB: Row, key: string, cidx: number, direction: SortByDirection): number {
return sortRowWithDir(rowA, rowB, key.toUpperCase(), cidx, direction)
}
export function isSortableCol(key: string) {
return /NAME|SIZE|NAMESPACE|OBJECT/i.test(key.toUpperCase())
}