Skip to content

Commit

Permalink
Make isAfter arguments more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelbucket-dev committed Feb 5, 2023
1 parent 84151bc commit 18d652b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/lib/isAfter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import toDate from './toDate';
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 = options?.comparisonDate || options || Date().toString();
const comparisonDate = (typeof options === 'object' ? options.comparisonDate : options) || Date().toString();

const comparison = toDate(comparisonDate);
const original = toDate(date);
Expand Down
15 changes: 15 additions & 0 deletions test/validators/isAfter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,21 @@ describe('isAfter', () => {
args: [{ comparisonDate: 'invalid date' }],
invalid: ['invalid date', '2015-09-17'],
});
test({
validator: 'isAfter',
args: [], // will fall back to the current date
valid: ['2100-08-04', new Date(Date.now() + 86400000).toString()],
});
test({
validator: 'isAfter',
args: [undefined], // will fall back to the current date
valid: ['2100-08-04', new Date(Date.now() + 86400000).toString()],
});
test({
validator: 'isAfter',
args: [{ comparisonDate: undefined }], // will fall back to the current date
valid: ['2100-08-04', new Date(Date.now() + 86400000).toString()],
});
});

describe('(legacy syntax)', () => {
Expand Down

0 comments on commit 18d652b

Please sign in to comment.