Skip to content

Commit

Permalink
Korean dates tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kensnyder committed Sep 16, 2024
1 parent 4078022 commit f95fdb3
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 59 deletions.
2 changes: 1 addition & 1 deletion src/Format/Format.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('Format', () => {
it('should build RegExp from template', () => {
const format = new Format({ handler: () => {}, template: 'year:_YEAR_' });
const regex = format.getRegExp();
expect(regex).toEqual(/year:\d{4}|\d{2}/i);
expect(regex).toEqual(/year:\d{4}|\d{2}|-\d{6}|\+?\d{5,6}/i);
});
it('should getMatches()', () => {
const format = new Format({ handler: () => {}, matcher: /foo:(\d)(\d)/ });
Expand Down
8 changes: 5 additions & 3 deletions src/data/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ export const latn = {
ZONE: '\\(?(' + Object.keys(timezoneNames).join('|') + ')\\)?',
MERIDIEM: '(am|pm|a.m.|p.m.)',
ORDINAL: 'st|nd|rd|th|\\.',
YEAR: '\\d{4}|\\d{2}',
YEAR: '\\d{4}|\\d{2}|-\\d{6}|\\+?\\d{5,6}',
YEAR4: '\\d{4}',
YEAR6: '-\\d{6}|\\+?\\d{5,6}',
MONTH: '1[0-2]|0?[1-9]',
MONTH2: '1[0-2]|0[1-9]',
DAY: '3[01]|[12]\\d|0?[1-9]',
Expand All @@ -26,8 +27,9 @@ export const latn = {

export const other = {
...latn,
YEAR: '*{4}|*{2}',
YEAR4: '*{4}|*{2}',
YEAR: '*{4}|*{2}|-*{6}|\\+?*{5,6}',
YEAR4: '*{4}',
YEAR6: '-*{6}|\\+?*{5,6}',
MONTH: '*{1,2}',
MONTH2: '*{2}',
DAY: '*{1,2}',
Expand Down
38 changes: 7 additions & 31 deletions src/formats/dayMonthnameYear/dayMonthnameYear.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { expect, it } from 'vitest';
import parser from '../../../index';
import localeList from '../../../test-fixtures/localeList';
import testDates from '../../../test-fixtures/testDates';

Expand Down Expand Up @@ -39,34 +41,8 @@ testDates({
formats: ["dd'th' MMM yy", "d'th' MMM yy", "dd'th'-MMM-yy", "d'th'-MMM-yy"],
});

// describe('dayMonthnameYear', () => {
// it('should handle GitHub issue #11', () => {
// function dateAsISO(dateValue: string | Date) {
// if (!dateValue) {
// return null;
// }
//
// let date = null;
//
// // if it's already a date, just use that.
// if (dateValue instanceof Date) {
// date = dateValue;
// } else if (typeof dateValue === 'string') {
// date = parser.fromString(dateValue);
// if (date.invalid) {
// return null;
// }
// }
//
// try {
// return date.toISOString();
// } catch (error) {
// // maybe RangeError
// return null;
// }
// }
// expect(dateAsISO('Fri, 19 Nov 2021 02:07:18 +0000')).toBe(
// '2021-11-19T02:07:18.000Z'
// );
// });
// });
it('should handle GitHub issue #11', () => {
const nov19 = parser.fromString('Fri, 19 Nov 2021 02:07:18 +0000');
const nov19ISO = nov19 instanceof Date ? nov19.toISOString() : nov19.invalid;
expect(nov19ISO).toEqual('2021-11-19T02:07:18.000Z');
});
23 changes: 23 additions & 0 deletions src/formats/korean/korean.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { describe, expect, it } from 'vitest';
import parser from '../../../index';

describe('korean', () => {
it('should handle no gaps', () => {
const actual = parser.attempt('2024년09월16일');
const expected = {
year: 2024,
month: 9,
day: 16,
};
expect(actual).toMatchObject(expected);
});
it('should handle spaces', () => {
const actual = parser.attempt('2024년 09월 16일');
const expected = {
year: 2024,
month: 9,
day: 16,
};
expect(actual).toMatchObject(expected);
});
});
5 changes: 2 additions & 3 deletions src/formats/korean/korean.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import Format from '../../Format/Format';

const korean = new Format({
/* prettier-ignore */
// $1 $2 $3
template: `^(_YEAR_)년(?:_GAP_)?(_MONTH_)월(?:_GAP_)?(_DAY_)일(?:_GAP_)?(?:_DAYNAME_)?$`,
// $1 $2 $3
template: `^(_YEAR_)년\\s*(_MONTH_)월\\s*(_DAY_)일`,
units: ['year', 'month', 'day'],
});

Expand Down
38 changes: 20 additions & 18 deletions src/fromString/fromString.spec.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,15 @@
import { describe, expect, it, vi } from 'vitest';
import parser from '../../index';
import type Parser from '../Parser/Parser';
import fromString from './fromString';

const now = new Date();
describe('fromString', () => {
describe('fromString with spies', () => {
it('should return invalid dates', () => {
const invalid = { invalid: 'foo' };
const parser = { attempt: vi.fn(() => invalid) } as unknown as Parser;
const fromFn = fromString(parser);
expect(fromFn('')).toEqual(invalid);
});
it('should use current month and day', () => {
const result = { year: 2020 };
const parser = { attempt: vi.fn(() => result) } as unknown as Parser;
const fromFn = fromString(parser);
const jan1 = new Date(
Date.UTC(2020, now.getUTCMonth(), now.getUTCDate(), 0, 0, 0, 0)
);
expect(fromFn('')).toEqual(jan1);
});
it('should use current month', () => {
const result = { year: 2020, month: 5 };
const parser = { attempt: vi.fn(() => result) } as unknown as Parser;
const fromFn = fromString(parser);
const may1 = new Date(Date.UTC(2020, 4, now.getUTCDate(), 0, 0, 0, 0));
expect(fromFn('')).toEqual(may1);
});
it('should reset all but day', () => {
const result = { year: 2020, month: 4, day: 15 };
const parser = { attempt: vi.fn(() => result) } as unknown as Parser;
Expand Down Expand Up @@ -109,3 +93,21 @@ describe('fromString', () => {
expect(fromFn('')).toEqual(oct31);
});
});

describe('fromString with out-of-range values', () => {
it('should roll over dates like JS', () => {
const result = parser.fromString('31 feb 2020');
expect(result).toBeInstanceOf(Date);
// @ts-ignore If it isn't a date, this test will exit by now
expect(result.toISOString()).toEqual('2020-03-02T00:00:00.000Z');
});
});

describe('fromString with 5-plus-digit years', () => {
it('should roll over dates like JS', () => {
const result = parser.fromString('12024-01-01');
expect(result).toBeInstanceOf(Date);
// @ts-ignore If it isn't a date, this test will exit by now
expect(result.getFullYear()).toEqual(12024);
});
});
6 changes: 3 additions & 3 deletions test-fixtures/dateStyles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ const date = new Date();
const results = [date.toJSON()];
const today = toMDY(date);
console.log({ today });
const dateStyles = ['full', 'long', 'medium', 'short'];
const timeStyles = ['full', 'long', 'medium', 'short'];
const dayPeriods = ['narrow', 'short', 'long'];
const dateStyles = ['full', 'long', 'medium', 'short'] as const;
const timeStyles = ['full', 'long', 'medium', 'short'] as const;
const dayPeriods = ['narrow', 'short', 'long'] as const;
let i = 0;
let found = 0;
for (const locale of localeList) {
Expand Down

0 comments on commit f95fdb3

Please sign in to comment.