Skip to content

Commit

Permalink
Add exact digits patterns (date-fns#916)
Browse files Browse the repository at this point in the history
  • Loading branch information
yesl-kim committed Mar 23, 2024
1 parent 5c1adb5 commit 676bfa2
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 8 deletions.
4 changes: 4 additions & 0 deletions src/parse/_lib/constants.ts
Expand Up @@ -15,6 +15,10 @@ export const numericPatterns = {
threeDigits: /^\d{1,3}/, // 0 to 999
fourDigits: /^\d{1,4}/, // 0 to 9999

exactTwoDigits: /^\d{2}/, // 00 to 99
exactThreeDigits: /^\d{3}/, // 000 to 999
exactFourDigits: /^\d{4}/, // 0000 to 9999

anyDigitsSigned: /^-?\d+/,
singleDigitSigned: /^-?\d/, // 0 to 9, -0 to -9
twoDigitsSigned: /^-?\d{1,2}/, // 0 to 99, -0 to -99
Expand Down
4 changes: 2 additions & 2 deletions src/parse/_lib/parsers/Hour0to23Parser.ts
Expand Up @@ -2,7 +2,7 @@ import type { Match } from "../../../locale/types.js";
import { numericPatterns } from "../constants.js";
import { Parser } from "../Parser.js";
import type { ParseFlags, ParseResult } from "../types.js";
import { parseNDigits, parseNumericPattern } from "../utils.js";
import { parseNumericPattern } from "../utils.js";

export class Hour0to23Parser extends Parser<number> {
priority = 70;
Expand All @@ -14,7 +14,7 @@ export class Hour0to23Parser extends Parser<number> {
case "Ho":
return match.ordinalNumber(dateString, { unit: "hour" });
default:
return parseNDigits(token.length, dateString);
return parseNumericPattern(numericPatterns.exactTwoDigits, dateString);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/parse/_lib/parsers/Hour1to12Parser.ts
Expand Up @@ -2,7 +2,7 @@ import type { Match } from "../../../locale/types.js";
import { numericPatterns } from "../constants.js";
import { Parser } from "../Parser.js";
import type { ParseFlags, ParseResult } from "../types.js";
import { parseNDigits, parseNumericPattern } from "../utils.js";
import { parseNumericPattern } from "../utils.js";

export class Hour1to12Parser extends Parser<number> {
priority = 70;
Expand All @@ -14,7 +14,7 @@ export class Hour1to12Parser extends Parser<number> {
case "ho":
return match.ordinalNumber(dateString, { unit: "hour" });
default:
return parseNDigits(token.length, dateString);
return parseNumericPattern(numericPatterns.exactTwoDigits, dateString);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/parse/_lib/parsers/MinuteParser.ts
Expand Up @@ -2,7 +2,7 @@ import type { Match } from "../../../locale/types.js";
import { numericPatterns } from "../constants.js";
import { Parser } from "../Parser.js";
import type { ParseFlags, ParseResult } from "../types.js";
import { parseNDigits, parseNumericPattern } from "../utils.js";
import { parseNumericPattern } from "../utils.js";

export class MinuteParser extends Parser<number> {
priority = 60;
Expand All @@ -14,7 +14,7 @@ export class MinuteParser extends Parser<number> {
case "mo":
return match.ordinalNumber(dateString, { unit: "minute" });
default:
return parseNDigits(token.length, dateString);
return parseNumericPattern(numericPatterns.exactTwoDigits, dateString);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/parse/_lib/parsers/SecondParser.ts
Expand Up @@ -2,7 +2,7 @@ import type { Match } from "../../../locale/types.js";
import { numericPatterns } from "../constants.js";
import { Parser } from "../Parser.js";
import type { ParseFlags, ParseResult } from "../types.js";
import { parseNDigits, parseNumericPattern } from "../utils.js";
import { parseNumericPattern } from "../utils.js";

export class SecondParser extends Parser<number> {
priority = 50;
Expand All @@ -14,7 +14,7 @@ export class SecondParser extends Parser<number> {
case "so":
return match.ordinalNumber(dateString, { unit: "second" });
default:
return parseNDigits(token.length, dateString);
return parseNumericPattern(numericPatterns.exactTwoDigits, dateString);
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/parse/test.ts
Expand Up @@ -2259,6 +2259,13 @@ describe("parse", () => {
expect(result instanceof Date && isNaN(result.getTime())).toBe(true);
});

it("H, h, m, s parsers returns Invalid Date if the digits of `formatString` does not match exactly that of the `datestring`", () => {
["HH", "hh", "mm", "ss"].forEach((token) => {
const result = parse("1", token, referenceDate);
assert(result instanceof Date && isNaN(result.getTime()));
});
});

it("parses normally if the remaining input is just whitespace", () => {
const result = parse("2016-11-05 \n", "yyyy-MM-dd", referenceDate);
expect(result).toEqual(new Date(2016, 10 /* Nov */, 5));
Expand Down

0 comments on commit 676bfa2

Please sign in to comment.