From addb0eb01330eb6109e75424a0db248e0b3b9144 Mon Sep 17 00:00:00 2001 From: Allan Jardine Date: Fri, 15 Jan 2021 16:40:04 +0000 Subject: [PATCH] Fix - sorting: Datetime moment was giving an error when used on non-string types https://datatables.net/forums/discussion/comment/185605#Comment_185605 --- sorting/datetime-moment.js | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/sorting/datetime-moment.js b/sorting/datetime-moment.js index dd4aa93..52b537c 100644 --- a/sorting/datetime-moment.js +++ b/sorting/datetime-moment.js @@ -28,20 +28,24 @@ } }(function ($, moment) { +function strip (d) { + if ( typeof d === 'string' ) { + // Strip HTML tags and newline characters if possible + d = d.replace(/(<.*?>)|(\r?\n|\r)/g, ''); + + // Strip out surrounding white space + d = d.trim(); + } + + return d; +} + $.fn.dataTable.moment = function ( format, locale, reverseEmpties ) { var types = $.fn.dataTable.ext.type; // Add type detection types.detect.unshift( function ( d ) { - if ( d ) { - // Strip HTML tags and newline characters if possible - if ( d.replace ) { - d = d.replace(/(<.*?>)|(\r?\n|\r)/g, ''); - } - - // Strip out surrounding white space - d = d.trim(); - } + d = strip(d); // Null and empty values are acceptable if ( d === '' || d === null ) { @@ -55,15 +59,7 @@ $.fn.dataTable.moment = function ( format, locale, reverseEmpties ) { // Add sorting method - use an integer for the sorting types.order[ 'moment-'+format+'-pre' ] = function ( d ) { - if ( d ) { - // Strip HTML tags and newline characters if possible - if ( d.replace ) { - d = d.replace(/(<.*?>)|(\r?\n|\r)/g, ''); - } - - // Strip out surrounding white space - d = d.trim(); - } + d = strip(d); return !moment(d, format, locale, true).isValid() ? (reverseEmpties ? -Infinity : Infinity) :