jqgrid_utils 1.28.0 → 1.29.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -507,6 +507,73 @@ col_model = await jqu.add_formatter(col_model,'select',{ formatter: "select", fo
507
507
  return col_model;
508
508
  }
509
509
 
510
+ /**
511
+ * Natural Sort Column
512
+ @alias module:Jqgrid_utils
513
+ @param {array} - grid col_model
514
+ @param {string} - string columns names for natural sorting
515
+ @returns {array} - col_model
516
+ @example
517
+ var jqu = new Jqgrid_utils();
518
+ col_model = await jqu.natural_sort(col_model,'colunmename');
519
+ */
520
+
521
+ async natural_sort(col_model, column_name)
522
+ {
523
+ for(let i=0;i< col_model.length;i++)
524
+ {
525
+ if(col_model[i]['name'] === column_name)
526
+ {
527
+
528
+ col_model[i]['sortfunc'] = function (a, b, d) {
529
+
530
+ if(d===undefined) { d=1; }
531
+ var re = /(^-?[0-9]+(\.?[0-9]*)[df]?e?[0-9]?$|^0x[0-9a-f]+$|[0-9]+)/gi,
532
+ sre = /(^[ ]*|[ ]*$)/g,
533
+ dre = /(^([\w ]+,?[\w ]+)?[\w ]+,?[\w ]+\d+:\d+(:\d+)?[\w ]?|^\d{1,4}[\/\-]\d{1,4}[\/\-]\d{1,4}|^\w+, \w+ \d+, \d{4})/,
534
+ hre = /^0x[0-9a-f]+$/i,
535
+ ore = /^0/,
536
+ i = function(s) { return self.insensitive && (''+s).toLowerCase() || ''+s },
537
+ // convert all to strings strip whitespace
538
+ x = i(a).replace(sre, '') || '',
539
+ y = i(b).replace(sre, '') || '',
540
+ // chunk/tokenize
541
+ xN = x.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0'),
542
+ yN = y.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0'),
543
+ // numeric, hex or date detection
544
+ xD = parseInt(x.match(hre)) || (xN.length != 1 && x.match(dre) && Date.parse(x)),
545
+ yD = parseInt(y.match(hre)) || xD && y.match(dre) && Date.parse(y) || null,
546
+ oFxNcL, oFyNcL;
547
+ // first try and sort Hex codes or Dates
548
+ if (yD)
549
+ if ( xD < yD ) return -d;
550
+ else if ( xD > yD ) return d;
551
+ // natural sorting through split numeric strings and default strings
552
+ for(var cLoc=0, numS=Math.max(xN.length, yN.length); cLoc < numS; cLoc++) {
553
+ // find floats not starting with '0', string or 0 if not defined (Clint Priest)
554
+ oFxNcL = !(xN[cLoc] || '').match(ore) && parseFloat(xN[cLoc]) || xN[cLoc] || 0;
555
+ oFyNcL = !(yN[cLoc] || '').match(ore) && parseFloat(yN[cLoc]) || yN[cLoc] || 0;
556
+ // handle numeric vs string comparison - number < string - (Kyle Adams)
557
+ if (isNaN(oFxNcL) !== isNaN(oFyNcL)) { return (isNaN(oFxNcL)) ? d : -d; }
558
+ // rely on string comparison if different types - i.e. '02' < 2 != '02' < '2'
559
+ else if (typeof oFxNcL !== typeof oFyNcL) {
560
+ oFxNcL += '';
561
+ oFyNcL += '';
562
+ }
563
+ if (oFxNcL < oFyNcL) return -d;
564
+ if (oFxNcL > oFyNcL) return d;
565
+ }
566
+ return 0;
567
+ };
568
+
569
+
570
+
571
+ }
572
+ }
573
+ return col_model;
574
+ }
575
+
576
+
510
577
 
511
578
  /**
512
579
  * Add HTML Formatter
package/jqgrid_utils.js CHANGED
@@ -506,6 +506,73 @@ col_model = await jqu.add_formatter(col_model,'select',{ formatter: "select", fo
506
506
  return col_model;
507
507
  }
508
508
 
509
+ /**
510
+ * Natural Sort Column
511
+ @alias module:Jqgrid_utils
512
+ @param {array} - grid col_model
513
+ @param {string} - string columns names for natural sorting
514
+ @returns {array} - col_model
515
+ @example
516
+ var jqu = new Jqgrid_utils();
517
+ col_model = await jqu.natural_sort(col_model,'colunmename');
518
+ */
519
+
520
+ async natural_sort(col_model, column_name)
521
+ {
522
+ for(let i=0;i< col_model.length;i++)
523
+ {
524
+ if(col_model[i]['name'] === column_name)
525
+ {
526
+
527
+ col_model[i]['sortfunc'] = function (a, b, d) {
528
+
529
+ if(d===undefined) { d=1; }
530
+ var re = /(^-?[0-9]+(\.?[0-9]*)[df]?e?[0-9]?$|^0x[0-9a-f]+$|[0-9]+)/gi,
531
+ sre = /(^[ ]*|[ ]*$)/g,
532
+ dre = /(^([\w ]+,?[\w ]+)?[\w ]+,?[\w ]+\d+:\d+(:\d+)?[\w ]?|^\d{1,4}[\/\-]\d{1,4}[\/\-]\d{1,4}|^\w+, \w+ \d+, \d{4})/,
533
+ hre = /^0x[0-9a-f]+$/i,
534
+ ore = /^0/,
535
+ i = function(s) { return self.insensitive && (''+s).toLowerCase() || ''+s },
536
+ // convert all to strings strip whitespace
537
+ x = i(a).replace(sre, '') || '',
538
+ y = i(b).replace(sre, '') || '',
539
+ // chunk/tokenize
540
+ xN = x.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0'),
541
+ yN = y.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0'),
542
+ // numeric, hex or date detection
543
+ xD = parseInt(x.match(hre)) || (xN.length != 1 && x.match(dre) && Date.parse(x)),
544
+ yD = parseInt(y.match(hre)) || xD && y.match(dre) && Date.parse(y) || null,
545
+ oFxNcL, oFyNcL;
546
+ // first try and sort Hex codes or Dates
547
+ if (yD)
548
+ if ( xD < yD ) return -d;
549
+ else if ( xD > yD ) return d;
550
+ // natural sorting through split numeric strings and default strings
551
+ for(var cLoc=0, numS=Math.max(xN.length, yN.length); cLoc < numS; cLoc++) {
552
+ // find floats not starting with '0', string or 0 if not defined (Clint Priest)
553
+ oFxNcL = !(xN[cLoc] || '').match(ore) && parseFloat(xN[cLoc]) || xN[cLoc] || 0;
554
+ oFyNcL = !(yN[cLoc] || '').match(ore) && parseFloat(yN[cLoc]) || yN[cLoc] || 0;
555
+ // handle numeric vs string comparison - number < string - (Kyle Adams)
556
+ if (isNaN(oFxNcL) !== isNaN(oFyNcL)) { return (isNaN(oFxNcL)) ? d : -d; }
557
+ // rely on string comparison if different types - i.e. '02' < 2 != '02' < '2'
558
+ else if (typeof oFxNcL !== typeof oFyNcL) {
559
+ oFxNcL += '';
560
+ oFyNcL += '';
561
+ }
562
+ if (oFxNcL < oFyNcL) return -d;
563
+ if (oFxNcL > oFyNcL) return d;
564
+ }
565
+ return 0;
566
+ };
567
+
568
+
569
+
570
+ }
571
+ }
572
+ return col_model;
573
+ }
574
+
575
+
509
576
 
510
577
  /**
511
578
  * Add HTML Formatter
package/package.json CHANGED
@@ -29,5 +29,5 @@
29
29
  {
30
30
  "test": "echo \"Error: no test specified\" && exit 1"
31
31
  },
32
- "version": "1.28.0"
32
+ "version": "1.29.0"
33
33
  }