Skip to content
This repository was archived by the owner on Oct 20, 2021. It is now read-only.

Commit 59a1e08

Browse files
perf(date): Use date-fns instead of moment
1 parent 8f2ae6f commit 59a1e08

20 files changed

+167
-187
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ The library can be loaded either as a standalone script, or through an [AMD](htt
116116
- [CurrencyCode](docs/currency-code.md)
117117
- [Currency](docs/currency.md)
118118
- [DataUri](docs/data-uri.md)
119-
- [DateTime](docs/date-time.md)
119+
- [DateFormat](docs/date-format.md)
120120
- [Decimal](docs/decimal.md)
121121
- [Digit](docs/digit.md)
122122
- [Directory](docs/directory.md)

awesome-validator.min.js

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/date-format.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# DateFormat
2+
3+
Validates if input is a date.
4+
5+
Valid values:
6+
7+
```js
8+
validator.dateFormat().validate('2008');
9+
validator.dateFormat().validate(new Date(2008, 1, 29));
10+
validator.dateFormat().validate('2008-02-29');
11+
validator.dateFormat().validate(2008);
12+
validator.dateFormat().validate(moment([2008]));
13+
validator.dateFormat().validate([]);
14+
validator.dateFormat().validate({});
15+
validator.dateFormat('DD-MM-YYYY').validate('29-02-2008');
16+
validator.dateFormat('DD/MM/YYYY HH:mm:ss').validate('20/10/2008 10:30:29');
17+
```
18+
19+
Invalid values:
20+
21+
```js
22+
validator.dateFormat().validate('');
23+
validator.dateFormat().validate('2009-12-00');
24+
validator.dateFormat('DD/MM/YYYY').validate('2008-02-29');
25+
```

docs/date-time.md

-25
This file was deleted.

package-lock.json

+14-14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,14 @@
6060
"@types/deep-equal": "^1.0.1",
6161
"@types/mime-types": "^2.1.0",
6262
"chardet": "^0.4.2",
63+
"date-fns": "^2.0.0-alpha.7",
6364
"deep-equal": "^1.0.1",
64-
"mime-types": "^2.1.17",
65-
"moment": "^2.20.1"
65+
"mime-types": "^2.1.17"
6666
},
6767
"devDependencies": {
6868
"@cknow/tslint-config": "^3.3.0",
69-
"@types/chai": "^4.1.1",
70-
"@types/mocha": "^2.2.46",
69+
"@types/chai": "^4.1.2",
70+
"@types/mocha": "^2.2.48",
7171
"@types/mock-fs": "^3.6.30",
7272
"awesome-typescript-loader": "^3.4.1",
7373
"chai": "^4.1.2",
@@ -83,7 +83,7 @@
8383
"temp-write": "^3.4.0",
8484
"ts-node": "^4.1.0",
8585
"tslint": "^5.9.1",
86-
"typescript": "^2.6.2",
86+
"typescript": "^2.7.1",
8787
"validate-commit-msg": "^2.14.0",
8888
"webpack": "^3.10.0"
8989
},

src/rules/abstract-date.ts

+12-21
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,44 @@
1-
import * as moment from 'moment';
1+
import { format as formatDate, isValid, parse as parseDate, toDate } from 'date-fns';
22

33
import { AbstractRule } from './abstract-rule';
4-
import { BooleanType } from './boolean-type';
5-
import { NumberVal } from './number-val';
6-
import { StringType } from './string-type';
74

85
export abstract class AbstractDate extends AbstractRule {
96

107
/**
118
* Parse.
129
*/
13-
public static parse(input: any, format?: moment.MomentFormatSpecification): moment.Moment {
14-
if (new NumberVal().validate(input)) {
15-
if (moment([input]).isValid()) {
16-
return moment([input]);
17-
}
18-
19-
return moment.unix(Number(input));
10+
public static parse(input: any, format?: string): Date | null {
11+
if (typeof input !== 'string' || !format) {
12+
return isValid(input) ? toDate(input) : null;
2013
}
2114

22-
if (new StringType().validate(input)) {
23-
return moment(input, format, !!format);
24-
}
15+
const parsed: Date = parseDate(String(input), format, new Date());
2516

26-
if (new BooleanType().validate(input)) {
27-
return moment();
17+
if (!isValid(parsed) || formatDate(parsed, format) !== input) {
18+
return null;
2819
}
2920

30-
return moment(input);
21+
return parsed;
3122
}
3223

3324
/**
3425
* AbstractDate.
3526
*/
36-
public constructor(public readonly format?: moment.MomentFormatSpecification) {
27+
public constructor(public readonly format?: string) {
3728
super();
3829
}
3930

4031
/**
4132
* Validate.
4233
*/
4334
public validate(input: any): boolean {
44-
const date: moment.Moment = AbstractDate.parse(input, this.format);
35+
const date: Date | null = AbstractDate.parse(input, this.format);
4536

46-
return date.isValid() && this.validateDate(date);
37+
return !!date && this.validateDate(date);
4738
}
4839

4940
/**
5041
* Validate Date.
5142
*/
52-
protected abstract validateDate(date: moment.Moment): boolean;
43+
protected abstract validateDate(date: Date): boolean;
5344
}

src/rules/abstract-interval.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { AbstractRule } from './abstract-rule';
22
import { ArrayType } from './array-type';
3-
import { DateTime } from './date-time';
3+
import { DateFormat } from './date-format';
44
import { NotOptional } from './not-optional';
55
import { NumberVal } from './number-val';
66
import { ObjectTypeStrict } from './object-type-strict';
@@ -43,8 +43,9 @@ export abstract class AbstractInterval extends AbstractRule {
4343
return input.size;
4444
}
4545

46-
if (new DateTime().validate(input)) {
47-
return DateTime.parse(input).unix();
46+
const date: Date | null = DateFormat.parse(input);
47+
if (date) {
48+
return date.getTime();
4849
}
4950

5051
return String(input).length;

src/rules/age.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
import * as moment from 'moment';
1+
import { subYears } from 'date-fns';
22

33
import { AbstractRule } from './abstract-rule';
44
import { Between } from './between';
5-
import { DateTime } from './date-time';
5+
import { DateFormat } from './date-format';
66

77
export class Age extends AbstractRule {
88

99
/**
1010
* Age.
1111
*/
1212
public constructor(
13-
public readonly min: moment.DurationInputArg1 = 18,
14-
public readonly max?: moment.DurationInputArg1,
15-
public readonly format?: moment.MomentFormatSpecification
13+
public readonly min: number = 18,
14+
public readonly max?: number,
15+
public readonly format?: string
1616
) {
1717
super();
1818
}
@@ -21,11 +21,11 @@ export class Age extends AbstractRule {
2121
* Validate.
2222
*/
2323
public validate(input: any): boolean {
24-
const date: moment.Moment = DateTime.parse(input, this.format);
24+
const date: Date | null = DateFormat.parse(input, this.format);
2525

26-
return date.isValid() && new Between(
27-
this.max ? moment().subtract(this.max, 'years').hour(0).minute(0).second(0) : null,
28-
this.min ? moment().subtract(this.min, 'years').hour(23).minute(59).second(59) : null
26+
return !!date && new Between(
27+
this.max ? subYears(new Date(), this.max).setHours(0, 0, 0) : null,
28+
this.min ? subYears(new Date(), this.min).setHours(23, 59, 59) : null
2929
).validate(date);
3030
}
3131
}

src/rules/date-format.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { AbstractDate } from './abstract-date';
2+
3+
export class DateFormat extends AbstractDate {
4+
5+
/**
6+
* Validate Date.
7+
*/
8+
protected validateDate(date: Date): boolean {
9+
return true;
10+
}
11+
}
12+
13+
export default DateFormat;

src/rules/date-time.ts

-15
This file was deleted.

src/rules/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export * from './credit-card';
3434
export * from './currency-code';
3535
export * from './currency';
3636
export * from './data-uri';
37-
export * from './date-time';
37+
export * from './date-format';
3838
export * from './decimal';
3939
export * from './digit';
4040
export * from './directory';

src/rules/leap-date.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
import * as moment from 'moment';
2-
31
import { AbstractDate } from './abstract-date';
42

53
export class LeapDate extends AbstractDate {
64

75
/**
86
* Validate Date.
97
*/
10-
protected validateDate(date: moment.Moment): boolean {
11-
return date.format('DD-MM') === '29-02';
8+
protected validateDate(date: Date): boolean {
9+
return date.getDate() === 29 && date.getMonth() === 1;
1210
}
1311
}
1412

src/rules/leap-year.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as moment from 'moment';
1+
import { isLeapYear } from 'date-fns';
22

33
import { AbstractDate } from './abstract-date';
44

@@ -7,8 +7,8 @@ export class LeapYear extends AbstractDate {
77
/**
88
* Validate Date.
99
*/
10-
protected validateDate(date: moment.Moment): boolean {
11-
return date.isLeapYear();
10+
protected validateDate(date: Date): boolean {
11+
return isLeapYear(date);
1212
}
1313
}
1414

test/rules/age.ts

+12-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import * as moment from 'moment';
2-
31
import { assert } from 'chai';
2+
import { format, subYears } from 'date-fns';
43

54
import { AbstractRule } from '../../src/rules/abstract-rule';
65
import { Age } from '../../src/rules/age';
@@ -13,23 +12,23 @@ describe('Age', () => {
1312

1413
it('values is valid', () => {
1514
assert.isTrue(new Age().validate('1969-07-20'));
16-
assert.isTrue(new Age(0).validate(moment()));
17-
assert.isTrue(new Age(0, 0).validate(moment()));
15+
assert.isTrue(new Age(0).validate(new Date()));
16+
assert.isTrue(new Age(0, 0).validate(new Date()));
1817
assert.isTrue(new Age(18).validate('1969-07-20'));
19-
assert.isTrue(new Age(undefined, 50).validate(moment().subtract(20, 'years')));
18+
assert.isTrue(new Age(undefined, 50).validate(subYears(new Date(), 20)));
2019
assert.isTrue(new Age(undefined, undefined, 'DD/MM/YYYY').validate('20/07/1969'));
21-
assert.isTrue(new Age(10, 50).validate(moment().subtract(20, 'years')));
22-
assert.isTrue(new Age(10, 50, 'DD/MM/YYYY').validate(moment().subtract(20, 'years').format('DD/MM/YYYY')));
20+
assert.isTrue(new Age(10, 50).validate(subYears(new Date(), 20)));
21+
assert.isTrue(new Age(10, 50, 'DD/MM/YYYY').validate(format(subYears(new Date(), 20), 'DD/MM/YYYY')));
2322
});
2423

2524
it('values is not valid', () => {
26-
assert.isFalse(new Age().validate(moment()));
27-
assert.isFalse(new Age(undefined, 0).validate(moment()));
28-
assert.isFalse(new Age(10).validate(moment().subtract(5, 'years')));
29-
assert.isFalse(new Age(undefined, 10).validate(moment().subtract(20, 'years')));
25+
assert.isFalse(new Age().validate(new Date()));
26+
assert.isFalse(new Age(undefined, 0).validate(new Date()));
27+
assert.isFalse(new Age(10).validate(subYears(new Date(), 5)));
28+
assert.isFalse(new Age(undefined, 10).validate(subYears(new Date(), 20)));
3029
assert.isFalse(new Age(undefined, undefined, 'DD/MM/YYYY').validate('1969-07-20'));
31-
assert.isFalse(new Age(10, 50).validate(moment().subtract(5, 'years')));
32-
assert.isFalse(new Age(30, 50, 'DD/MM/YYYY').validate(moment().subtract(20, 'years').format('DD/MM/YYYY')));
30+
assert.isFalse(new Age(10, 50).validate(subYears(new Date(), 5)));
31+
assert.isFalse(new Age(30, 50, 'DD/MM/YYYY').validate(format(subYears(new Date(), 20), 'DD/MM/YYYY')));
3332
});
3433

3534
});

0 commit comments

Comments
 (0)