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