Skip to content

Commit 8f878f2

Browse files
committed
Date parser ngbDateStruct
1 parent dc1aecf commit 8f878f2

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

src/min-date/validator.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,17 @@ describe('MinDate', () => {
3939
control = new FormControl('2016-09-10');
4040
expect(minDate('2016-09-09')(control)).toBeNull();
4141
});
42+
43+
it('Date object year-month-day should equal to "null"', () => {
44+
const obj = { year: 2018, month: 10, day: 11};
45+
control = new FormControl(obj);
46+
expect(minDate('2017-09-01')(control)).toBeNull();
47+
});
48+
49+
it('Date object year-month-day should equal to "{minDate: true}"', () => {
50+
const obj = { year: 2017, month: 10, day: 11};
51+
control = new FormControl(obj);
52+
expect(minDate('2017-11-01')(control)).toEqual({minDate: true});
53+
});
4254
});
4355

src/min-date/validator.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { AbstractControl, Validators, ValidatorFn } from '@angular/forms';
22

3-
import { isPresent, isDate } from '../util/lang';
3+
import { isPresent, isDate, parseDate } from '../util/lang';
44

55
export const minDate = (minDate: any): ValidatorFn => {
66

7+
minDate = parseDate(minDate);
8+
79
if (!isDate(minDate) && !(minDate instanceof Function)) {
810
throw Error('minDate value must be or return a formatted date');
911
}

src/util/lang.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,11 @@ export function isPresent(obj: any): boolean {
44

55
export function isDate(obj: any): boolean {
66
return !/Invalid|NaN/.test(new Date(obj).toString());
7+
}
8+
9+
export function parseDate(obj: any): string {
10+
if (typeof obj === 'object') {
11+
return obj.year + '-' + obj.month + '-' + obj.day;
12+
}
13+
return obj;
714
}

0 commit comments

Comments
 (0)