Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/isValid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 28 additions & 11 deletions src/preparse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ' ';
Expand Down Expand Up @@ -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.
Expand Down
36 changes: 36 additions & 0 deletions tests/isValid.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
38 changes: 37 additions & 1 deletion tests/parse.spec.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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));
Expand Down
29 changes: 23 additions & 6 deletions tests/preparse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
Expand All @@ -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);
});
});
Expand Down Expand Up @@ -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);
});
});
Expand All @@ -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);
});
});
Expand All @@ -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);
});
});
Expand All @@ -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);
});
});
Expand Down Expand Up @@ -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 };
Expand Down