codekingpro/portable-devtools
114k
1/////////////////////////////////////////////////////////////2//3// pgAdmin 4 - PostgreSQL Tools4//5// Copyright (C) 2013 - 2024, The pgAdmin Development Team6// This software is released under the PostgreSQL Licence7//8//////////////////////////////////////////////////////////////9 10define([], function() {11 let pgAdmin = window.pgAdmin = window.pgAdmin || {};12 13 // Reference:14 // https://github.com/javve/natural-sort/blob/master/index.js15 // Changed it to save the function in pgAdmin object.16 pgAdmin.natural_sort = function(a, b, options) {17 options = options || {};18 19 let re = /(^-?\d+(\.?\d*)[df]?e?\d?$|^0x[0-9a-f]+$|\d+)/gi,20 sre = /(^[ ]*|[ ]*$)/g,21 dre = /(^([\w ]+,?[\w ]+)?[\w ]+,?[\w ]+\d+:\d+(:\d+)?[\w ]?|^\d{1,4}[\/\-]\d{1,4}[\/\-]\d{1,4}|^\w+, \w+ \d+, \d{4})/,22 hre = /^0x[0-9a-f]+$/i,23 ore = /^0/,24 i = function(s) {25 return options.insensitive && ('' + s).toLowerCase() || '' + s;26 },27 // convert all to strings strip whitespace28 x = i(a).replace(sre, '') || '',29 y = i(b).replace(sre, '') || '',30 // chunk/tokenize31 xN = x.replace(re, '\0$1\0').replace(/\0$/, '').replace(/^\0/, '').split('\0'),32 yN = y.replace(re, '\0$1\0').replace(/\0$/, '').replace(/^\0/, '').split('\0'),33 // numeric, hex or date detection34 xD = parseInt(x.match(hre)) || (xN.length !== 1 && x.match(dre) && Date.parse(x)),35 yD = parseInt(y.match(hre)) || xD && y.match(dre) && Date.parse(y) || null,36 oFxNcL, oFyNcL,37 mult = options.desc ? -1 : 1;38 39 // first try and sort Hex codes or Dates40 if (yD)41 if (xD < yD) return -1 * mult;42 else if (xD > yD) return 1 * mult;43 44 // natural sorting through split numeric strings and default strings45 for (let cLoc = 0, numS = Math.max(xN.length, yN.length); cLoc < numS; cLoc++) {46 // find floats not starting with '0', string or 0 if not defined (Clint Priest)47 oFxNcL = !(xN[cLoc] || '').match(ore) && parseFloat(xN[cLoc]) || xN[cLoc] || 0;48 oFyNcL = !(yN[cLoc] || '').match(ore) && parseFloat(yN[cLoc]) || yN[cLoc] || 0;49 // handle numeric vs string comparison - number < string - (Kyle Adams)50 if (isNaN(oFxNcL) !== isNaN(oFyNcL)) {51 return (isNaN(oFxNcL) ? 1 : -1) * mult;52 }53 // rely on string comparison if different types - i.e. '02' < 2 != '02' < '2'54 else if (typeof oFxNcL !== typeof oFyNcL) {55 oFxNcL += '';56 oFyNcL += '';57 }58 if (oFxNcL < oFyNcL) return -1 * mult;59 if (oFxNcL > oFyNcL) return 1 * mult;60 }61 return 0;62 };63 64 pgAdmin.numeric_comparator = function(a, b) {65 a = parseInt(a);66 b = parseInt(b);67 if (a < b)68 return -1;69 else if (a == b)70 return 0;71 else72 return 1;73 };74 75 /**76 * Decimal adjustment of a number.77 *78 * @param {String} type The type of adjustment.79 * @param {Number} value The number.80 * @param {Integer} exp The exponent (the 10 logarithm of the adjustment base).81 * @returns {Number} The adjusted value.82 */83 function decimalAdjust(type, value, exp) {84 // If the exp is undefined or zero...85 if (typeof exp === 'undefined' || +exp === 0) {86 return Math[type](value);87 }88 value = +value;89 exp = +exp;90 // If the value is not a number or the exp is not an integer...91 if (isNaN(value) || exp % 1 !== 0) {92 return NaN;93 }94 // Shift95 value = value.toString().split('e');96 value = Math[type](+(value[0] + 'e' + (value[1] ? (+value[1] - exp) : -exp)));97 // Shift back98 value = value.toString().split('e');99 return +(value[0] + 'e' + (value[1] ? (+value[1] + exp) : exp));100 }101 102 // Decimal round103 if (!Math.round10) {104 Math.round10 = function(value, exp) {105 return decimalAdjust('round', value, exp);106 };107 }108 // Decimal floor109 if (!Math.floor10) {110 Math.floor10 = function(value, exp) {111 return decimalAdjust('floor', value, exp);112 };113 }114 // Decimal ceil115 if (!Math.ceil10) {116 Math.ceil10 = function(value, exp) {117 return decimalAdjust('ceil', value, exp);118 };119 }120 121 return pgAdmin;122});123 