Skip to content

Commit

Permalink
fix(sorter): issue #72 circular dependency from last PR #70
Browse files Browse the repository at this point in the history
  • Loading branch information
ghiscoding committed Jun 1, 2018
1 parent 0e1d176 commit 97450c2
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -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));
}
Original file line number Diff line number Diff line change
@@ -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);
};
6 changes: 3 additions & 3 deletions aurelia-slickgrid/src/aurelia-slickgrid/sorters/dateSorter.ts
Original file line number Diff line number Diff line change
@@ -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);
};
Original file line number Diff line number Diff line change
@@ -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);
};
Original file line number Diff line number Diff line change
@@ -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);
};
Original file line number Diff line number Diff line change
@@ -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));
Expand Down
17 changes: 0 additions & 17 deletions aurelia-slickgrid/src/aurelia-slickgrid/sorters/sorterUtilities.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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));
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit 97450c2

Please sign in to comment.