diff --git a/src/isValid.ts b/src/isValid.ts index 67481d5..a241f63 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._mismatch + && 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..07153ca 100644 --- a/src/preparse.ts +++ b/src/preparse.ts @@ -21,6 +21,11 @@ export interface PreparseResult extends ParsedComponents { * Number of matched tokens */ _match: number; + + /** + * Set when the date string did not match the whole pattern + */ + _mismatch?: true; } const wildcard = ' '; @@ -62,31 +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) { - 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) { + 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 { - 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/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 7d9be04..e64592d 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 = { _index: 0, _length: 4, _match: 0, _mismatch: true }; expect(preparse('Zero', 'MMMM')).toEqual(dt); }); }); @@ -147,7 +147,7 @@ describe('MMM', () => { }); test('Zero', () => { - const dt = { _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 = { _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 = { _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 = { _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 = { _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 };