Skip to content

Commit 753c29d

Browse files
authored
feat(isAfter): allow usage of options object (#2075)
* refactor: change test files to prepare for #1874 * feat(isAfter): allow usage of options object * refactor: improve comparisonDate * refactor: undo breaking change to toDate
1 parent b2a999d commit 753c29d

File tree

5 files changed

+70
-31
lines changed

5 files changed

+70
-31
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ Validator | Description
9090
--------------------------------------- | --------------------------------------
9191
**contains(str, seed [, options])** | check if the string contains the seed.<br/><br/>`options` is an object that defaults to `{ ignoreCase: false, minOccurrences: 1 }`.<br />Options: <br/> `ignoreCase`: Ignore case when doing comparison, default false.<br/>`minOccurences`: Minimum number of occurrences for the seed in the string. Defaults to 1.
9292
**equals(str, comparison)** | check if the string matches the comparison.
93-
**isAfter(str [, date])** | check if the string is a date that is after the specified date (defaults to now).
93+
**isAfter(str [, options])** | check if the string is a date that is after the specified date.<br/><br/>`options` is an object that defaults to `{ comparisonDate: Date().toString() }`.<br/>**Options:**<br/>`comparisonDate`: Date to compare to. Defaults to `Date().toString()` (now).
9494
**isAlpha(str [, locale, options])** | check if the string contains only letters (a-zA-Z).<br/><br/>`locale` is one of `['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bg-BG', 'bn', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fa-IR', 'fi-FI', 'fr-CA', 'fr-FR', 'he', 'hi-IN', 'hu-HU', 'it-IT', 'ko-KR', 'ja-JP', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'si-LK', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']` and defaults to `en-US`. Locale list is `validator.isAlphaLocales`. `options` is an optional object that can be supplied with the following key(s): `ignore` which can either be a String or RegExp of characters to be ignored e.g. " -" will ignore spaces and -'s.
9595
**isAlphanumeric(str [, locale, options])** | check if the string contains only letters and numbers (a-zA-Z0-9).<br/><br/>`locale` is one of `['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bn', 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fa-IR', 'fi-FI', 'fr-CA', 'fr-FR', 'he', 'hi-IN', 'hu-HU', 'it-IT', 'ko-KR', 'ja-JP','ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'si-LK', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']`) and defaults to `en-US`. Locale list is `validator.isAlphanumericLocales`. `options` is an optional object that can be supplied with the following key(s): `ignore` which can either be a String or RegExp of characters to be ignored e.g. " -" will ignore spaces and -'s.
9696
**isAscii(str)** | check if the string contains ASCII chars only.

src/lib/isAfter.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
import assertString from './util/assertString';
21
import toDate from './toDate';
32

4-
export default function isAfter(str, date = String(new Date())) {
5-
assertString(str);
6-
const comparison = toDate(date);
7-
const original = toDate(str);
3+
export default function isAfter(date, options) {
4+
// For backwards compatibility:
5+
// isAfter(str [, date]), i.e. `options` could be used as argument for the legacy `date`
6+
const comparisonDate = options?.comparisonDate || options || Date().toString();
7+
8+
const comparison = toDate(comparisonDate);
9+
const original = toDate(date);
810
return !!(original && comparison && original > comparison);
911
}

src/lib/toDate.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import assertString from './util/assertString';
22

33
export default function toDate(date) {
44
assertString(date);
5+
56
date = Date.parse(date);
67
return !isNaN(date) ? new Date(date) : null;
78
}

test/validators.test.js

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4995,31 +4995,6 @@ describe('Validators', () => {
49954995
});
49964996
});
49974997

4998-
it('should validate dates against a start date', () => {
4999-
test({
5000-
validator: 'isAfter',
5001-
args: ['2011-08-03'],
5002-
valid: ['2011-08-04', new Date(2011, 8, 10).toString()],
5003-
invalid: ['2010-07-02', '2011-08-03', new Date(0).toString(), 'foo'],
5004-
});
5005-
test({
5006-
validator: 'isAfter',
5007-
valid: ['2100-08-04', new Date(Date.now() + 86400000).toString()],
5008-
invalid: ['2010-07-02', new Date(0).toString()],
5009-
});
5010-
test({
5011-
validator: 'isAfter',
5012-
args: ['2011-08-03'],
5013-
valid: ['2015-09-17'],
5014-
invalid: ['invalid date'],
5015-
});
5016-
test({
5017-
validator: 'isAfter',
5018-
args: ['invalid date'],
5019-
invalid: ['invalid date', '2015-09-17'],
5020-
});
5021-
});
5022-
50234998
it('should validate dates against an end date', () => {
50244999
test({
50255000
validator: 'isBefore',

test/validators/isAfter.test.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import test from '../testFunctions';
2+
3+
describe('isAfter', () => {
4+
it('should validate dates against a start date', () => {
5+
test({
6+
validator: 'isAfter',
7+
args: [{ comparisonDate: '2011-08-03' }],
8+
valid: ['2011-08-04', new Date(2011, 8, 10).toString()],
9+
invalid: ['2010-07-02', '2011-08-03', new Date(0).toString(), 'foo'],
10+
});
11+
12+
test({
13+
validator: 'isAfter',
14+
valid: ['2100-08-04', new Date(Date.now() + 86400000).toString()],
15+
invalid: ['2010-07-02', new Date(0).toString()],
16+
});
17+
18+
test({
19+
validator: 'isAfter',
20+
args: [{ comparisonDate: '2011-08-03' }],
21+
valid: ['2015-09-17'],
22+
invalid: ['invalid date'],
23+
});
24+
25+
test({
26+
validator: 'isAfter',
27+
args: [{ comparisonDate: 'invalid date' }],
28+
invalid: ['invalid date', '2015-09-17'],
29+
});
30+
});
31+
32+
describe('(legacy syntax)', () => {
33+
it('should validate dates against a start date', () => {
34+
test({
35+
validator: 'isAfter',
36+
args: ['2011-08-03'],
37+
valid: ['2011-08-04', new Date(2011, 8, 10).toString()],
38+
invalid: ['2010-07-02', '2011-08-03', new Date(0).toString(), 'foo'],
39+
});
40+
41+
test({
42+
validator: 'isAfter',
43+
valid: ['2100-08-04', new Date(Date.now() + 86400000).toString()],
44+
invalid: ['2010-07-02', new Date(0).toString()],
45+
});
46+
47+
test({
48+
validator: 'isAfter',
49+
args: ['2011-08-03'],
50+
valid: ['2015-09-17'],
51+
invalid: ['invalid date'],
52+
});
53+
54+
test({
55+
validator: 'isAfter',
56+
args: ['invalid date'],
57+
invalid: ['invalid date', '2015-09-17'],
58+
});
59+
});
60+
});
61+
});

0 commit comments

Comments
 (0)