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

3763 parse iso fix #3780

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/parseISO/index.ts
Expand Up @@ -136,6 +136,9 @@ function splitDateString(dateString: string): DateString {
timeString = array[0];
} else {
dateStrings.date = array[0];
const date = array[0];
if (date.length < 4) dateStrings.date = addLeadingZeros(date);
else dateStrings.date = array[0];
timeString = array[1];
if (patterns.timeZoneDelimiter.test(dateStrings.date)) {
dateStrings.date = dateString.split(patterns.timeZoneDelimiter)[0];
Expand All @@ -162,10 +165,10 @@ function splitDateString(dateString: string): DateString {
function parseYear(dateString: string, additionalDigits: number): ParsedYear {
const regex = new RegExp(
"^(?:(\\d{4}|[+-]\\d{" +
(4 + additionalDigits) +
"})|(\\d{2}|[+-]\\d{" +
(2 + additionalDigits) +
"})$)",
(4 + additionalDigits) +
"})|(\\d{2}|[+-]\\d{" +
(2 + additionalDigits) +
"})$)",
);

const captures = dateString.match(regex);
Expand Down Expand Up @@ -318,3 +321,12 @@ function validateTime(
function validateTimezone(_hours: number, minutes: number): boolean {
return minutes >= 0 && minutes <= 59;
}

function addLeadingZeros(year: string) {
const zerosToAdd = 4 - year.length;

for (let i = 0; i < zerosToAdd; i++) {
year = '0' + year;
}
return year;
}
Copy link
Member

Choose a reason for hiding this comment

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

Please use addLeadingZeros from src/_lib/addLeadingZero/index.ts

11 changes: 4 additions & 7 deletions src/parseISO/test.ts
Expand Up @@ -3,10 +3,10 @@

describe("parseISO", () => {
describe("string argument", () => {
describe("centuries", () => {
describe("years", () => {
it("parses YY", () => {
const result = parseISO("20");
expect(result).toEqual(new Date(2000, 0 /* Jan */, 1));
expect(result.getUTCFullYear()).toEqual(19);

Check failure on line 9 in src/parseISO/test.ts

View workflow job for this annotation

GitHub Actions / tests

src/parseISO/test.ts > parseISO > string argument > years > parses YY

AssertionError: expected 20 to deeply equal 19 - Expected + Received - 19 + 20 ❯ src/parseISO/test.ts:9:40

Check failure on line 9 in src/parseISO/test.ts

View workflow job for this annotation

GitHub Actions / tests (18)

src/parseISO/test.ts > parseISO > string argument > years > parses YY

AssertionError: expected 20 to deeply equal 19 - Expected + Received - 19 + 20 ❯ src/parseISO/test.ts:9:41

Check failure on line 9 in src/parseISO/test.ts

View workflow job for this annotation

GitHub Actions / tests (20)

src/parseISO/test.ts > parseISO > string argument > years > parses YY

AssertionError: expected 20 to deeply equal 19 - Expected + Received - 19 + 20 ❯ src/parseISO/test.ts:9:41
Copy link
Member

Choose a reason for hiding this comment

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

Please compare with a full date like before, as it prevents other date components from being parsed incorrectly.

});
});

Expand Down Expand Up @@ -112,11 +112,8 @@
});

it("allows to specify the number of additional digits", () => {
const result = parseISO("-20", { additionalDigits: 0 });
const date = new Date(0);
date.setFullYear(-2000, 0 /* Jan */, 1);
date.setHours(0, 0, 0, 0);
expect(result).toEqual(date);
const result = parseISO("20", { additionalDigits: 0 });
Copy link
Member

Choose a reason for hiding this comment

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

I prefer to keep the tests as is and update/add only what is required. Here, it needs to be clarified how these changes relate to the PR goal.

expect(result.getUTCFullYear()).toEqual(19);

Check failure on line 116 in src/parseISO/test.ts

View workflow job for this annotation

GitHub Actions / tests

src/parseISO/test.ts > parseISO > string argument > extended century representation > allows to specify the number of additional digits

AssertionError: expected 20 to deeply equal 19 - Expected + Received - 19 + 20 ❯ src/parseISO/test.ts:116:40

Check failure on line 116 in src/parseISO/test.ts

View workflow job for this annotation

GitHub Actions / tests (18)

src/parseISO/test.ts > parseISO > string argument > extended century representation > allows to specify the number of additional digits

AssertionError: expected 20 to deeply equal 19 - Expected + Received - 19 + 20 ❯ src/parseISO/test.ts:116:41

Check failure on line 116 in src/parseISO/test.ts

View workflow job for this annotation

GitHub Actions / tests (20)

src/parseISO/test.ts > parseISO > string argument > extended century representation > allows to specify the number of additional digits

AssertionError: expected 20 to deeply equal 19 - Expected + Received - 19 + 20 ❯ src/parseISO/test.ts:116:41
});
});

Expand Down