Skip to content

Commit

Permalink
fix(datepicker): mark year 0 as invalid in Gregorian calendar (#2733)
Browse files Browse the repository at this point in the history
Fixes #2721
  • Loading branch information
maxokorokov authored and pkozlowski-opensource committed Sep 21, 2018
1 parent 3cd653d commit dd788a0
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
22 changes: 18 additions & 4 deletions src/datepicker/adapters/ngb-date-adapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,17 @@ describe('ngb-date model adapter', () => {
expect(adapter.fromModel(<any>{})).toBeNull();
expect(adapter.fromModel(<any>new Date())).toBeNull();
expect(adapter.fromModel(<any>{year: 2017, month: 10})).toBeNull();
expect(adapter.fromModel(<any>{month: 10, day: 10})).toBeNull();
expect(adapter.fromModel(<any>{year: 2017, day: 10})).toBeNull();
expect(adapter.fromModel(<any>{year: '2017', month: 10, day: 10})).toBeNull();
expect(adapter.fromModel(<any>{year: 2017, month: '10', day: 10})).toBeNull();
expect(adapter.fromModel(<any>{year: 2017, month: 10, day: '10'})).toBeNull();
});

it('should convert valid date',
() => { expect(adapter.fromModel({year: 2016, month: 5, day: 1})).toEqual({year: 2016, month: 5, day: 1}); });
it('should bypass numeric date', () => {
expect(adapter.fromModel({year: 0, month: 0, day: 0})).toEqual({year: 0, month: 0, day: 0});
expect(adapter.fromModel({year: 2016, month: 5, day: 1})).toEqual({year: 2016, month: 5, day: 1});
});
});

describe('toModel', () => {
Expand All @@ -33,10 +40,17 @@ describe('ngb-date model adapter', () => {
expect(adapter.toModel(<any>{})).toBeNull();
expect(adapter.toModel(<any>new Date())).toBeNull();
expect(adapter.toModel(<any>{year: 2017, month: 10})).toBeNull();
expect(adapter.toModel(<any>{month: 10, day: 10})).toBeNull();
expect(adapter.toModel(<any>{year: 2017, day: 10})).toBeNull();
expect(adapter.toModel(<any>{year: '2017', month: 10, day: 10})).toBeNull();
expect(adapter.toModel(<any>{year: 2017, month: '10', day: 10})).toBeNull();
expect(adapter.toModel(<any>{year: 2017, month: 10, day: '10'})).toBeNull();
});

it('should convert a valid date',
() => { expect(adapter.toModel({year: 2016, month: 10, day: 15})).toEqual({year: 2016, month: 10, day: 15}); });
it('should bypass numeric date', () => {
expect(adapter.toModel({year: 0, month: 0, day: 0})).toEqual({year: 0, month: 0, day: 0});
expect(adapter.toModel({year: 2016, month: 10, day: 15})).toEqual({year: 2016, month: 10, day: 15});
});
});

});
9 changes: 7 additions & 2 deletions src/datepicker/adapters/ngb-date-adapter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {Injectable} from '@angular/core';
import {NgbDateStruct} from '../ngb-date-struct';
import {isNumber} from 'util';

export function NGB_DATEPICKER_DATE_ADAPTER_FACTORY() {
return new NgbDateStructAdapter();
Expand Down Expand Up @@ -31,13 +32,17 @@ export class NgbDateStructAdapter extends NgbDateAdapter<NgbDateStruct> {
* Converts a NgbDateStruct value into NgbDateStruct value
*/
fromModel(date: NgbDateStruct): NgbDateStruct {
return (date && date.year && date.month && date.day) ? {year: date.year, month: date.month, day: date.day} : null;
return (date && isNumber(date.year) && isNumber(date.month) && isNumber(date.day)) ?
{year: date.year, month: date.month, day: date.day} :
null;
}

/**
* Converts a NgbDateStruct value into NgbDateStruct value
*/
toModel(date: NgbDateStruct): NgbDateStruct {
return (date && date.year && date.month && date.day) ? {year: date.year, month: date.month, day: date.day} : null;
return (date && isNumber(date.year) && isNumber(date.month) && isNumber(date.day)) ?
{year: date.year, month: date.month, day: date.day} :
null;
}
}
2 changes: 2 additions & 0 deletions src/datepicker/ngb-calendar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,6 @@ describe('ngb-calendar-gregorian', () => {
expect(calendar.isValid(new NgbDate(2016, 8, 8))).toBeTruthy();
});

it('should dates with year 0 as invalid', () => { expect(calendar.isValid(new NgbDate(0, 1, 1))).toBeFalsy(); });

});
5 changes: 5 additions & 0 deletions src/datepicker/ngb-calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ export class NgbCalendarGregorian extends NgbCalendar {
return false;
}

// year 0 doesn't exist in Gregorian calendar
if (date.year === 0) {
return false;
}

const jsDate = toJSDate(date);

return !isNaN(jsDate.getTime()) && jsDate.getFullYear() === date.year && jsDate.getMonth() + 1 === date.month &&
Expand Down

0 comments on commit dd788a0

Please sign in to comment.