From 257a4c7c22ec168a1f550ec1b9add8bd5f1edc78 Mon Sep 17 00:00:00 2001 From: Allan Jardine Date: Tue, 28 Nov 2023 15:39:35 +0000 Subject: [PATCH] Fix: any-number extension should work with non-sting values (ie. numbers!) gh#455 --- sorting/any-number.js | 63 +++++++++++++++++++++++++++++++++++--- sorting/any-number.min.js | 2 +- sorting/any-number.min.mjs | 2 +- sorting/any-number.mjs | 12 +++++--- sorting/src/any-number.ts | 15 ++++++--- 5 files changed, 80 insertions(+), 14 deletions(-) diff --git a/sorting/any-number.js b/sorting/any-number.js index c7eda6e..891b69f 100644 --- a/sorting/any-number.js +++ b/sorting/any-number.js @@ -1,4 +1,51 @@ /*! © SpryMedia Ltd, David Konrad - datatables.net/license */ + +(function( factory ){ + if ( typeof define === 'function' && define.amd ) { + // AMD + define( ['jquery', 'datatables.net'], function ( $ ) { + return factory( $, window, document ); + } ); + } + else if ( typeof exports === 'object' ) { + // CommonJS + var jq = require('jquery'); + var cjsRequires = function (root, $) { + if ( ! $.fn.dataTable ) { + require('datatables.net')(root, $); + } + }; + + if (typeof window === 'undefined') { + module.exports = function (root, $) { + if ( ! root ) { + // CommonJS environments without a window global must pass a + // root. This will give an error otherwise + root = window; + } + + if ( ! $ ) { + $ = jq( root ); + } + + cjsRequires( root, $ ); + return factory( $, root, root.document ); + }; + } + else { + cjsRequires( window, jq ); + module.exports = factory( jq, window, window.document ); + } + } + else { + // Browser + factory( jQuery, window, document ); + } +}(function( $, window, document ) { +'use strict'; +var DataTable = $.fn.dataTable; + + /** * Sorts columns by any number, ignoring text. This plugin is useful if you have * mixed content in a column, but still want to sort by numbers. Any number means @@ -29,10 +76,14 @@ */ function _anyNumberSort(a, b, high) { var reg = /[+-]?((\d+(\.\d*)?)|\.\d+)([eE][+-]?[0-9]+)?/; - a = a.replace(',', '.').match(reg); - a = a !== null ? parseFloat(a[0]) : high; - b = b.replace(',', '.').match(reg); - b = b !== null ? parseFloat(b[0]) : high; + if (typeof a === 'string') { + a = a.replace(',', '.').match(reg); + a = a !== null ? parseFloat(a[0]) : high; + } + if (typeof b === 'string') { + b = b.replace(',', '.').match(reg); + b = b !== null ? parseFloat(b[0]) : high; + } return a < b ? -1 : a > b ? 1 : 0; } DataTable.ext.type.order['any-number-asc'] = function (a, b) { @@ -41,3 +92,7 @@ DataTable.ext.type.order['any-number-asc'] = function (a, b) { DataTable.ext.type.order['any-number-desc'] = function (a, b) { return _anyNumberSort(a, b, Number.NEGATIVE_INFINITY) * -1; }; + + +return DataTable; +})); diff --git a/sorting/any-number.min.js b/sorting/any-number.min.js index a6f59fc..3cfd905 100644 --- a/sorting/any-number.min.js +++ b/sorting/any-number.min.js @@ -1,2 +1,2 @@ /*! © SpryMedia Ltd, David Konrad - datatables.net/license */ -!function(t){var r,u;"function"==typeof define&&define.amd?define(["jquery","datatables.net"],function(e){return t(e,window,document)}):"object"==typeof exports?(r=require("jquery"),u=function(e,n){n.fn.dataTable||require("datatables.net")(e,n)},"undefined"==typeof window?module.exports=function(e,n){return e=e||window,n=n||r(e),u(e,n),t(n,0,e.document)}:(u(window,r),module.exports=t(r,window,window.document))):t(jQuery,window,document)}(function(e,n,t,r){"use strict";e=e.fn.dataTable;function u(e,n,t){var r=/[+-]?((\d+(\.\d*)?)|\.\d+)([eE][+-]?[0-9]+)?/;return(e=null!==(e=e.replace(",",".").match(r))?parseFloat(e[0]):t)<(n=null!==(n=n.replace(",",".").match(r))?parseFloat(n[0]):t)?-1:n b ? 1 : 0; } DataTable.ext.type.order['any-number-asc'] = function (a, b) { diff --git a/sorting/src/any-number.ts b/sorting/src/any-number.ts index ff5510d..11fbc70 100644 --- a/sorting/src/any-number.ts +++ b/sorting/src/any-number.ts @@ -33,10 +33,17 @@ import DataTable from 'datatables.net'; function _anyNumberSort(a, b, high) { var reg = /[+-]?((\d+(\.\d*)?)|\.\d+)([eE][+-]?[0-9]+)?/; - a = a.replace(',', '.').match(reg); - a = a !== null ? parseFloat(a[0]) : high; - b = b.replace(',', '.').match(reg); - b = b !== null ? parseFloat(b[0]) : high; + + if (typeof a === 'string') { + a = a.replace(',', '.').match(reg); + a = a !== null ? parseFloat(a[0]) : high; + } + + if (typeof b === 'string') { + b = b.replace(',', '.').match(reg); + b = b !== null ? parseFloat(b[0]) : high; + } + return a < b ? -1 : a > b ? 1 : 0; }