diff --git a/aurelia-slickgrid/src/aurelia-slickgrid/sorters/compareDateUtility.ts b/aurelia-slickgrid/src/aurelia-slickgrid/sorters/compareDateUtility.ts new file mode 100644 index 000000000..6e64e852d --- /dev/null +++ b/aurelia-slickgrid/src/aurelia-slickgrid/sorters/compareDateUtility.ts @@ -0,0 +1,17 @@ +import * as moment from 'moment'; + +export function compareDates(value1: any, value2: any, sortDirection: number, format: string | moment.MomentBuiltinFormat, strict?: boolean) { + let diff = 0; + + if (value1 === null || value1 === '' || !moment(value1, format, strict).isValid()) { + diff = -1; + } else if (value2 === null || value2 === '' || !moment(value2, format, strict).isValid()) { + diff = 1; + } else { + const date1 = moment(value1, format, strict); + const date2 = moment(value2, format, strict); + diff = parseInt(date1.format('X'), 10) - parseInt(date2.format('X'), 10); + } + + return sortDirection * (diff === 0 ? 0 : (diff > 0 ? 1 : -1)); +} diff --git a/aurelia-slickgrid/src/aurelia-slickgrid/sorters/dateIsoSorter.ts b/aurelia-slickgrid/src/aurelia-slickgrid/sorters/dateIsoSorter.ts index 1b5d3b7ba..4bd436e7b 100644 --- a/aurelia-slickgrid/src/aurelia-slickgrid/sorters/dateIsoSorter.ts +++ b/aurelia-slickgrid/src/aurelia-slickgrid/sorters/dateIsoSorter.ts @@ -1,8 +1,8 @@ import { mapMomentDateFormatWithFieldType } from './../services/utilities'; import { FieldType, Sorter } from './../models/index'; -import { compareDates } from './sorterUtilities'; +import { compareDates } from './compareDateUtility'; const FORMAT = mapMomentDateFormatWithFieldType(FieldType.dateIso); -export const dateIsoSorter: Sorter = (value1, value2, sortDirection) => { - return compareDates(sortDirection, value1, value2, FORMAT, true); +export const dateIsoSorter: Sorter = (value1: any, value2: any, sortDirection: number) => { + return compareDates(value1, value2, sortDirection, FORMAT, true); }; diff --git a/aurelia-slickgrid/src/aurelia-slickgrid/sorters/dateSorter.ts b/aurelia-slickgrid/src/aurelia-slickgrid/sorters/dateSorter.ts index 83c88dabb..b2a74c72f 100644 --- a/aurelia-slickgrid/src/aurelia-slickgrid/sorters/dateSorter.ts +++ b/aurelia-slickgrid/src/aurelia-slickgrid/sorters/dateSorter.ts @@ -1,7 +1,7 @@ import * as moment from 'moment'; import { Sorter } from './../models/sorter.interface'; -import { compareDates } from './sorterUtilities'; +import { compareDates } from './compareDateUtility'; -export const dateSorter: Sorter = (value1, value2, sortDirection) => { - return compareDates(sortDirection, value1, value2, moment.ISO_8601); +export const dateSorter: Sorter = (value1: any, value2: any, sortDirection: number) => { + return compareDates(value1, value2, sortDirection, moment.ISO_8601); }; diff --git a/aurelia-slickgrid/src/aurelia-slickgrid/sorters/dateUsShortSorter.ts b/aurelia-slickgrid/src/aurelia-slickgrid/sorters/dateUsShortSorter.ts index 3d813fb5b..616c88130 100644 --- a/aurelia-slickgrid/src/aurelia-slickgrid/sorters/dateUsShortSorter.ts +++ b/aurelia-slickgrid/src/aurelia-slickgrid/sorters/dateUsShortSorter.ts @@ -1,8 +1,8 @@ import { mapMomentDateFormatWithFieldType } from './../services/utilities'; import { FieldType, Sorter } from './../models/index'; -import { compareDates } from './sorterUtilities'; +import { compareDates } from './compareDateUtility'; const FORMAT = mapMomentDateFormatWithFieldType(FieldType.dateUsShort); -export const dateUsShortSorter: Sorter = (value1, value2, sortDirection) => { - return compareDates(sortDirection, value1, value2, FORMAT, true); +export const dateUsShortSorter: Sorter = (value1: any, value2: any, sortDirection: number) => { + return compareDates(value1, value2, sortDirection, FORMAT, true); }; diff --git a/aurelia-slickgrid/src/aurelia-slickgrid/sorters/dateUsSorter.ts b/aurelia-slickgrid/src/aurelia-slickgrid/sorters/dateUsSorter.ts index 80483bad3..3a078c348 100644 --- a/aurelia-slickgrid/src/aurelia-slickgrid/sorters/dateUsSorter.ts +++ b/aurelia-slickgrid/src/aurelia-slickgrid/sorters/dateUsSorter.ts @@ -1,8 +1,8 @@ import { mapMomentDateFormatWithFieldType } from './../services/utilities'; import { FieldType, Sorter } from './../models/index'; -import { compareDates } from './sorterUtilities'; +import { compareDates } from './compareDateUtility'; const FORMAT = mapMomentDateFormatWithFieldType(FieldType.dateUs); -export const dateUsSorter: Sorter = (value1, value2, sortDirection) => { - return compareDates(sortDirection, value1, value2, FORMAT, true); +export const dateUsSorter: Sorter = (value1: any, value2: any, sortDirection: number) => { + return compareDates(value1, value2, sortDirection, FORMAT, true); }; diff --git a/aurelia-slickgrid/src/aurelia-slickgrid/sorters/numericSorter.ts b/aurelia-slickgrid/src/aurelia-slickgrid/sorters/numericSorter.ts index d3ba4bb7c..a492f75c6 100644 --- a/aurelia-slickgrid/src/aurelia-slickgrid/sorters/numericSorter.ts +++ b/aurelia-slickgrid/src/aurelia-slickgrid/sorters/numericSorter.ts @@ -1,6 +1,6 @@ import { Sorter } from './../models/sorter.interface'; -export const numericSorter: Sorter = (value1, value2, sortDirection) => { +export const numericSorter: Sorter = (value1: any, value2: any, sortDirection: number) => { const x = (isNaN(value1) || value1 === '' || value1 === null) ? -99e+10 : parseFloat(value1); const y = (isNaN(value2) || value2 === '' || value2 === null) ? -99e+10 : parseFloat(value2); return sortDirection * (x === y ? 0 : (x > y ? 1 : -1)); diff --git a/aurelia-slickgrid/src/aurelia-slickgrid/sorters/sorterUtilities.ts b/aurelia-slickgrid/src/aurelia-slickgrid/sorters/sorterUtilities.ts index 2dd52db1d..2f7436031 100644 --- a/aurelia-slickgrid/src/aurelia-slickgrid/sorters/sorterUtilities.ts +++ b/aurelia-slickgrid/src/aurelia-slickgrid/sorters/sorterUtilities.ts @@ -1,6 +1,5 @@ import { FieldType } from './../models/fieldType.enum'; import { Sorters } from './index'; -import * as moment from 'moment'; export function sortByFieldType(value1: any, value2: any, fieldType: FieldType, sortDirection: number) { let sortResult = 0; @@ -28,19 +27,3 @@ export function sortByFieldType(value1: any, value2: any, fieldType: FieldType, return sortResult; } - -export function compareDates(sortDirection: number, value1: any, value2: any, format: string | moment.MomentBuiltinFormat, strict?: boolean) { - let diff = 0; - - if (value1 === null || value1 === '' || !moment(value1, format, strict).isValid()) { - diff = -1; - } else if (value2 === null || value2 === '' || !moment(value2, format, strict).isValid()) { - diff = 1; - } else { - const date1 = moment(value1, format, strict); - const date2 = moment(value2, format, strict); - diff = parseInt(date1.format('X'), 10) - parseInt(date2.format('X'), 10); - } - - return sortDirection * (diff === 0 ? 0 : (diff > 0 ? 1 : -1)); -} diff --git a/aurelia-slickgrid/src/aurelia-slickgrid/sorters/stringSorter.ts b/aurelia-slickgrid/src/aurelia-slickgrid/sorters/stringSorter.ts index 77b3c5fd8..e43ac1491 100644 --- a/aurelia-slickgrid/src/aurelia-slickgrid/sorters/stringSorter.ts +++ b/aurelia-slickgrid/src/aurelia-slickgrid/sorters/stringSorter.ts @@ -1,6 +1,6 @@ import { Sorter } from './../models/sorter.interface'; -export const stringSorter: Sorter = (value1, value2, sortDirection) => { +export const stringSorter: Sorter = (value1: any, value2: any, sortDirection: number) => { let position = 0; if (value1 === null) { position = -1;