Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/lib/isAfter.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
import toDate from './toDate';

function resolveComparisonDate(options) {
if (options instanceof Date) {
return options;
}
if (typeof options === 'object' && options !== null) {
return options.comparisonDate;
}
return options;
}

export default function isAfter(date, options) {
// For backwards compatibility:
// isAfter(str [, date]), i.e. `options` could be used as argument for the legacy `date`
const comparisonDate = (typeof options === 'object' ? options.comparisonDate : options) || Date().toString();
const comparisonDate = resolveComparisonDate(options) || Date().toString();

const comparison = toDate(comparisonDate);
const comparison = comparisonDate instanceof Date
? comparisonDate
: toDate(comparisonDate);
const original = toDate(date);

return !!(original && comparison && original > comparison);
Expand Down
16 changes: 14 additions & 2 deletions src/lib/isBefore.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
import toDate from './toDate';

function resolveComparisonDate(options) {
if (options instanceof Date) {
return options;
}
if (typeof options === 'object' && options !== null) {
return options.comparisonDate;
}
return options;
}

export default function isBefore(date, options) {
// For backwards compatibility:
// isBefore(str [, date]), i.e. `options` could be used as argument for the legacy `date`
const comparisonDate = (typeof options === 'object' ? options.comparisonDate : options) || Date().toString();
const comparisonDate = resolveComparisonDate(options) || Date().toString();

const comparison = toDate(comparisonDate);
const comparison = comparisonDate instanceof Date
? comparisonDate
: toDate(comparisonDate);
const original = toDate(date);

return !!(original && comparison && original < comparison);
Expand Down
19 changes: 19 additions & 0 deletions test/validators/isAfter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,22 @@ describe('isAfter', () => {
});
});
});
describe('(legacy syntax with Date object)', () => {
it('should accept a Date object as the second argument and use it as the comparison date', () => {
// Regression: previously `typeof options === 'object' && options.comparisonDate === undefined`
// meant the Date argument was silently ignored and the comparison fell back to "now".
test({
validator: 'isAfter',
args: [new Date('2010-01-01T00:00:00Z')],
valid: ['2010-01-02', '2011-08-04', '2030-01-01', new Date(2020, 0, 1).toString()],
invalid: ['2009-12-31', '2010-01-01', new Date(0).toString()],
});

test({
validator: 'isAfter',
args: [new Date('2030-01-01T00:00:00Z')],
valid: ['2030-01-02', '2050-06-15'],
invalid: ['2025-06-26', '2029-12-31', new Date(2025, 0, 1).toString()],
});
});
});
19 changes: 19 additions & 0 deletions test/validators/isBefore.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,22 @@ describe('isBefore', () => {
});
});
});
describe('legacy syntax with Date object', () => {
it('should accept a Date object as the second argument and use it as the comparison date', () => {
// Regression: previously `typeof options === 'object' && options.comparisonDate === undefined`
// meant the Date argument was silently ignored and the comparison fell back to "now".
test({
validator: 'isBefore',
args: [new Date('2010-01-01T00:00:00Z')],
valid: ['2009-12-31', '1999-12-31', new Date(0).toString()],
invalid: ['2010-01-02', '2011-08-04', '2030-01-01'],
});

test({
validator: 'isBefore',
args: [new Date('2030-01-01T00:00:00Z')],
valid: ['2025-06-26', '2029-12-31', new Date(2025, 0, 1).toString()],
invalid: ['2030-01-02', '2050-06-15'],
});
});
});
Loading