Skip to content

Commit 650a2fa

Browse files
feat(isBefore): allow usage of options object (#2088)
* refactor: allow for splitting tests to different files * feat(isAfter): allow usage of options object * style: make options italic * refactor: rename test file extension to .test.js * refactor: rename test-functions to testFunctions * refactor: implement suggestion from #2019 review * refactor: remove custom repeat to use native function * refactor: implement suggestion new Date * Refactor isBefore with options API * Refactor isBefore tests * Refactor to simplify logic * Update README * Refactor logic * Improve README formatting * Fix backwards-compat * Remove redundant string assertion * Fix comment * Reinstate legacy tests * Change arg name according to code review * Add line break according to code review * Revert change of simplifying toDate * Fix whitespace issues * Fix test * Fix tests * Format file for consistency with isBefore * Remove redundant file * Remove old tests * Add tests for undefined args * Remove arguments: linter error * Improve comment * Use recommended variable name * Improve readme text according to code review * Make isAfter arguments more robust * Split test cases into given and default end date --------- Co-authored-by: Rik Smale <[email protected]>
1 parent c174a1f commit 650a2fa

File tree

6 files changed

+145
-42
lines changed

6 files changed

+145
-42
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ Validator | Description
9494
**isBase32(str [, options])** | check if the string is base32 encoded. `options` is optional and defaults to `{ crockford: false }`.<br/> When `crockford` is true it tests the given base32 encoded string using [Crockford's base32 alternative][Crockford Base32].
9595
**isBase58(str)** | check if the string is base58 encoded.
9696
**isBase64(str [, options])** | check if the string is base64 encoded. `options` is optional and defaults to `{ urlSafe: false, padding: true }`<br/> when `urlSafe` is true default value for `padding` is false and it tests the given base64 encoded string is [url safe][Base64 URL Safe].
97-
**isBefore(str [, date])** | check if the string is a date that is before the specified date.
97+
**isBefore(str [, options])** | check if the string is a date that is before the specified date.<br/><br/>`options` is an object that defaults to `{ comparisonDate: Date().toString() }`.<br/><br/>**Options:**<br/>`comparisonDate`: Date to compare to. Defaults to `Date().toString()` (now).
9898
**isBIC(str)** | check if the string is a BIC (Bank Identification Code) or SWIFT code.
9999
**isBoolean(str [, options])** | check if the string is a boolean.<br/>`options` is an object which defaults to `{ loose: false }`. If `loose` is set to false, the validator will strictly match ['true', 'false', '0', '1']. If `loose` is set to true, the validator will also match 'yes', 'no', and will match a valid boolean string of any case. (e.g.: ['true', 'True', 'TRUE']).
100100
**isBtcAddress(str)** | check if the string is a valid BTC address.

src/lib/isAfter.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import toDate from './toDate';
33
export default function isAfter(date, options) {
44
// For backwards compatibility:
55
// isAfter(str [, date]), i.e. `options` could be used as argument for the legacy `date`
6-
const comparisonDate = options?.comparisonDate || options || Date().toString();
6+
const comparisonDate = (typeof options === 'object' ? options.comparisonDate : options) || Date().toString();
77

88
const comparison = toDate(comparisonDate);
99
const original = toDate(date);
10+
1011
return !!(original && comparison && original > comparison);
1112
}

src/lib/isBefore.js

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

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

test/validators.test.js

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5650,41 +5650,6 @@ describe('Validators', () => {
56505650
});
56515651
});
56525652

5653-
it('should validate dates against an end date', () => {
5654-
test({
5655-
validator: 'isBefore',
5656-
args: ['08/04/2011'],
5657-
valid: ['2010-07-02', '2010-08-04', new Date(0).toString()],
5658-
invalid: ['08/04/2011', new Date(2011, 9, 10).toString()],
5659-
});
5660-
test({
5661-
validator: 'isBefore',
5662-
args: [new Date(2011, 7, 4).toString()],
5663-
valid: ['2010-07-02', '2010-08-04', new Date(0).toString()],
5664-
invalid: ['08/04/2011', new Date(2011, 9, 10).toString()],
5665-
});
5666-
test({
5667-
validator: 'isBefore',
5668-
valid: [
5669-
'2000-08-04',
5670-
new Date(0).toString(),
5671-
new Date(Date.now() - 86400000).toString(),
5672-
],
5673-
invalid: ['2100-07-02', new Date(2217, 10, 10).toString()],
5674-
});
5675-
test({
5676-
validator: 'isBefore',
5677-
args: ['2011-08-03'],
5678-
valid: ['1999-12-31'],
5679-
invalid: ['invalid date'],
5680-
});
5681-
test({
5682-
validator: 'isBefore',
5683-
args: ['invalid date'],
5684-
invalid: ['invalid date', '1999-12-31'],
5685-
});
5686-
});
5687-
56885653
it('should validate ABA routing number', () => {
56895654
test({
56905655
validator: 'isAbaRouting',

test/validators/isAfter.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,21 @@ describe('isAfter', () => {
2727
args: [{ comparisonDate: 'invalid date' }],
2828
invalid: ['invalid date', '2015-09-17'],
2929
});
30+
test({
31+
validator: 'isAfter',
32+
args: [], // will fall back to the current date
33+
valid: ['2100-08-04', new Date(Date.now() + 86400000).toString()],
34+
});
35+
test({
36+
validator: 'isAfter',
37+
args: [undefined], // will fall back to the current date
38+
valid: ['2100-08-04', new Date(Date.now() + 86400000).toString()],
39+
});
40+
test({
41+
validator: 'isAfter',
42+
args: [{ comparisonDate: undefined }], // will fall back to the current date
43+
valid: ['2100-08-04', new Date(Date.now() + 86400000).toString()],
44+
});
3045
});
3146

3247
describe('(legacy syntax)', () => {

test/validators/isBefore.test.js

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import { describe } from 'mocha';
2+
import test from '../testFunctions';
3+
4+
describe('isBefore', () => {
5+
describe('should validate dates a given end date', () => {
6+
describe('new syntax', () => {
7+
test({
8+
validator: 'isBefore',
9+
args: [{ comparisonDate: '08/04/2011' }],
10+
valid: ['2010-07-02', '2010-08-04', new Date(0).toString()],
11+
invalid: ['08/04/2011', new Date(2011, 9, 10).toString()],
12+
});
13+
test({
14+
validator: 'isBefore',
15+
args: [{ comparisonDate: new Date(2011, 7, 4).toString() }],
16+
valid: ['2010-07-02', '2010-08-04', new Date(0).toString()],
17+
invalid: ['08/04/2011', new Date(2011, 9, 10).toString()],
18+
});
19+
test({
20+
validator: 'isBefore',
21+
args: [{ comparisonDate: '2011-08-03' }],
22+
valid: ['1999-12-31'],
23+
invalid: ['invalid date'],
24+
});
25+
test({
26+
validator: 'isBefore',
27+
args: [{ comparisonDate: 'invalid date' }],
28+
invalid: ['invalid date', '1999-12-31'],
29+
});
30+
});
31+
32+
describe('legacy syntax', () => {
33+
test({
34+
validator: 'isBefore',
35+
args: ['08/04/2011'],
36+
valid: ['2010-07-02', '2010-08-04', new Date(0).toString()],
37+
invalid: ['08/04/2011', new Date(2011, 9, 10).toString()],
38+
});
39+
test({
40+
validator: 'isBefore',
41+
args: [new Date(2011, 7, 4).toString()],
42+
valid: ['2010-07-02', '2010-08-04', new Date(0).toString()],
43+
invalid: ['08/04/2011', new Date(2011, 9, 10).toString()],
44+
});
45+
test({
46+
validator: 'isBefore',
47+
args: ['2011-08-03'],
48+
valid: ['1999-12-31'],
49+
invalid: ['invalid date'],
50+
});
51+
test({
52+
validator: 'isBefore',
53+
args: ['invalid date'],
54+
invalid: ['invalid date', '1999-12-31'],
55+
});
56+
});
57+
});
58+
59+
describe('should validate dates a default end date', () => {
60+
describe('new syntax', () => {
61+
test({
62+
validator: 'isBefore',
63+
valid: [
64+
'2000-08-04',
65+
new Date(0).toString(),
66+
new Date(Date.now() - 86400000).toString(),
67+
],
68+
invalid: ['2100-07-02', new Date(2217, 10, 10).toString()],
69+
});
70+
test({
71+
validator: 'isBefore',
72+
args: undefined, // will fall back to the current date
73+
valid: ['1999-06-07'],
74+
});
75+
test({
76+
validator: 'isBefore',
77+
args: [], // will fall back to the current date
78+
valid: ['1999-06-07'],
79+
});
80+
test({
81+
validator: 'isBefore',
82+
args: [undefined], // will fall back to the current date
83+
valid: ['1999-06-07'],
84+
});
85+
test({
86+
validator: 'isBefore',
87+
args: [{ comparisonDate: undefined }], // will fall back to the current date
88+
valid: ['1999-06-07'],
89+
});
90+
});
91+
92+
describe('legacy syntax', () => {
93+
test({
94+
validator: 'isBefore',
95+
valid: [
96+
'2000-08-04',
97+
new Date(0).toString(),
98+
new Date(Date.now() - 86400000).toString(),
99+
],
100+
invalid: ['2100-07-02', new Date(2217, 10, 10).toString()],
101+
});
102+
test({
103+
validator: 'isBefore',
104+
args: undefined, // will fall back to the current date
105+
valid: ['1999-06-07'],
106+
});
107+
test({
108+
validator: 'isBefore',
109+
args: [], // will fall back to the current date
110+
valid: ['1999-06-07'],
111+
});
112+
test({
113+
validator: 'isBefore',
114+
args: [undefined], // will fall back to the current date
115+
valid: ['1999-06-07'],
116+
});
117+
});
118+
});
119+
});

0 commit comments

Comments
 (0)