Skip to content

Commit

Permalink
feat(sorters): consolidate & provide all date sorters
Browse files Browse the repository at this point in the history
  • Loading branch information
Ghislain Beaulac authored and Ghislain Beaulac committed Aug 19, 2019
1 parent 1e983e2 commit fdc1155
Show file tree
Hide file tree
Showing 20 changed files with 199 additions and 207 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export class CollectionService {
const value1 = (enableTranslateLabel) ? this.translate.instant(dataRow1[propertyName] || ' ') : dataRow1[propertyName];
const value2 = (enableTranslateLabel) ? this.translate.instant(dataRow2[propertyName] || ' ') : dataRow2[propertyName];

const sortResult = sortByFieldType(value1, value2, fieldType, sortDirection, columnDef);
const sortResult = sortByFieldType(fieldType, value1, value2, sortDirection, columnDef);
if (sortResult !== SortDirectionNumber.neutral) {
return sortResult;
}
Expand All @@ -123,7 +123,7 @@ export class CollectionService {
sortedCollection = collection.sort((dataRow1: any, dataRow2: any) => {
const value1 = (enableTranslateLabel) ? this.translate.instant(dataRow1[propertyName] || ' ') : dataRow1[propertyName];
const value2 = (enableTranslateLabel) ? this.translate.instant(dataRow2[propertyName] || ' ') : dataRow2[propertyName];
const sortResult = sortByFieldType(value1, value2, fieldType, sortDirection, columnDef);
const sortResult = sortByFieldType(fieldType, value1, value2, sortDirection, columnDef);
if (sortResult !== SortDirectionNumber.neutral) {
return sortResult;
}
Expand All @@ -136,7 +136,7 @@ export class CollectionService {
sortedCollection = collection.sort((dataRow1: any, dataRow2: any) => {
const value1 = (enableTranslateLabel) ? this.translate.instant(dataRow1 || ' ') : dataRow1;
const value2 = (enableTranslateLabel) ? this.translate.instant(dataRow2 || ' ') : dataRow2;
const sortResult = sortByFieldType(value1, value2, fieldType, sortDirection, columnDef);
const sortResult = sortByFieldType(fieldType, value1, value2, sortDirection, columnDef);
if (sortResult !== SortDirectionNumber.neutral) {
return sortResult;
}
Expand Down
2 changes: 1 addition & 1 deletion src/app/modules/angular-slickgrid/services/sort.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ export class SortService {
return customSortResult;
}
} else {
const sortResult = sortByFieldType(value1, value2, fieldType, sortDirection, columnSortObj.sortCol);
const sortResult = sortByFieldType(fieldType, value1, value2, sortDirection, columnSortObj.sortCol);
if (sortResult !== SortDirectionNumber.neutral) {
return sortResult;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
import { SortDirectionNumber } from '../../models/sortDirectionNumber.enum';
import { dateEuroShortSorter } from '../dateEuroShortSorter';
import { sortByFieldType } from '../sorterUtilities';
import { FieldType, SortDirectionNumber } from '../../models';

describe('the Date Euro Short Sorter', () => {
it('should return an array of Euro dates sorted ascending when only valid dates are provided', () => {
it('should return an array of dates sorted ascending when only valid dates are provided', () => {
const direction = SortDirectionNumber.asc;
const inputArray = ['8/8/98', '8/10/98', '8/8/98', '01/01/18', '14/12/98'];
inputArray.sort((value1, value2) => dateEuroShortSorter(value1, value2, direction));
inputArray.sort((value1, value2) => sortByFieldType(FieldType.dateEuroShort, value1, value2, direction));
expect(inputArray).toEqual(['8/8/98', '8/8/98', '8/10/98', '14/12/98', '01/01/18']);
});

it('should return an array of Euro dates sorted descending when only valid dates are provided', () => {
it('should return an array of dates sorted descending when only valid dates are provided', () => {
const direction = SortDirectionNumber.desc;
const inputArray = ['8/8/98', '8/10/98', null, '8/8/98', '01/01/18', '14/12/98'];
inputArray.sort((value1, value2) => dateEuroShortSorter(value1, value2, direction));
inputArray.sort((value1, value2) => sortByFieldType(FieldType.dateEuroShort, value1, value2, direction));
expect(inputArray).toEqual(['01/01/18', '14/12/98', '8/10/98', '8/8/98', '8/8/98', null]);
});

it(`should return an array with unsorted characters showing at the beginning
then comes numbers sorted ascending when digits and chars are provided`, () => {
const direction = SortDirectionNumber.asc;
const inputArray = ['8/10/98', 'y', '8/8/98', '01/01/18', '14/12/98'];
inputArray.sort((value1, value2) => dateEuroShortSorter(value1, value2, direction));
expect(inputArray).toEqual(['y', '8/8/98', '8/10/98', '14/12/98', '01/01/18']);
});
const direction = SortDirectionNumber.asc;
const inputArray = ['8/10/98', 'y', '8/8/98', '01/01/18', '14/12/98'];
inputArray.sort((value1, value2) => sortByFieldType(FieldType.dateEuroShort, value1, value2, direction));
expect(inputArray).toEqual(['y', '8/8/98', '8/10/98', '14/12/98', '01/01/18']);
});

it(`should return an array with dates sorted descending showing at the beginning then characters`, () => {
const direction = SortDirectionNumber.desc;
const inputArray = ['8/10/98', null, '8/8/98', '01/01/18', '14/12/98'];
inputArray.sort((value1, value2) => dateEuroShortSorter(value1, value2, direction));
inputArray.sort((value1, value2) => sortByFieldType(FieldType.dateEuroShort, value1, value2, direction));
expect(inputArray).toEqual(['01/01/18', '14/12/98', '8/10/98', '8/8/98', null]);
});
});
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
import { SortDirectionNumber } from '../../models/sortDirectionNumber.enum';
import { dateEuroSorter } from '../dateEuroSorter';
import { sortByFieldType } from '../sorterUtilities';
import { FieldType, SortDirectionNumber } from '../../models';

describe('the Date Euro Sorter', () => {
it('should return an array of Euro dates sorted ascending when only valid dates are provided', () => {
it('should return an array of dates sorted ascending when only valid dates are provided', () => {
const direction = SortDirectionNumber.asc;
const inputArray = ['08/08/1998', '08/10/1998', '08/08/1998', '01/01/2000', '14/12/1998'];
inputArray.sort((value1, value2) => dateEuroSorter(value1, value2, direction));
inputArray.sort((value1, value2) => sortByFieldType(FieldType.dateEuro, value1, value2, direction));
expect(inputArray).toEqual(['08/08/1998', '08/08/1998', '08/10/1998', '14/12/1998', '01/01/2000']);
});

it('should return an array of Euro dates sorted descending when only valid dates are provided', () => {
it('should return an array of dates sorted descending when only valid dates are provided', () => {
const direction = SortDirectionNumber.desc;
const inputArray = ['08/08/1998', '08/10/1998', null, '08/08/1998', '01/01/2000', '14/12/1998'];
inputArray.sort((value1, value2) => dateEuroSorter(value1, value2, direction));
inputArray.sort((value1, value2) => sortByFieldType(FieldType.dateEuro, value1, value2, direction));
expect(inputArray).toEqual(['01/01/2000', '14/12/1998', '08/10/1998', '08/08/1998', '08/08/1998', null]);
});

it(`should return an array with unsorted characters showing at the beginning
then comes numbers sorted ascending when digits and chars are provided`, () => {
const direction = SortDirectionNumber.asc;
const inputArray = ['08/10/1998', 'y', '08/08/1998', '01/01/2000', '14/12/1998'];
inputArray.sort((value1, value2) => dateEuroSorter(value1, value2, direction));
expect(inputArray).toEqual(['y', '08/08/1998', '08/10/1998', '14/12/1998', '01/01/2000']);
});
const direction = SortDirectionNumber.asc;
const inputArray = ['08/10/1998', 'y', '08/08/1998', '01/01/2000', '14/12/1998'];
inputArray.sort((value1, value2) => sortByFieldType(FieldType.dateEuro, value1, value2, direction));
expect(inputArray).toEqual(['y', '08/08/1998', '08/10/1998', '14/12/1998', '01/01/2000']);
});

it(`should return an array with dates sorted descending showing at the beginning then characters`, () => {
const direction = SortDirectionNumber.desc;
const inputArray = ['08/10/1998', null, '08/08/1998', '01/01/2000', '14/12/1998'];
inputArray.sort((value1, value2) => dateEuroSorter(value1, value2, direction));
inputArray.sort((value1, value2) => sortByFieldType(FieldType.dateEuro, value1, value2, direction));
expect(inputArray).toEqual(['01/01/2000', '14/12/1998', '08/10/1998', '08/08/1998', null]);
});
});
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
import { SortDirectionNumber } from '../../models/sortDirectionNumber.enum';
import { dateIsoSorter } from '../dateIsoSorter';
import { sortByFieldType } from '../sorterUtilities';
import { FieldType, SortDirectionNumber } from '../../models';

describe('the Date ISO (without time) Sorter', () => {
it('should return an array of US dates sorted ascending when only valid dates are provided', () => {
const direction = SortDirectionNumber.asc;
const inputArray = ['1998-08-08', '1998-10-08', '1998-08-08', '2001-01-01', '1998-12-14'];
inputArray.sort((value1, value2) => dateIsoSorter(value1, value2, direction));
inputArray.sort((value1, value2) => sortByFieldType(FieldType.dateIso, value1, value2, direction));
expect(inputArray).toEqual(['1998-08-08', '1998-08-08', '1998-10-08', '1998-12-14', '2001-01-01']);
});

it('should return an array of US dates sorted descending when only valid dates are provided', () => {
const direction = SortDirectionNumber.desc;
const inputArray = ['1998-08-08', '1998-10-08', null, '1998-08-08', '2001-01-01', '1998-12-14'];
inputArray.sort((value1, value2) => dateIsoSorter(value1, value2, direction));
inputArray.sort((value1, value2) => sortByFieldType(FieldType.dateIso, value1, value2, direction));
expect(inputArray).toEqual(['2001-01-01', '1998-12-14', '1998-10-08', '1998-08-08', '1998-08-08', null]);
});

it(`should return an array with unsorted characters showing at the beginning
then comes numbers sorted ascending when digits and chars are provided`, () => {
const direction = SortDirectionNumber.asc;
const inputArray = ['1998-10-08', 'y', '1998-08-08', '2001-01-01', '1998-12-14'];
inputArray.sort((value1, value2) => dateIsoSorter(value1, value2, direction));
expect(inputArray).toEqual(['y', '1998-08-08', '1998-10-08', '1998-12-14', '2001-01-01']);
});
const direction = SortDirectionNumber.asc;
const inputArray = ['1998-10-08', 'y', '1998-08-08', '2001-01-01', '1998-12-14'];
inputArray.sort((value1, value2) => sortByFieldType(FieldType.dateIso, value1, value2, direction));
expect(inputArray).toEqual(['y', '1998-08-08', '1998-10-08', '1998-12-14', '2001-01-01']);
});

it(`should return an array with dates sorted descending showing at the beginning then characters`, () => {
const direction = SortDirectionNumber.desc;
const inputArray = ['1998-10-08', null, '1998-08-08', '2001-01-01', '1998-12-14'];
inputArray.sort((value1, value2) => dateIsoSorter(value1, value2, direction));
inputArray.sort((value1, value2) => sortByFieldType(FieldType.dateIso, value1, value2, direction));
expect(inputArray).toEqual(['2001-01-01', '1998-12-14', '1998-10-08', '1998-08-08', null]);
});
});
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
import { SortDirectionNumber } from '../../models/sortDirectionNumber.enum';
import { dateSorter } from '../dateSorter';
import { sortByFieldType } from '../sorterUtilities';
import { FieldType, SortDirectionNumber } from '../../models';

describe('the Date Sorter (ISO format with optional time included)', () => {
it('should return an array of ISO dates sorted ascending when only valid dates are provided', () => {
const direction = SortDirectionNumber.asc;
const inputArray = ['1998-08-08', '1998-08-09 01:01:10', null, '1998-08-08', '2000-01-01 23:00:05', '1998-08-09 01:01:11'];
inputArray.sort((value1, value2) => dateSorter(value1, value2, direction));
inputArray.sort((value1, value2) => sortByFieldType(FieldType.date, value1, value2, direction));
expect(inputArray).toEqual([null, '1998-08-08', '1998-08-08', '1998-08-09 01:01:10', '1998-08-09 01:01:11', '2000-01-01 23:00:05']);
});

it('should return an array of ISO dates sorted descending when only valid dates are provided', () => {
const direction = SortDirectionNumber.desc;
const inputArray = ['1998-08-08', '1998-08-09 01:01:10', null, '1998-08-08', '2000-01-01 23:00:05', '1998-08-09 01:01:11'];
inputArray.sort((value1, value2) => dateSorter(value1, value2, direction));
inputArray.sort((value1, value2) => sortByFieldType(FieldType.date, value1, value2, direction));
expect(inputArray).toEqual(['2000-01-01 23:00:05', '1998-08-09 01:01:11', '1998-08-09 01:01:10', '1998-08-08', '1998-08-08', null]);
});

it(`should return an array with unsorted characters showing at the beginning
then comes numbers sorted ascending when digits and chars are provided`, () => {
const direction = SortDirectionNumber.asc;
const inputArray = ['1998-08-09 01:01:10', 'y', '1998-08-08', '2000-01-01 23:00:05', '1998-08-09 01:01:11'];
inputArray.sort((value1, value2) => dateSorter(value1, value2, direction));
expect(inputArray).toEqual(['y', '1998-08-08', '1998-08-09 01:01:10', '1998-08-09 01:01:11', '2000-01-01 23:00:05']);
});
const direction = SortDirectionNumber.asc;
const inputArray = ['1998-08-09 01:01:10', 'y', '1998-08-08', '2000-01-01 23:00:05', '1998-08-09 01:01:11'];
inputArray.sort((value1, value2) => sortByFieldType(FieldType.date, value1, value2, direction));
expect(inputArray).toEqual(['y', '1998-08-08', '1998-08-09 01:01:10', '1998-08-09 01:01:11', '2000-01-01 23:00:05']);
});

it(`should return an array with dates sorted descending showing at the beginning then characters`, () => {
const direction = SortDirectionNumber.desc;
const inputArray = ['1998-08-09 01:01:10', null, '1998-08-08', '2000-01-01 23:00:05', '1998-08-09 01:01:11'];
inputArray.sort((value1, value2) => dateSorter(value1, value2, direction));
inputArray.sort((value1, value2) => sortByFieldType(FieldType.date, value1, value2, direction));
expect(inputArray).toEqual(['2000-01-01 23:00:05', '1998-08-09 01:01:11', '1998-08-09 01:01:10', '1998-08-08', null]);
});
});
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
import { SortDirectionNumber } from '../../models/sortDirectionNumber.enum';
import { dateUsShortSorter } from '../dateUsShortSorter';
import { sortByFieldType } from '../sorterUtilities';
import { FieldType, SortDirectionNumber } from '../../models';

describe('the Date US Short Sorter', () => {
it('should return an array of US dates sorted ascending when only valid dates are provided', () => {
const direction = SortDirectionNumber.asc;
const inputArray = ['8/8/98', '10/8/98', '8/8/98', '01/01/18', '12/14/98'];
inputArray.sort((value1, value2) => dateUsShortSorter(value1, value2, direction));
inputArray.sort((value1, value2) => sortByFieldType(FieldType.dateUsShort, value1, value2, direction));
expect(inputArray).toEqual(['8/8/98', '8/8/98', '10/8/98', '12/14/98', '01/01/18']);
});

it('should return an array of US dates sorted descending when only valid dates are provided', () => {
const direction = SortDirectionNumber.desc;
const inputArray = ['8/8/98', '10/8/98', null, '8/8/98', '01/01/18', '12/14/98'];
inputArray.sort((value1, value2) => dateUsShortSorter(value1, value2, direction));
inputArray.sort((value1, value2) => sortByFieldType(FieldType.dateUsShort, value1, value2, direction));
expect(inputArray).toEqual(['01/01/18', '12/14/98', '10/8/98', '8/8/98', '8/8/98', null]);
});

it(`should return an array with unsorted characters showing at the beginning
then comes numbers sorted ascending when digits and chars are provided`, () => {
const direction = SortDirectionNumber.asc;
const inputArray = ['10/8/98', 'y', '8/8/98', '01/01/18', '12/14/98'];
inputArray.sort((value1, value2) => dateUsShortSorter(value1, value2, direction));
expect(inputArray).toEqual(['y', '8/8/98', '10/8/98', '12/14/98', '01/01/18']);
});
const direction = SortDirectionNumber.asc;
const inputArray = ['10/8/98', 'y', '8/8/98', '01/01/18', '12/14/98'];
inputArray.sort((value1, value2) => sortByFieldType(FieldType.dateUsShort, value1, value2, direction));
expect(inputArray).toEqual(['y', '8/8/98', '10/8/98', '12/14/98', '01/01/18']);
});

it(`should return an array with dates sorted descending showing at the beginning then characters`, () => {
const direction = SortDirectionNumber.desc;
const inputArray = ['10/8/98', null, '8/8/98', '01/01/18', '12/14/98'];
inputArray.sort((value1, value2) => dateUsShortSorter(value1, value2, direction));
inputArray.sort((value1, value2) => sortByFieldType(FieldType.dateUsShort, value1, value2, direction));
expect(inputArray).toEqual(['01/01/18', '12/14/98', '10/8/98', '8/8/98', null]);
});
});
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
import { SortDirectionNumber } from '../../models/sortDirectionNumber.enum';
import { dateUsSorter } from '../dateUsSorter';
import { sortByFieldType } from '../sorterUtilities';
import { FieldType, SortDirectionNumber } from '../../models';

describe('the Date US Sorter', () => {
it('should return an array of US dates sorted ascending when only valid dates are provided', () => {
const direction = SortDirectionNumber.asc;
const inputArray = ['08/08/1998', '10/08/1998', '08/08/1998', '01/01/2000', '12/14/1998'];
inputArray.sort((value1, value2) => dateUsSorter(value1, value2, direction));
inputArray.sort((value1, value2) => sortByFieldType(FieldType.dateUs, value1, value2, direction));
expect(inputArray).toEqual(['08/08/1998', '08/08/1998', '10/08/1998', '12/14/1998', '01/01/2000']);
});

it('should return an array of US dates sorted descending when only valid dates are provided', () => {
const direction = SortDirectionNumber.desc;
const inputArray = ['08/08/1998', '10/08/1998', null, '08/08/1998', '01/01/2000', '12/14/1998'];
inputArray.sort((value1, value2) => dateUsSorter(value1, value2, direction));
inputArray.sort((value1, value2) => sortByFieldType(FieldType.dateUs, value1, value2, direction));
expect(inputArray).toEqual(['01/01/2000', '12/14/1998', '10/08/1998', '08/08/1998', '08/08/1998', null]);
});

it(`should return an array with unsorted characters showing at the beginning
then comes numbers sorted ascending when digits and chars are provided`, () => {
const direction = SortDirectionNumber.asc;
const inputArray = ['10/08/1998', 'y', '08/08/1998', '01/01/2000', '12/14/1998'];
inputArray.sort((value1, value2) => dateUsSorter(value1, value2, direction));
expect(inputArray).toEqual(['y', '08/08/1998', '10/08/1998', '12/14/1998', '01/01/2000']);
});
const direction = SortDirectionNumber.asc;
const inputArray = ['10/08/1998', 'y', '08/08/1998', '01/01/2000', '12/14/1998'];
inputArray.sort((value1, value2) => sortByFieldType(FieldType.dateUs, value1, value2, direction));
expect(inputArray).toEqual(['y', '08/08/1998', '10/08/1998', '12/14/1998', '01/01/2000']);
});

it(`should return an array with dates sorted descending showing at the beginning then characters`, () => {
const direction = SortDirectionNumber.desc;
const inputArray = ['10/08/1998', null, '08/08/1998', '01/01/2000', '12/14/1998'];
inputArray.sort((value1, value2) => dateUsSorter(value1, value2, direction));
inputArray.sort((value1, value2) => sortByFieldType(FieldType.dateUs, value1, value2, direction));
expect(inputArray).toEqual(['01/01/2000', '12/14/1998', '10/08/1998', '08/08/1998', null]);
});
});

0 comments on commit fdc1155

Please sign in to comment.