xref: /OpenGrok/opengrok-web/src/main/webapp/js/tablesorter-parsers-0.0.3.js (revision 7c0a1357931d754d4eed5bce3a80f71a2eb8dac2)
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * See LICENSE.txt included in this distribution for the specific
9 * language governing permissions and limitations under the License.
10 *
11 * When distributing Covered Code, include this CDDL HEADER in each
12 * file and include the License file at LICENSE.txt.
13 * If applicable, add the following below this CDDL HEADER, with the
14 * fields enclosed by brackets "[]" replaced with your own identifying
15 * information: Portions Copyright [yyyy] [name of copyright owner]
16 *
17 * CDDL HEADER END
18 */
19
20/*
21 * Copyright (c) 2016, 2021 Oracle and/or its affiliates. All rights reserved.
22 */
23$.tablesorter.addParser({
24    id: 'dates',
25    is: function (s) {
26        // return false so this parser is not auto detected
27        return false;
28    },
29    format: function (s) {
30        const date = s.match(/^(\d{2})\-(\w{3})\-(\d{4})$/);
31        if (!date) {
32            return new Date().getTime();
33        }
34        const d = date[1];
35        const m = date[2];
36        const y = date[3];
37        return new Date(m + ' ' + d + ' ' + y).getTime();
38    },
39    type: 'numeric'
40});
41
42$.tablesorter.addParser({
43    id: 'groksizes',
44    is: function (s) {
45        // return false so this parser is not auto detected
46        return false;
47    },
48    format: function (s) {
49        /*
50         * This correctly handles thousand separator
51         * in a big number (either ',' or ' ' or none)
52         *
53         * In our case there is a little gap between 1000 and 1023 which
54         * is still smaller than the next order unit. This should accept all
55         * values like:
56         * 1,000 or 1 000 or 1,023
57         *
58         * However it is more generic just in case. It should not have trouble
59         * with:
60         * 1,000,123,133.235
61         * 1 000.4564
62         * and with even misspelled numbers:
63         * 1,00,345,0.123 (wrong number of digits between the separator)
64         * 13,456 13 45.1234 (mixed separators)
65         * 1000,123 (wrong number of digits between the separator)
66         * 1,123534435,134547435.165165165 (again)
67         */
68        const parts = s.match(/^(\d{1,3}(?:[, ]?\d{1,3})*(?:\.\d+)?|\.\d+) ?(\w*)$/);
69
70        if (parts === null || parts.length < 3) {
71            return 0;
72        }
73
74        const num = parts[1].replace(/[, ]/g, "");
75        const unit = parts[2];
76
77        // convert to bytes
78        if (unit == "KiB") {
79            return (num * 1024);
80        } else if (unit == "MiB") {
81            return (num * 1024 * 1024);
82        } else if (unit == "GiB") {
83            return (num * 1024 * 1024 * 1024);
84        } else {
85            return (num);
86        }
87    },
88    type: 'numeric'
89});
90