From fbb7636b393fcf3d79f662da06d82c115f2684c0 Mon Sep 17 00:00:00 2001 From: awais-ur-rehman Date: Mon, 2 Nov 2026 09:01:04 +0500 Subject: [PATCH 1/2] fix(parse): reject incomplete format matches --- src/isValid.ts | 3 +- src/preparse.ts | 9 +++++- tests/incomplete-format.spec.ts | 49 +++++++++++++++++++++++++++++++++ tests/preparse.spec.ts | 12 ++++---- 4 files changed, 65 insertions(+), 8 deletions(-) create mode 100644 tests/incomplete-format.spec.ts diff --git a/src/isValid.ts b/src/isValid.ts index 67481d5..7516833 100644 --- a/src/isValid.ts +++ b/src/isValid.ts @@ -48,7 +48,8 @@ export function validatePreparseResult(pr: PreparseResult, options?: ParserOptio const year = toGregorianYear(pr.Y, options) ?? base.Y; const month = pr.M ?? base.M; - return pr._index > 0 + return !pr._error + && pr._index > 0 && pr._length > 0 && pr._index === pr._length && pr._match > 0 diff --git a/src/preparse.ts b/src/preparse.ts index db56443..3604f0a 100644 --- a/src/preparse.ts +++ b/src/preparse.ts @@ -21,6 +21,11 @@ export interface PreparseResult extends ParsedComponents { * Number of matched tokens */ _match: number; + + /** + * True when a required format token could not be matched. + */ + _error?: true; } const wildcard = ' '; @@ -70,6 +75,7 @@ export function preparse(dateString: string, arg: string | CompiledObject, optio if (result) { if (!result.length) { + pr._error = true; break; } if (result.token && validateToken(result.token)) { @@ -77,7 +83,7 @@ export function preparse(dateString: string, arg: string | CompiledObject, optio } pr._index += result.length; pr._match++; - } else if (token === str[0] || token === wildcard) { + } else if (token === str[0] || token === wildcard && str.length > 0) { pr._index++; } else if (COMMENT.test(token) && !str.indexOf(token.replace(COMMENT, '$1'))) { pr._index += token.length - 2; @@ -85,6 +91,7 @@ export function preparse(dateString: string, arg: string | CompiledObject, optio pr._index = dateString.length; break; } else { + pr._error = true; break; } } diff --git a/tests/incomplete-format.spec.ts b/tests/incomplete-format.spec.ts new file mode 100644 index 0000000..00ead39 --- /dev/null +++ b/tests/incomplete-format.spec.ts @@ -0,0 +1,49 @@ +import { describe, expect, test } from 'vitest'; +import { compile, isValid, parse, preparse } from '@/index.ts'; +import { parser } from '@/plugins/day-of-week.ts'; + +describe('incomplete format matching', () => { + test.each([ + ['2025', 'YYYY-MM-DD'], + ['2025-', 'YYYY-MM-DD'], + ['2025-01', 'YYYY-MM-DD'], + ['2025-01-', 'YYYY-MM-DD'], + ['2025-01-01', 'YYYY-MM-DD HH:mm'], + ['2025', 'YYYY[ required]'], + ['2025', 'YYYY '], + ['2025', 'YYYY ...'], + ['2025', 'YYYY ...'], + ['2025 ', 'YYYY ...'] + ])('rejects %s when %s still requires input', (input, format) => { + for (const pattern of [format, compile(format)]) { + expect(isValid(input, pattern)).toBe(false); + expect(Number.isNaN(parse(input, pattern).getTime())).toBe(true); + } + }); + + test('rejects an absent plugin token', () => { + const options = { plugins: [parser] }; + expect(isValid('2025-01-01 ', 'YYYY-MM-DD dddd', options)).toBe(false); + expect(Number.isNaN(parse('2025-01-01 ', 'YYYY-MM-DD dddd', options).getTime())).toBe(true); + }); + + test('preserves parsed components when matching fails', () => { + expect(preparse('2025-01', 'YYYY-MM-DD')).toMatchObject({ + Y: 2025, M: 1, _error: true, _index: 7, _length: 7, _match: 2 + }); + }); + + test.each([ + ['2025', 'YYYY'], + ['2025-01', 'YYYY-MM'], + ['2025-01-01', 'YYYY-MM-DD'], + ['2025', 'YYYY...'], + ['2025 additional text', 'YYYY...'], + ['2025 ', 'YYYY '], + ['2025 ', 'YYYY ...'], + ['2025 ', 'YYYY ...'] + ])('accepts %s when %s is fully matched', (input, pattern) => { + expect(isValid(input, pattern)).toBe(true); + expect(Number.isNaN(parse(input, pattern).getTime())).toBe(false); + }); +}); diff --git a/tests/preparse.spec.ts b/tests/preparse.spec.ts index 7d9be04..c9813ac 100644 --- a/tests/preparse.spec.ts +++ b/tests/preparse.spec.ts @@ -130,7 +130,7 @@ describe('MMMM', () => { }); test('Zero', () => { - const dt = { _index: 0, _length: 4, _match: 0 }; + const dt = { _error: true, _index: 0, _length: 4, _match: 0 }; expect(preparse('Zero', 'MMMM')).toEqual(dt); }); }); @@ -147,7 +147,7 @@ describe('MMM', () => { }); test('Zero', () => { - const dt = { _index: 0, _length: 4, _match: 0 }; + const dt = { _error: true, _index: 0, _length: 4, _match: 0 }; expect(preparse('Zero', 'MMM')).toEqual(dt); }); }); @@ -300,7 +300,7 @@ describe('AA', () => { }); test('M.M.', () => { - const dt = { _index: 0, _length: 4, _match: 0 }; + const dt = { _error: true, _index: 0, _length: 4, _match: 0 }; expect(preparse('M.M.', 'AA')).toEqual(dt); }); }); @@ -317,7 +317,7 @@ describe('A', () => { }); test('MM', () => { - const dt = { _index: 0, _length: 2, _match: 0 }; + const dt = { _error: true, _index: 0, _length: 2, _match: 0 }; expect(preparse('MM', 'A')).toEqual(dt); }); }); @@ -334,7 +334,7 @@ describe('aa', () => { }); test('m.m.', () => { - const dt = { _index: 0, _length: 4, _match: 0 }; + const dt = { _error: true, _index: 0, _length: 4, _match: 0 }; expect(preparse('m.m.', 'aa')).toEqual(dt); }); }); @@ -351,7 +351,7 @@ describe('a', () => { }); test('mm', () => { - const dt = { _index: 0, _length: 2, _match: 0 }; + const dt = { _error: true, _index: 0, _length: 2, _match: 0 }; expect(preparse('mm', 'a')).toEqual(dt); }); }); From 338d227decf63a753089fdfdd6bddcef1eb07ecd Mon Sep 17 00:00:00 2001 From: knowledgecode Date: Sat, 28 Nov 2026 11:49:48 +0900 Subject: [PATCH 2/2] fix(parse): rename the mismatch flag and set it in one place - Rename PreparseResult._error to _mismatch, since preparse() records a matching result rather than deciding validity - Match the tokens up to the first ellipsis with find(), so the first token that fails to match sets the flag, from one place - Advance _index for a wildcard past the end of the input as before, so callers comparing _index with _length keep rejecting it - Move the tests into the preparse, isValid, and parse specs --- src/isValid.ts | 2 +- src/preparse.ts | 40 +++++++++++++++++---------- tests/incomplete-format.spec.ts | 49 --------------------------------- tests/isValid.spec.ts | 36 ++++++++++++++++++++++++ tests/parse.spec.ts | 38 ++++++++++++++++++++++++- tests/preparse.spec.ts | 29 +++++++++++++++---- 6 files changed, 122 insertions(+), 72 deletions(-) delete mode 100644 tests/incomplete-format.spec.ts diff --git a/src/isValid.ts b/src/isValid.ts index 7516833..a241f63 100644 --- a/src/isValid.ts +++ b/src/isValid.ts @@ -48,7 +48,7 @@ export function validatePreparseResult(pr: PreparseResult, options?: ParserOptio const year = toGregorianYear(pr.Y, options) ?? base.Y; const month = pr.M ?? base.M; - return !pr._error + return !pr._mismatch && pr._index > 0 && pr._length > 0 && pr._index === pr._length diff --git a/src/preparse.ts b/src/preparse.ts index 3604f0a..07153ca 100644 --- a/src/preparse.ts +++ b/src/preparse.ts @@ -23,9 +23,9 @@ export interface PreparseResult extends ParsedComponents { _match: number; /** - * True when a required format token could not be matched. + * Set when the date string did not match the whole pattern */ - _error?: true; + _mismatch?: true; } const wildcard = ' '; @@ -67,33 +67,43 @@ export function preparse(dateString: string, arg: string | CompiledObject, optio return undefined; }; - dateString = parserOptions.numeral.decode(dateString); - - for (const token of pattern) { + // Consumes one token from the current position and returns whether it matched. + const matchToken = (token: string) => { const str = dateString.substring(pr._index); const result = resolveToken(token, str); if (result) { if (!result.length) { - pr._error = true; - break; + return false; } if (result.token && validateToken(result.token)) { pr[result.token] = result.value + 0; } pr._index += result.length; pr._match++; - } else if (token === str[0] || token === wildcard && str.length > 0) { + return true; + } + if (token === str[0] || token === wildcard) { + // A wildcard past the end still advances, so _index exceeds _length. pr._index++; - } else if (COMMENT.test(token) && !str.indexOf(token.replace(COMMENT, '$1'))) { + return str.length > 0; + } + if (COMMENT.test(token) && !str.indexOf(token.replace(COMMENT, '$1'))) { pr._index += token.length - 2; - } else if (token === ellipsis) { - pr._index = dateString.length; - break; - } else { - pr._error = true; - break; + return true; } + return false; + }; + + dateString = parserOptions.numeral.decode(dateString); + + // Tokens are matched up to the first ellipsis, which then matches the rest. + const stop = pattern.find(token => token === ellipsis || !matchToken(token)); + + if (stop === ellipsis) { + pr._index = dateString.length; + } else if (stop !== undefined) { + pr._mismatch = true; } pr._length = dateString.length; // Breaking change: 12-hour to 24-hour conversion is no longer performed here; values are preserved as read. diff --git a/tests/incomplete-format.spec.ts b/tests/incomplete-format.spec.ts deleted file mode 100644 index 00ead39..0000000 --- a/tests/incomplete-format.spec.ts +++ /dev/null @@ -1,49 +0,0 @@ -import { describe, expect, test } from 'vitest'; -import { compile, isValid, parse, preparse } from '@/index.ts'; -import { parser } from '@/plugins/day-of-week.ts'; - -describe('incomplete format matching', () => { - test.each([ - ['2025', 'YYYY-MM-DD'], - ['2025-', 'YYYY-MM-DD'], - ['2025-01', 'YYYY-MM-DD'], - ['2025-01-', 'YYYY-MM-DD'], - ['2025-01-01', 'YYYY-MM-DD HH:mm'], - ['2025', 'YYYY[ required]'], - ['2025', 'YYYY '], - ['2025', 'YYYY ...'], - ['2025', 'YYYY ...'], - ['2025 ', 'YYYY ...'] - ])('rejects %s when %s still requires input', (input, format) => { - for (const pattern of [format, compile(format)]) { - expect(isValid(input, pattern)).toBe(false); - expect(Number.isNaN(parse(input, pattern).getTime())).toBe(true); - } - }); - - test('rejects an absent plugin token', () => { - const options = { plugins: [parser] }; - expect(isValid('2025-01-01 ', 'YYYY-MM-DD dddd', options)).toBe(false); - expect(Number.isNaN(parse('2025-01-01 ', 'YYYY-MM-DD dddd', options).getTime())).toBe(true); - }); - - test('preserves parsed components when matching fails', () => { - expect(preparse('2025-01', 'YYYY-MM-DD')).toMatchObject({ - Y: 2025, M: 1, _error: true, _index: 7, _length: 7, _match: 2 - }); - }); - - test.each([ - ['2025', 'YYYY'], - ['2025-01', 'YYYY-MM'], - ['2025-01-01', 'YYYY-MM-DD'], - ['2025', 'YYYY...'], - ['2025 additional text', 'YYYY...'], - ['2025 ', 'YYYY '], - ['2025 ', 'YYYY ...'], - ['2025 ', 'YYYY ...'] - ])('accepts %s when %s is fully matched', (input, pattern) => { - expect(isValid(input, pattern)).toBe(true); - expect(Number.isNaN(parse(input, pattern).getTime())).toBe(false); - }); -}); diff --git a/tests/isValid.spec.ts b/tests/isValid.spec.ts index c45d3f0..5a8efcf 100644 --- a/tests/isValid.spec.ts +++ b/tests/isValid.spec.ts @@ -89,6 +89,42 @@ test('20150101235959 is invalid', () => { expect(isValid('20150101235959', compile('20150101235959'))).toBe(false); }); +describe('incomplete input', () => { + test.each([ + ['2025', 'YYYY-MM-DD'], + ['2025-', 'YYYY-MM-DD'], + ['2025-01', 'YYYY-MM-DD'], + ['2025-01-', 'YYYY-MM-DD'], + ['2025-01-01', 'YYYY-MM-DD HH:mm'], + ['2025', 'YYYY[ required]'], + ['2025', 'YYYY '], + ['2025', 'YYYY ...'], + ['2025', 'YYYY ...'], + ['2025 ', 'YYYY ...'] + ])('%s does not complete %s', (dateString, formatString) => { + expect(isValid(dateString, formatString)).toBe(false); + expect(isValid(dateString, compile(formatString))).toBe(false); + }); + + test('a missing plugin token is invalid', () => { + expect(isValid('2025-01-01 ', 'YYYY-MM-DD dddd', { plugins: [parser] })).toBe(false); + }); + + test.each([ + ['2025', 'YYYY'], + ['2025-01', 'YYYY-MM'], + ['2025-01-01', 'YYYY-MM-DD'], + ['2025', 'YYYY...'], + ['2025 additional text', 'YYYY...'], + ['2025 ', 'YYYY '], + ['2025 ', 'YYYY ...'], + ['2025 ', 'YYYY ...'] + ])('%s completes %s', (dateString, formatString) => { + expect(isValid(dateString, formatString)).toBe(true); + expect(isValid(dateString, compile(formatString))).toBe(true); + }); +}); + describe('options', () => { test('hour12: h11', () => { expect(isValid('2000-01-01 00:00 AM', 'YYYY-MM-DD hh:mm A', { hour12: 'h11' })).toBe(true); diff --git a/tests/parse.spec.ts b/tests/parse.spec.ts index 0478231..c08bdc9 100644 --- a/tests/parse.spec.ts +++ b/tests/parse.spec.ts @@ -1,5 +1,5 @@ import { describe, expect, test, beforeAll } from 'vitest'; -import { parse } from '@/index.ts'; +import { compile, parse } from '@/index.ts'; import { parser } from '@/plugins/day-of-week.ts'; import Los_Angeles from '@/timezones/America/Los_Angeles.ts'; import Tokyo from '@/timezones/Asia/Tokyo.ts'; @@ -504,6 +504,42 @@ test('\\[YYYY-MM-DD HH:mm:ss\\]', () => { expect(parse('[2025-08-23 14:30:45]', '\\[YYYY-MM-DD HH:mm:ss\\]')).toEqual(now); }); +describe('incomplete input', () => { + test.each([ + ['2025', 'YYYY-MM-DD'], + ['2025-', 'YYYY-MM-DD'], + ['2025-01', 'YYYY-MM-DD'], + ['2025-01-', 'YYYY-MM-DD'], + ['2025-01-01', 'YYYY-MM-DD HH:mm'], + ['2025', 'YYYY[ required]'], + ['2025', 'YYYY '], + ['2025', 'YYYY ...'], + ['2025', 'YYYY ...'], + ['2025 ', 'YYYY ...'] + ])('%s does not complete %s', (dateString, formatString) => { + expect(Number.isNaN(parse(dateString, formatString).getTime())).toBe(true); + expect(Number.isNaN(parse(dateString, compile(formatString)).getTime())).toBe(true); + }); + + test('a missing plugin token yields Invalid Date', () => { + expect(Number.isNaN(parse('2025-01-01 ', 'YYYY-MM-DD dddd', { plugins: [parser] }).getTime())).toBe(true); + }); + + test.each([ + ['2025', 'YYYY'], + ['2025-01', 'YYYY-MM'], + ['2025-01-01', 'YYYY-MM-DD'], + ['2025', 'YYYY...'], + ['2025 additional text', 'YYYY...'], + ['2025 ', 'YYYY '], + ['2025 ', 'YYYY ...'], + ['2025 ', 'YYYY ...'] + ])('%s completes %s', (dateString, formatString) => { + expect(Number.isNaN(parse(dateString, formatString).getTime())).toBe(false); + expect(Number.isNaN(parse(dateString, compile(formatString)).getTime())).toBe(false); + }); +}); + describe('options', () => { test('hour12: h11', () => { expect(parse('2000-01-01 00:00 AM', 'YYYY-MM-DD hh:mm A', { hour12: 'h11' })).toEqual(new Date(2000, 0, 1, 0, 0)); diff --git a/tests/preparse.spec.ts b/tests/preparse.spec.ts index c9813ac..e64592d 100644 --- a/tests/preparse.spec.ts +++ b/tests/preparse.spec.ts @@ -130,7 +130,7 @@ describe('MMMM', () => { }); test('Zero', () => { - const dt = { _error: true, _index: 0, _length: 4, _match: 0 }; + const dt = { _index: 0, _length: 4, _match: 0, _mismatch: true }; expect(preparse('Zero', 'MMMM')).toEqual(dt); }); }); @@ -147,7 +147,7 @@ describe('MMM', () => { }); test('Zero', () => { - const dt = { _error: true, _index: 0, _length: 4, _match: 0 }; + const dt = { _index: 0, _length: 4, _match: 0, _mismatch: true }; expect(preparse('Zero', 'MMM')).toEqual(dt); }); }); @@ -300,7 +300,7 @@ describe('AA', () => { }); test('M.M.', () => { - const dt = { _error: true, _index: 0, _length: 4, _match: 0 }; + const dt = { _index: 0, _length: 4, _match: 0, _mismatch: true }; expect(preparse('M.M.', 'AA')).toEqual(dt); }); }); @@ -317,7 +317,7 @@ describe('A', () => { }); test('MM', () => { - const dt = { _error: true, _index: 0, _length: 2, _match: 0 }; + const dt = { _index: 0, _length: 2, _match: 0, _mismatch: true }; expect(preparse('MM', 'A')).toEqual(dt); }); }); @@ -334,7 +334,7 @@ describe('aa', () => { }); test('m.m.', () => { - const dt = { _error: true, _index: 0, _length: 4, _match: 0 }; + const dt = { _index: 0, _length: 4, _match: 0, _mismatch: true }; expect(preparse('m.m.', 'aa')).toEqual(dt); }); }); @@ -351,7 +351,7 @@ describe('a', () => { }); test('mm', () => { - const dt = { _error: true, _index: 0, _length: 2, _match: 0 }; + const dt = { _index: 0, _length: 2, _match: 0, _mismatch: true }; expect(preparse('mm', 'a')).toEqual(dt); }); }); @@ -516,6 +516,23 @@ describe('special tokens', () => { }); }); +describe('incomplete input', () => { + test('keeps the components parsed before the mismatch', () => { + const dt = { Y: 2025, M: 1, _index: 7, _length: 7, _match: 2, _mismatch: true }; + expect(preparse('2025-01', 'YYYY-MM-DD')).toEqual(dt); + }); + + test('advances past the end for a trailing wildcard', () => { + const dt = { Y: 2025, M: 1, D: 1, _index: 11, _length: 10, _match: 3, _mismatch: true }; + expect(preparse('2025-01-01', 'YYYY-MM-DD ')).toEqual(dt); + }); + + test('stops at an ellipsis without a mismatch', () => { + const dt = { Y: 2025, _index: 4, _length: 4, _match: 1 }; + expect(preparse('2025', 'YYYY...')).toEqual(dt); + }); +}); + describe('options', () => { test('hour12: h11', () => { const dt1 = { Y: 2000, M: 1, D: 1, A: 0, h: 0, m: 0, _index: 19, _length: 19, _match: 6 };