Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(perf): improve date sorting speed #259

Merged
merged 2 commits into from
Feb 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('the DateEuro Formatter', () => {

it('should provide a dateIso formatted input and return a formatted date value without time when valid date value is provided', () => {
const value = '2019-05-03 00:00:01';
const result = Formatters.dateEuro(0, 0, value, { input: 'dateIso' } as unknown as Column, {}, {} as any);
const result = Formatters.dateEuro(0, 0, value, { type: 'dateIso' } as unknown as Column, {}, {} as any);
expect(result).toBe('03/05/2019');
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('the Date ISO Formatter', () => {

it('should provide a dateIso formatted input and return a formatted date value without time when valid date value is provided', () => {
const value = '2019-05-03 00:00:01';
const result = Formatters.dateIso(0, 0, value, { input: 'dateIso' } as unknown as Column, {}, {} as any);
const result = Formatters.dateIso(0, 0, value, { type: 'dateIso' } as unknown as Column, {}, {} as any);
expect(result).toBe('2019-05-03');
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('the DateTimeShortEuro Formatter', () => {

it('should provide a dateIso formatted input and return a formatted date value without time when valid date value is provided', () => {
const value = '2019-05-01 02:36:07';
const result = Formatters.dateTimeEuroAmPm(0, 0, value, { input: 'dateIso' } as unknown as Column, {}, {} as any);
const result = Formatters.dateTimeEuroAmPm(0, 0, value, { type: 'dateIso' } as unknown as Column, {}, {} as any);
expect(result).toBe('01/05/2019 02:36:07 am');
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('the DateTimeEuro Formatter', () => {

it('should provide a dateIso formatted input and return a formatted date value without time when valid date value is provided', () => {
const value = '2019-05-01 02:36:07';
const result = Formatters.dateTimeEuro(0, 0, value, { input: 'dateIso' } as unknown as Column, {}, {} as any);
const result = Formatters.dateTimeEuro(0, 0, value, { type: 'dateIso' } as unknown as Column, {}, {} as any);
expect(result).toBe('01/05/2019 02:36:07');
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('the DateTimeIsoAmPm Formatter', () => {

it('should provide a dateIso formatted input and return a formatted date value without time when valid date value is provided', () => {
const value = '2019-05-01 02:36:07';
const result = Formatters.dateTimeIsoAmPm(0, 0, value, { input: 'dateIso' } as unknown as Column, {}, {} as any);
const result = Formatters.dateTimeIsoAmPm(0, 0, value, { type: 'dateIso' } as unknown as Column, {}, {} as any);
expect(result).toBe('2019-05-01 02:36:07 am');
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('the DateTimeIso Formatter', () => {

it('should provide a dateIso formatted input and return a formatted date value without time when valid date value is provided', () => {
const value = '2019-05-01 02:36:07';
const result = Formatters.dateTimeIso(0, 0, value, { input: 'dateIso' } as unknown as Column, {}, {} as any);
const result = Formatters.dateTimeIso(0, 0, value, { type: 'dateIso' } as unknown as Column, {}, {} as any);
expect(result).toBe('2019-05-01 02:36:07');
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('the DateTimeShortEuro Formatter', () => {

it('should provide a dateIso formatted input and return a formatted date value without time when valid date value is provided', () => {
const value = '2019-05-01 02:36:07';
const result = Formatters.dateTimeShortEuro(0, 0, value, { input: 'dateIso' } as unknown as Column, {}, {} as any);
const result = Formatters.dateTimeShortEuro(0, 0, value, { type: 'dateIso' } as unknown as Column, {}, {} as any);
expect(result).toBe('01/05/2019 02:36');
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('the DateTimeShortIso Formatter', () => {

it('should provide a dateIso formatted input and return a formatted date value without time when valid date value is provided', () => {
const value = '2019-05-01 02:36:07';
const result = Formatters.dateTimeShortIso(0, 0, value, { input: 'dateIso' } as unknown as Column, {}, {} as any);
const result = Formatters.dateTimeShortIso(0, 0, value, { type: 'dateIso' } as unknown as Column, {}, {} as any);
expect(result).toBe('2019-05-01 02:36');
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('the DateTimeShortUs Formatter', () => {

it('should provide a dateIso formatted input and return a formatted date value without time when valid date value is provided', () => {
const value = '2019-05-03 02:36:07';
const result = Formatters.dateTimeShortUs(0, 0, value, { input: 'dateIso' } as unknown as Column, {}, {} as any);
const result = Formatters.dateTimeShortUs(0, 0, value, { type: 'dateIso' } as unknown as Column, {}, {} as any);
expect(result).toBe('05/03/2019 02:36');
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('the DateTimeShortUs Formatter', () => {

it('should provide a dateIso formatted input and return a formatted date value without time when valid date value is provided', () => {
const value = '2019-05-03 02:36:07';
const result = Formatters.dateTimeUsAmPm(0, 0, value, { input: 'dateIso' } as unknown as Column, {}, {} as any);
const result = Formatters.dateTimeUsAmPm(0, 0, value, { type: 'dateIso' } as unknown as Column, {}, {} as any);
expect(result).toBe('05/03/2019 02:36:07 am');
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('the DateTimeUs Formatter', () => {

it('should provide a dateIso formatted input and return a formatted date value without time when valid date value is provided', () => {
const value = '2019-05-03 02:36:07';
const result = Formatters.dateTimeUs(0, 0, value, { input: 'dateIso' } as unknown as Column, {}, {} as any);
const result = Formatters.dateTimeUs(0, 0, value, { type: 'dateIso' } as unknown as Column, {}, {} as any);
expect(result).toBe('05/03/2019 02:36:07');
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('the DateUs Formatter', () => {

it('should provide a dateIso formatted input and return a formatted date value without time when valid date value is provided', () => {
const value = '2019-05-03';
const result = Formatters.dateUs(0, 0, value, { input: 'dateIso' } as unknown as Column, {}, {} as any);
const result = Formatters.dateUs(0, 0, value, { type: 'dateIso' } as unknown as Column, {}, {} as any);
expect(result).toBe('05/03/2019');
});

Expand Down
12 changes: 6 additions & 6 deletions packages/common/src/sortComparers/dateUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ const moment = moment_['default'] || moment_; // patch to fix rollup "moment has
export function compareDates(value1: any, value2: any, sortDirection: number, sortColumn: Column, gridOptions: GridOption, format: string | moment_.MomentBuiltinFormat, strict?: boolean) {
let diff = 0;
const checkForUndefinedValues = sortColumn?.valueCouldBeUndefined ?? gridOptions?.cellValueCouldBeUndefined ?? false;
const date1 = moment(value1, format, strict);
const date2 = moment(value2, format, strict);

if (value1 === null || value1 === '' || (checkForUndefinedValues && value1 === undefined) || !moment(value1, format, strict).isValid()) {
if (value1 === null || value1 === '' || (checkForUndefinedValues && value1 === undefined) || !date1.isValid()) {
diff = -1;
} else if (value2 === null || value2 === '' || (checkForUndefinedValues && value2 === undefined) || !moment(value2, format, strict).isValid()) {
} else if (value2 === null || value2 === '' || (checkForUndefinedValues && value2 === undefined) || !date2.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);
diff = date1.valueOf() < date2.valueOf() ? -1 : 1;
}

return sortDirection * (diff === 0 ? 0 : (diff > 0 ? 1 : -1));
return sortDirection * diff;
}

/** From a FieldType, return the associated Date SortComparer */
Expand Down