Skip to content

Commit

Permalink
Merge pull request DataTables#541 from mvwieringen/mvw/datetime-luxon
Browse files Browse the repository at this point in the history
Fix datetime-luxon.js
  • Loading branch information
AllanJard authored Jul 26, 2022
2 parents dba5a61 8c583e9 commit 47e8c82
Showing 1 changed file with 29 additions and 19 deletions.
48 changes: 29 additions & 19 deletions sorting/datetime-luxon.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 29,11 @@
}(function ($, luxon) {

function strip (d) {
if ( typeof d === 'string' ) {
// Strip HTML tags and newline characters if possible
d = d.replace(/(<.*?>)|(\r?\n|\r)/g, '');
// Strip HTML tags and newline characters if possible
d = d.replace(/(<.*?>)|(\r?\n|\r)/g, '');

// Strip out surrounding white space
d = d.trim();
}
// Strip out surrounding white space
d = d.trim();

return d;
}
Expand All @@ -45,25 43,37 @@ $.fn.dataTable.luxon = function ( format, locale, reverseEmpties ) {

// Add type detection
types.detect.unshift( function ( d ) {
d = strip(d);

// Null and empty values are acceptable
if ( d === '' || d === null ) {
// Null values are acceptable
if ( d === null ) {
return 'luxon-' format;
}
}
if ( typeof d === 'string' ) {
d = strip(d);

return luxon.DateTime.fromFormat( d, format).isValid ?
'luxon-' format :
null;
// Empty values are acceptable
if ( d === '' ) {
return 'luxon-' format;
}

return luxon.DateTime.fromFormat( d, format).isValid ?
'luxon-' format :
null;
} else {
return null;
}
} );

// Add sorting method - use an integer for the sorting
types.order[ 'luxon-' format '-pre' ] = function ( d ) {
d = strip(d);

return !luxon.DateTime.fromFormat(d, format).isValid ?
(reverseEmpties ? -Infinity : Infinity) :
parseInt( luxon.DateTime.fromFormat( d, format).ts, 10 );
if ( typeof d === 'string' ) {
d = strip(d);

return !luxon.DateTime.fromFormat(d, format).isValid ?
(reverseEmpties ? -Infinity : Infinity) :
parseInt( luxon.DateTime.fromFormat( d, format).ts, 10 );
} else {
return null;
}
};
};

Expand Down

0 comments on commit 47e8c82

Please sign in to comment.