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(common)!: migrate from moment to moment-tiny #1456

Merged
merged 8 commits into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion examples/vite-demo-vanilla-bundle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"fetch-jsonp": "^1.3.0",
"flatpickr": "^4.6.13",
"isomorphic-dompurify": "^2.6.0",
"moment-mini": "^2.29.4",
"moment-tiny": "^2.30.3",
"multiple-select-vanilla": "^3.1.0",
"rxjs": "^7.8.1",
"whatwg-fetch": "^3.6.20"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
import { BindingEventService } from '@slickgrid-universal/binding';
import { GraphqlService, type GraphqlPaginatedResult, type GraphqlServiceApi, type GraphqlServiceOption, } from '@slickgrid-universal/graphql';
import { Slicker, type SlickVanillaGridBundle } from '@slickgrid-universal/vanilla-bundle';
import moment from 'moment-mini';
import moment from 'moment-tiny';
import { ExampleGridOptions } from './example-grid-options';
import type { TranslateService } from '../translate.service';
import './example10.scss';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { BindingEventService } from '@slickgrid-universal/binding';
import { SlickCustomTooltip } from '@slickgrid-universal/custom-tooltip-plugin';
import { ExcelExportService } from '@slickgrid-universal/excel-export';
import { Slicker, type SlickVanillaGridBundle } from '@slickgrid-universal/vanilla-bundle';
import moment from 'moment-mini';
import moment from 'moment-tiny';

import exampleModal from './example11-modal.html?raw';
import Example11Modal from './example11-modal';
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
"jest-extended": "^4.0.2",
"jsdom": "^24.0.0",
"jsdom-global": "^3.0.2",
"moment-mini": "^2.29.4",
"moment-tiny": "^2.30.3",
"npm-run-all2": "^6.1.2",
"pnpm": "^8.15.6",
"rimraf": "^5.0.5",
Expand Down
2 changes: 1 addition & 1 deletion packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
"excel-builder-vanilla": "3.0.1",
"flatpickr": "^4.6.13",
"isomorphic-dompurify": "^2.6.0",
"moment-mini": "^2.29.4",
"moment-tiny": "^2.30.3",
"multiple-select-vanilla": "^3.1.0",
"sortablejs": "^1.15.2",
"un-flatten-tree": "^2.0.12"
Expand Down
4 changes: 2 additions & 2 deletions packages/common/src/editors/__tests__/dateEditor.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import moment from 'moment-mini';
import moment from 'moment-tiny';

import { Editors } from '../index';
import { DateEditor } from '../dateEditor';
import { FieldType } from '../../enums/index';
import { Column, ColumnEditor, Editor, EditorArguments, GridOption } from '../../interfaces/index';
import { Column, Editor, EditorArguments, GridOption } from '../../interfaces/index';
import { TranslateServiceStub } from '../../../../../test/translateServiceStub';
import { SlickEvent, type SlickDataView, type SlickGrid } from '../../core/index';

Expand Down
3 changes: 1 addition & 2 deletions packages/common/src/editors/dateEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { createDomElement, destroyAllElementProps, emptyElement, setDeepValue }
import flatpickr from 'flatpickr';
import type { BaseOptions as FlatpickrBaseOptions } from 'flatpickr/dist/types/options';
import type { Instance as FlatpickrInstance } from 'flatpickr/dist/types/instance';
import * as moment_ from 'moment-mini';
const moment = (moment_ as any)['default'] || moment_;
import moment from 'moment-tiny';

import { Constants } from './../constants';
import { FieldType } from '../enums/index';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,35 +75,35 @@ describe('dateEuroShortFilterCondition method', () => {
});

it('should return True when input value is in the range of search terms', () => {
const searchTerms = ['01/12/93..31/12/93'];
const searchTerms = ['1/12/93..31/12/93'];
const options = { dataKey: '', operator: 'EQ', cellValue: '25/12/93', fieldType: FieldType.dateEuroShort, searchTerms } as FilterConditionOption;
const output = executeFilterConditionTest(options, getFilterParsedDates(searchTerms, FieldType.dateEuroShort));
expect(output).toBe(true);
});

it('should return False when input value is not in the range of search terms', () => {
const searchTerms = ['01/12/93..31/12/93'];
const searchTerms = ['1/12/93..31/12/93'];
const options = { dataKey: '', operator: 'EQ', cellValue: '25/11/93', fieldType: FieldType.dateEuroShort, searchTerms } as FilterConditionOption;
const output = executeFilterConditionTest(options, getFilterParsedDates(searchTerms, FieldType.dateEuroShort));
expect(output).toBe(false);
});

it('should return True when input value equals the search terms min inclusive value and operator is set to "rangeInclusive"', () => {
const searchTerms = ['01/12/93..31/12/93'];
const options = { dataKey: '', operator: 'RangeInclusive', cellValue: '01/12/93', fieldType: FieldType.dateEuroShort, searchTerms } as FilterConditionOption;
const searchTerms = ['1/12/93..31/12/93'];
const options = { dataKey: '', operator: 'RangeInclusive', cellValue: '1/12/93', fieldType: FieldType.dateEuroShort, searchTerms } as FilterConditionOption;
const output = executeFilterConditionTest(options, getFilterParsedDates(searchTerms, FieldType.dateEuroShort));
expect(output).toBe(true);
});

it('should return False when input value equals the search terms min inclusive value and operator is set to "RangeExclusive"', () => {
const searchTerms = ['01/12/93..31/12/93'];
const options = { dataKey: '', operator: 'RangeExclusive', cellValue: '01/12/93', fieldType: FieldType.dateEuroShort, searchTerms } as FilterConditionOption;
const searchTerms = ['1/12/93..31/12/93'];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a different behavior now with the date parsing?

Copy link
Owner Author

@ghiscoding ghiscoding Apr 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for unknown reason the newer version of moment doesn't accept zero padding anymore and in some ways it makes sense though, If we look at their Format docs, it does show only 1 digit when using D or M. I could use DD or MM but I'm not sure if that would work and what I'm almost certain is that would probably fail in strict mode when zero padding is missing... I can give it another try tonight though

I guess that what you're saying is that because of this, I might have to wait until next major and advertise the possible strictness in next major, is that it?

image

ok so I went through all their recent PRs and found this commit introduced in v2.30.0 so it seems that it doesn't accept zero padding in strict mode only. I think I use strict mode when sorting and filtering

Sorting

export function compareDates(value1: any, value2: any, sortDirection: number, format: string | moment_.MomentBuiltinFormat, strict?: boolean) {
let diff = 0;
if (value1 === value2) {
diff = 0;
} else {
// use moment to validate the date
let date1: moment_.Moment | Date = moment(value1, format, strict);
let date2: moment_.Moment | Date = moment(value2, format, strict);

Filtering

export function executeDateFilterCondition(options: FilterConditionOption, parsedSearchDates: any[]): boolean {
const filterSearchType = options && (options.filterSearchType || options.fieldType) || FieldType.dateIso;
const FORMAT = mapMomentDateFormatWithFieldType(filterSearchType);
const [searchDate1, searchDate2] = parsedSearchDates;
// cell value in moment format
const dateCell = moment(options.cellValue, FORMAT, true);
// return when cell value is not a valid date
if ((!searchDate1 && !searchDate2) || !dateCell.isValid()) {

Copy link
Owner Author

@ghiscoding ghiscoding Apr 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zewa666 I gave it a try and it is as I thought, when having a date like 01/01/92 and M/D/YY will not pass in strict mode. I think I should keep using strict mode in my lib nonetheless... So it really looks like MomentJS introduced a breaking change without even knowing and for that reason, I guess I have no choice but to postpone it for the next major release in 2 months and mention this new strictness in the migration

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zewa666 alright, so what I found is that to go around the more strictness parsing problem with the new MomentJS version, what we can do is to provide multiple formats via an array. So what I've done is to add 2 formats for all the date formats that are considered as short. For example with Field.dateUsShort, I'm now passing ['MM/DD/YY', 'M/D/YY'] so that the user can still pass dates with/without zero padding. However there are 3 things to note

  1. I can only use 1 output format and so I will always prefer the format with zero padding MM/DD/YY
  2. parsing with multiple formats is slower than passing a single format and the array order matters, so zero padding will be quicker
  3. I use strict mode when filtering & sorting, another idea would be to loosen the parsing and not use strict mode but non-strict mode is often too loose and I prefer strict mode

More info can be found in this Moment doc:
https://momentjscom.readthedocs.io/en/stable/moment/01-parsing/04-string-formats/

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awesome feature. just need something like that for the paste handling

Copy link
Owner Author

@ghiscoding ghiscoding Apr 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wow you're not sleeping, I thought you would reply in a day or two 😋

awesome feature. just need something like that for the paste handling

do you mean for another feature like the copy handler or did you mean something else? Should I go ahead with the merge then? what is your thought on the subject? I guess I'll be ready for a beta version by next week :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kids just woke me up ;)

yeah we've just experienced during the POC that handling date pastes might be tricky as people usually have excel files and stuff whatever they found right into the grid. So having the combo of a couple of supported formats + a locale is a great option.

I think this is definitely good for merge.

going to have another week to focus on the grid in 2w so looking forward to incorporate the new releases and fixes 🎉

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I wish that I could have switched to date-fns but it always seems that MomentJS is still the best and no other lib have all the bells and whistles of Moment.

So I might have a beta by next week but it's still earlier than Angular 18 (which is scheduled for around May 20), however I will still make it the new minimum version requirement. I'm quite happy with the switch to pure CSS SVG, the Dark Mode is looking so so much better now :)

Copy link
Contributor

@zewa666 zewa666 Apr 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dont look at me, still using moment and numeral in most projects bc it just works and the size overhead is ignorable in enterprise internal only apps. 😅

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I totally agree with all of that

const options = { dataKey: '', operator: 'RangeExclusive', cellValue: '1/12/93', fieldType: FieldType.dateEuroShort, searchTerms } as FilterConditionOption;
const output = executeFilterConditionTest(options, getFilterParsedDates(searchTerms, FieldType.dateEuroShort));
expect(output).toBe(false);
});

it('should return False when any of the 2 search term value is not a valid date', () => {
const searchTerms = ['01/12/93..60/12/93'];
const searchTerms = ['1/12/93..60/12/93'];
const options = { dataKey: '', operator: 'RangeExclusive', cellValue: '05/12/93', fieldType: FieldType.dateEuroShort, searchTerms } as FilterConditionOption;
const output = executeFilterConditionTest(options, getFilterParsedDates(searchTerms, FieldType.dateEuroShort));
expect(output).toBe(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ describe('dateUsShortFilterCondition method', () => {
});

it('should return False when cell value is not the same value as the searchTerm', () => {
const searchTerms = ['03/03/2003'];
const searchTerms = ['3/3/2003'];
const options = { dataKey: '', operator: 'EQ', fieldType: FieldType.dateUsShort, cellValue: '12/25/93', searchTerms } as FilterConditionOption;
const output = executeFilterConditionTest(options, getFilterParsedDates(searchTerms, FieldType.dateUsShort));
expect(output).toBe(false);
});

it('should return False even when the cell value is found in the searchTerms since it only compares first term', () => {
const searchTerms = ['03/14/03', '12/25/93'];
const searchTerms = ['3/14/03', '12/25/93'];
const options = { dataKey: '', operator: 'EQ', fieldType: FieldType.dateUsShort, cellValue: '12/25/93', searchTerms } as FilterConditionOption;
const output = executeFilterConditionTest(options, getFilterParsedDates(searchTerms, FieldType.dateUsShort));
expect(output).toBe(false);
Expand Down Expand Up @@ -82,36 +82,36 @@ describe('dateUsShortFilterCondition method', () => {
});

it('should return True when input value is in the range of search terms', () => {
const searchTerms = ['12/01/93..12/31/93'];
const searchTerms = ['12/1/93..12/31/93'];
const options = { dataKey: '', operator: 'EQ', cellValue: '12/25/93', fieldType: FieldType.dateUsShort, searchTerms } as FilterConditionOption;
const output = executeFilterConditionTest(options, getFilterParsedDates(searchTerms, FieldType.dateUsShort));
expect(output).toBe(true);
});

it('should return False when input value is not in the range of search terms', () => {
const searchTerms = ['12/01/93..12/31/93'];
const searchTerms = ['12/1/93..12/31/93'];
const options = { dataKey: '', operator: 'EQ', cellValue: '11/25/93', fieldType: FieldType.dateUsShort, searchTerms } as FilterConditionOption;
const output = executeFilterConditionTest(options, getFilterParsedDates(searchTerms, FieldType.dateUsShort));
expect(output).toBe(false);
});

it('should return True when input value equals the search terms min inclusive value and operator is set to "rangeInclusive"', () => {
const searchTerms = ['12/01/93..12/31/93'];
const options = { dataKey: '', operator: 'RangeInclusive', cellValue: '12/01/93', fieldType: FieldType.dateUsShort, searchTerms } as FilterConditionOption;
const searchTerms = ['12/1/93..12/31/93'];
const options = { dataKey: '', operator: 'RangeInclusive', cellValue: '12/1/93', fieldType: FieldType.dateUsShort, searchTerms } as FilterConditionOption;
const output = executeFilterConditionTest(options, getFilterParsedDates(searchTerms, FieldType.dateUsShort));
expect(output).toBe(true);
});

it('should return False when input value equals the search terms min inclusive value and operator is set to "RangeExclusive"', () => {
const searchTerms = ['12/01/93..12/31/93'];
const options = { dataKey: '', operator: 'RangeExclusive', cellValue: '12/01/93', fieldType: FieldType.dateUsShort, searchTerms } as FilterConditionOption;
const searchTerms = ['12/1/93..12/31/93'];
const options = { dataKey: '', operator: 'RangeExclusive', cellValue: '12/1/93', fieldType: FieldType.dateUsShort, searchTerms } as FilterConditionOption;
const output = executeFilterConditionTest(options, getFilterParsedDates(searchTerms, FieldType.dateUsShort));
expect(output).toBe(false);
});

it('should return False when any of the 2 search term value is not a valid date', () => {
const searchTerms = ['12/01/93..12/60/93'];
const options = { dataKey: '', operator: 'RangeExclusive', cellValue: '12/05/93', fieldType: FieldType.dateUsShort, searchTerms } as FilterConditionOption;
const searchTerms = ['12/1/93..12/60/93'];
const options = { dataKey: '', operator: 'RangeExclusive', cellValue: '12/5/93', fieldType: FieldType.dateUsShort, searchTerms } as FilterConditionOption;
const output = executeFilterConditionTest(options, getFilterParsedDates(searchTerms, FieldType.dateUsShort));
expect(output).toBe(false);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import moment from 'moment-mini';
import moment from 'moment-tiny';

import { getParsedSearchTermsByFieldType } from '../filterConditionProcesses';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import moment from 'moment-mini';
import moment from 'moment-tiny';

import { FieldType, OperatorType, type SearchTerm } from '../enums/index';
import type { FilterConditionOption } from '../interfaces/index';
Expand Down
3 changes: 1 addition & 2 deletions packages/common/src/filters/dateFilter.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { BindingEventService } from '@slickgrid-universal/binding';
import { createDomElement, destroyAllElementProps, emptyElement, } from '@slickgrid-universal/utils';
import flatpickr from 'flatpickr';
import * as moment_ from 'moment-mini';
const moment = (moment_ as any)['default'] || moment_;
import moment from 'moment-tiny';

import type { BaseOptions as FlatpickrBaseOptions } from 'flatpickr/dist/types/options';
import type { Instance as FlatpickrInstance } from 'flatpickr/dist/types/instance';
Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/formatters/formatterUtilities.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getHtmlStringOutput, isPrimitiveOrHTML, stripTags } from '@slickgrid-universal/utils';
import moment from 'moment-mini';
import moment from 'moment-tiny';

import { FieldType } from '../enums/fieldType.enum';
import type { Column, ExcelExportOption, Formatter, FormatterResultWithHtml, FormatterResultWithText, GridOption, TextExportOption } from '../interfaces/index';
Expand Down
3 changes: 1 addition & 2 deletions packages/common/src/services/utilities.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { EventSubscription } from '@slickgrid-universal/event-pub-sub';
import { flatten } from 'un-flatten-tree';
import * as moment_ from 'moment-mini';
const moment = (moment_ as any)['default'] || moment_;
import moment from 'moment-tiny';

import { Constants } from '../constants';
import { FieldType, type OperatorString, OperatorType } from '../enums/index';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,30 @@ import { FieldType, SortDirectionNumber } from '../../enums/index';
describe('the Date Euro Short Sorter', () => {
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'];
const inputArray = ['8/8/98', '8/10/98', '8/8/98', '1/1/18', '14/12/98'];
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']);
expect(inputArray).toEqual(['8/8/98', '8/8/98', '8/10/98', '14/12/98', '1/1/18']);
});

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'];
const inputArray = ['8/8/98', '8/10/98', null, '8/8/98', '1/1/18', '14/12/98'];
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]);
expect(inputArray).toEqual(['1/1/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'];
const inputArray = ['8/10/98', 'y', '8/8/98', '1/1/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']);
expect(inputArray).toEqual(['y', '8/8/98', '8/10/98', '14/12/98', '1/1/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'];
const inputArray = ['8/10/98', null, '8/8/98', '1/1/18', '14/12/98'];
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]);
expect(inputArray).toEqual(['1/1/18', '14/12/98', '8/10/98', '8/8/98', null]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,30 @@ import { FieldType, SortDirectionNumber } from '../../enums/index';
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'];
const inputArray = ['8/8/98', '10/8/98', '8/8/98', '1/1/18', '12/14/98'];
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']);
expect(inputArray).toEqual(['8/8/98', '8/8/98', '10/8/98', '12/14/98', '1/1/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'];
const inputArray = ['8/8/98', '10/8/98', null, '8/8/98', '1/1/18', '12/14/98'];
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]);
expect(inputArray).toEqual(['1/1/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'];
const inputArray = ['10/8/98', 'y', '8/8/98', '1/1/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']);
expect(inputArray).toEqual(['y', '8/8/98', '10/8/98', '12/14/98', '1/1/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'];
const inputArray = ['10/8/98', null, '8/8/98', '1/1/18', '12/14/98'];
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]);
expect(inputArray).toEqual(['1/1/18', '12/14/98', '10/8/98', '8/8/98', null]);
});
});
17 changes: 8 additions & 9 deletions packages/common/src/sortComparers/dateUtilities.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
import * as moment_ from 'moment-mini';
const moment = (moment_ as any)['default'] || moment_;
import moment, { type Moment, type MomentBuiltinFormat } from 'moment-tiny';

import { FieldType } from '../enums/fieldType.enum';
import type { SortComparer } from '../interfaces/index';
import { mapMomentDateFormatWithFieldType } from '../services/utilities';

export function compareDates(value1: any, value2: any, sortDirection: number, format: string | moment_.MomentBuiltinFormat, strict?: boolean) {
export function compareDates(value1: any, value2: any, sortDirection: number, format?: string | MomentBuiltinFormat, strict?: boolean) {
let diff = 0;

if (value1 === value2) {
diff = 0;
} else {
// use moment to validate the date
let date1: moment_.Moment | Date = moment(value1, format, strict);
let date2: moment_.Moment | Date = moment(value2, format, strict);
let date1: Moment | Date = moment(value1, format, strict);
let date2: Moment | Date = moment(value2, format, strict);

// when moment date is invalid, we'll create a temporary old Date
if (!(date1 as moment_.Moment).isValid()) {
if (!(date1 as Moment).isValid()) {
date1 = new Date(1001, 1, 1);
}
if (!(date2 as moment_.Moment).isValid()) {
if (!(date2 as Moment).isValid()) {
date2 = new Date(1001, 1, 1);
}

Expand All @@ -32,10 +31,10 @@ export function compareDates(value1: any, value2: any, sortDirection: number, fo

/** From a FieldType, return the associated Date SortComparer */
export function getAssociatedDateSortComparer(fieldType: typeof FieldType[keyof typeof FieldType]): SortComparer {
const FORMAT = (fieldType === FieldType.date) ? moment.ISO_8601 : mapMomentDateFormatWithFieldType(fieldType);
const FORMAT = (fieldType === FieldType.date) ? undefined : mapMomentDateFormatWithFieldType(fieldType);

return ((value1: any, value2: any, sortDirection: number) => {
if (FORMAT === moment.ISO_8601) {
if (FORMAT === undefined) {
return compareDates(value1, value2, sortDirection, FORMAT, false) as number;
}
return compareDates(value1, value2, sortDirection, FORMAT, true) as number;
Expand Down
2 changes: 1 addition & 1 deletion packages/custom-footer-component/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"dependencies": {
"@slickgrid-universal/binding": "workspace:~",
"@slickgrid-universal/common": "workspace:~",
"moment-mini": "^2.29.4"
"moment-tiny": "^2.30.3"
},
"devDependencies": {
"@slickgrid-universal/event-pub-sub": "workspace:~"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import moment from 'moment-mini';
import moment from 'moment-tiny';
import type {
CustomFooterOption,
GridOption,
Expand Down
2 changes: 1 addition & 1 deletion packages/excel-export/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"@slickgrid-universal/common": "workspace:~",
"@slickgrid-universal/utils": "workspace:~",
"excel-builder-vanilla": "^3.0.1",
"moment-mini": "^2.29.4"
"moment-tiny": "^2.30.3"
},
"devDependencies": {
"@slickgrid-universal/event-pub-sub": "workspace:~"
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"@slickgrid-universal/utils": "workspace:~"
},
"devDependencies": {
"moment-mini": "^2.29.4"
"moment-tiny": "^2.30.3"
},
"funding": {
"type": "ko_fi",
Expand Down
Loading
Loading