Skip to content

Commit 9051f65

Browse files
committed
address comments
1 parent 0e919a9 commit 9051f65

12 files changed

+65
-60
lines changed

src/lib/core/datetime/date-adapter.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export abstract class DateAdapter<D> {
118118
abstract today(): D;
119119

120120
/**
121-
* Parses a date from a value.
121+
* Parses a date from a user-inputted value.
122122
* @param value The value to parse.
123123
* @param parseFormat The expected format of the value being parsed
124124
* (type is implementation-dependent).
@@ -127,7 +127,7 @@ export abstract class DateAdapter<D> {
127127
abstract parse(value: any, parseFormat: any): D | null;
128128

129129
/**
130-
* Formats a date as a string.
130+
* Formats a date as a string according to the given format.
131131
* @param date The value to format.
132132
* @param displayFormat The format to use to display the date as a string.
133133
* @returns The formatted date string.
@@ -165,6 +165,8 @@ export abstract class DateAdapter<D> {
165165

166166
/**
167167
* Gets the RFC 3339 compatible string (https://tools.ietf.org/html/rfc3339) for the given date.
168+
* This method is used to generate date strings that are compatible with native HTML attributes
169+
* such as the `min` or `max` attribute of an `<input>`.
168170
* @param date The date to get the ISO date string for.
169171
* @returns The ISO date string date string.
170172
*/
@@ -185,20 +187,23 @@ export abstract class DateAdapter<D> {
185187
abstract isValid(date: D): boolean;
186188

187189
/**
188-
* Attempts to coerce a value to a valid date object. This is different from parsing in that it
189-
* should only coerce non-ambiguous, locale-independent values (e.g. a ISO 8601 string).
190-
* The default implementation does not allow any coercion, it simply checks that the given value
191-
* is already a valid date object or null.
192-
* @param value The value to be coerced to a date object.
193-
* @returns The coerced date object, either a valid date, null if the value can be coerced to a
194-
* null date (e.g. the empty string).
195-
* @throws If the given value cannot be coerced to a valid date or null.
190+
* Attempts to deserialize a value to a valid date object. This is different from parsing in that
191+
* deserialize should only accept non-ambiguous, locale-independent values (e.g. a ISO 8601
192+
* string). The default implementation does not allow any deserialization, it simply checks that
193+
* the given value is already a valid date object or null. The `<mat-datepicker>` will call this
194+
* method on all of it's `@Input()` properties that accept dates. It is therefore possible to
195+
* support passing your wire format directly to these properties by overriding this method to
196+
* also deserialize your wire format.
197+
* @param value The value to be deserialized into a date object.
198+
* @returns The deserialized date object, either a valid date, null if the value can be
199+
* deserialized into a null date (e.g. the empty string).
200+
* @throws If the given value cannot be deserialized into a valid date or null.
196201
*/
197-
coerceToDate(value: any): D | null {
202+
deserialize(value: any): D | null {
198203
if (value == null || this.isDateInstance(value) && this.isValid(value)) {
199204
return value;
200205
}
201-
throw Error(`Could not coerce "${value}" to a valid date object.`);
206+
throw Error(`Could not deserialize "${value}" to a valid date object.`);
202207
}
203208

204209
/**

src/lib/core/datetime/native-date-adapter.spec.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -334,18 +334,18 @@ describe('NativeDateAdapter', () => {
334334
});
335335

336336
it('should create dates from valid ISO strings', () => {
337-
expect(adapter.coerceToDate('1985-04-12T23:20:50.52Z')).not.toBeNull();
338-
expect(adapter.coerceToDate('1996-12-19T16:39:57-08:00')).not.toBeNull();
339-
expect(adapter.coerceToDate('1937-01-01T12:00:27.87+00:20')).not.toBeNull();
340-
expect(adapter.coerceToDate('2017-01-01')).not.toBeNull();
341-
expect(adapter.coerceToDate('2017-01-01T00:00:00')).not.toBeNull();
342-
expect(() => adapter.coerceToDate('1990-13-31T23:59:00Z')).toThrow();
343-
expect(() => adapter.coerceToDate('1/1/2017')).toThrow();
344-
expect(() => adapter.coerceToDate('2017-01-01T')).toThrow();
345-
expect(adapter.coerceToDate('')).toBeNull();
346-
expect(adapter.coerceToDate(null)).toBeNull();
347-
expect(adapter.coerceToDate(new Date())).not.toBeNull();
348-
expect(() => adapter.coerceToDate(new Date(NaN))).toThrow();
337+
expect(adapter.deserialize('1985-04-12T23:20:50.52Z')).not.toBeNull();
338+
expect(adapter.deserialize('1996-12-19T16:39:57-08:00')).not.toBeNull();
339+
expect(adapter.deserialize('1937-01-01T12:00:27.87+00:20')).not.toBeNull();
340+
expect(adapter.deserialize('2017-01-01')).not.toBeNull();
341+
expect(adapter.deserialize('2017-01-01T00:00:00')).not.toBeNull();
342+
expect(() => adapter.deserialize('1990-13-31T23:59:00Z')).toThrow();
343+
expect(() => adapter.deserialize('1/1/2017')).toThrow();
344+
expect(() => adapter.deserialize('2017-01-01T')).toThrow();
345+
expect(adapter.deserialize('')).toBeNull();
346+
expect(adapter.deserialize(null)).toBeNull();
347+
expect(adapter.deserialize(new Date())).not.toBeNull();
348+
expect(() => adapter.deserialize(new Date(NaN))).toThrow();
349349
});
350350
});
351351

src/lib/core/datetime/native-date-adapter.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,11 @@ export class NativeDateAdapter extends DateAdapter<Date> {
221221
}
222222

223223
/**
224-
* Returns the given value if given a valid Date or null. Coerces valid ISO 8601 strings
225-
* (https://www.ietf.org/rfc/rfc3339.txt) to valid Dates and empty string to null. Throws on all
226-
* other values.
224+
* Returns the given value if given a valid Date or null. Deserializes valid ISO 8601 strings
225+
* (https://www.ietf.org/rfc/rfc3339.txt) into valid Dates and empty string into null. Throws on
226+
* all other values.
227227
*/
228-
coerceToDate(value: any): Date | null {
228+
deserialize(value: any): Date | null {
229229
if (typeof value === 'string') {
230230
if (!value) {
231231
return null;
@@ -239,7 +239,7 @@ export class NativeDateAdapter extends DateAdapter<Date> {
239239
}
240240
}
241241
}
242-
return super.coerceToDate(value);
242+
return super.deserialize(value);
243243
}
244244

245245
isDateInstance(obj: any) {

src/lib/datepicker/calendar.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export class MatCalendar<D> implements AfterContentInit, OnDestroy, OnChanges {
6666
/** A date representing the period (month or year) to start the calendar in. */
6767
@Input()
6868
get startAt(): D | null { return this._startAt; }
69-
set startAt(value: D | null) { this._startAt = this._dateAdapter.coerceToDate(value); }
69+
set startAt(value: D | null) { this._startAt = this._dateAdapter.deserialize(value); }
7070
private _startAt: D | null;
7171

7272
/** Whether the calendar should be started in month or year view. */
@@ -75,19 +75,19 @@ export class MatCalendar<D> implements AfterContentInit, OnDestroy, OnChanges {
7575
/** The currently selected date. */
7676
@Input()
7777
get selected(): D | null { return this._selected; }
78-
set selected(value: D | null) { this._selected = this._dateAdapter.coerceToDate(value); }
78+
set selected(value: D | null) { this._selected = this._dateAdapter.deserialize(value); }
7979
private _selected: D | null;
8080

8181
/** The minimum selectable date. */
8282
@Input()
8383
get minDate(): D | null { return this._minDate; }
84-
set minDate(value: D | null) { this._minDate = this._dateAdapter.coerceToDate(value); }
84+
set minDate(value: D | null) { this._minDate = this._dateAdapter.deserialize(value); }
8585
private _minDate: D | null;
8686

8787
/** The maximum selectable date. */
8888
@Input()
8989
get maxDate(): D | null { return this._maxDate; }
90-
set maxDate(value: D | null) { this._maxDate = this._dateAdapter.coerceToDate(value); }
90+
set maxDate(value: D | null) { this._maxDate = this._dateAdapter.deserialize(value); }
9191
private _maxDate: D | null;
9292

9393
/** A function used to filter which dates are selectable. */

src/lib/datepicker/coerce-date-property.spec.ts

Whitespace-only changes.

src/lib/datepicker/coerce-date-property.ts

Whitespace-only changes.

src/lib/datepicker/datepicker-input.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export class MatDatepickerInput<D> implements AfterContentInit, ControlValueAcce
112112
return this._value;
113113
}
114114
set value(value: D | null) {
115-
value = this._dateAdapter.coerceToDate(value);
115+
value = this._dateAdapter.deserialize(value);
116116
this._lastValueValid = !value || this._dateAdapter.isValid(value);
117117
value = this._getValidDateOrNull(value);
118118

@@ -130,7 +130,7 @@ export class MatDatepickerInput<D> implements AfterContentInit, ControlValueAcce
130130
@Input()
131131
get min(): D | null { return this._min; }
132132
set min(value: D | null) {
133-
this._min = this._dateAdapter.coerceToDate(value);
133+
this._min = this._dateAdapter.deserialize(value);
134134
this._validatorOnChange();
135135
}
136136
private _min: D | null;
@@ -139,7 +139,7 @@ export class MatDatepickerInput<D> implements AfterContentInit, ControlValueAcce
139139
@Input()
140140
get max(): D | null { return this._max; }
141141
set max(value: D | null) {
142-
this._max = this._dateAdapter.coerceToDate(value);
142+
this._max = this._dateAdapter.deserialize(value);
143143
this._validatorOnChange();
144144
}
145145
private _max: D | null;
@@ -187,23 +187,23 @@ export class MatDatepickerInput<D> implements AfterContentInit, ControlValueAcce
187187

188188
/** The form control validator for the min date. */
189189
private _minValidator: ValidatorFn = (control: AbstractControl): ValidationErrors | null => {
190-
const controlValue = this._dateAdapter.coerceToDate(control.value);
190+
const controlValue = this._dateAdapter.deserialize(control.value);
191191
return (!this.min || !controlValue ||
192192
this._dateAdapter.compareDate(this.min, controlValue) <= 0) ?
193193
null : {'matDatepickerMin': {'min': this.min, 'actual': controlValue}};
194194
}
195195

196196
/** The form control validator for the max date. */
197197
private _maxValidator: ValidatorFn = (control: AbstractControl): ValidationErrors | null => {
198-
const controlValue = this._dateAdapter.coerceToDate(control.value);
198+
const controlValue = this._dateAdapter.deserialize(control.value);
199199
return (!this.max || !controlValue ||
200200
this._dateAdapter.compareDate(this.max, controlValue) >= 0) ?
201201
null : {'matDatepickerMax': {'max': this.max, 'actual': controlValue}};
202202
}
203203

204204
/** The form control validator for the date filter. */
205205
private _filterValidator: ValidatorFn = (control: AbstractControl): ValidationErrors | null => {
206-
const controlValue = this._dateAdapter.coerceToDate(control.value);
206+
const controlValue = this._dateAdapter.deserialize(control.value);
207207
return !this._dateFilter || !controlValue || this._dateFilter(controlValue) ?
208208
null : {'matDatepickerFilter': true};
209209
}

src/lib/datepicker/datepicker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export class MatDatepicker<D> implements OnDestroy {
132132
// selected value is.
133133
return this._startAt || (this._datepickerInput ? this._datepickerInput.value : null);
134134
}
135-
set startAt(date: D | null) { this._startAt = this._dateAdapter.coerceToDate(date); }
135+
set startAt(date: D | null) { this._startAt = this._dateAdapter.deserialize(date); }
136136
private _startAt: D | null;
137137

138138
/** The view that the calendar should start in. */

src/lib/datepicker/month-view.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export class MatMonthView<D> implements AfterContentInit {
4646
get activeDate(): D { return this._activeDate; }
4747
set activeDate(value: D) {
4848
let oldActiveDate = this._activeDate;
49-
this._activeDate = this._dateAdapter.coerceToDate(value) || this._dateAdapter.today();
49+
this._activeDate = this._dateAdapter.deserialize(value) || this._dateAdapter.today();
5050
if (!this._hasSameMonthAndYear(oldActiveDate, this._activeDate)) {
5151
this._init();
5252
}
@@ -57,7 +57,7 @@ export class MatMonthView<D> implements AfterContentInit {
5757
@Input()
5858
get selected(): D | null { return this._selected; }
5959
set selected(value: D | null) {
60-
this._selected = this._dateAdapter.coerceToDate(value);
60+
this._selected = this._dateAdapter.deserialize(value);
6161
this._selectedDate = this._getDateInCurrentMonth(this._selected);
6262
}
6363
private _selected: D | null;

src/lib/datepicker/year-view.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class MatYearView<D> implements AfterContentInit {
4141
get activeDate(): D { return this._activeDate; }
4242
set activeDate(value: D) {
4343
let oldActiveDate = this._activeDate;
44-
this._activeDate = this._dateAdapter.coerceToDate(value) || this._dateAdapter.today();
44+
this._activeDate = this._dateAdapter.deserialize(value) || this._dateAdapter.today();
4545
if (this._dateAdapter.getYear(oldActiveDate) != this._dateAdapter.getYear(this._activeDate)) {
4646
this._init();
4747
}
@@ -52,7 +52,7 @@ export class MatYearView<D> implements AfterContentInit {
5252
@Input()
5353
get selected(): D | null { return this._selected; }
5454
set selected(value: D | null) {
55-
this._selected = this._dateAdapter.coerceToDate(value);
55+
this._selected = this._dateAdapter.deserialize(value);
5656
this._selectedMonth = this._getMonthInCurrentYear(this._selected);
5757
}
5858
private _selected: D | null;

src/material-moment-adapter/adapter/moment-date-adapter.spec.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -306,17 +306,17 @@ describe('MomentDateAdapter', () => {
306306
});
307307

308308
it('should create valid dates from valid ISO strings', () => {
309-
expect(adapter.coerceToDate('1985-04-12T23:20:50.52Z')).not.toBeNull();
310-
expect(adapter.coerceToDate('1996-12-19T16:39:57-08:00')).not.toBeNull();
311-
expect(adapter.coerceToDate('1937-01-01T12:00:27.87+00:20')).not.toBeNull();
312-
expect(() => adapter.coerceToDate('1990-13-31T23:59:00Z')).toThrow();
313-
expect(() => adapter.coerceToDate('1/1/2017')).toThrow();
314-
expect(adapter.coerceToDate('')).toBeNull();
315-
expect(adapter.coerceToDate(null)).toBeNull();
316-
expect(adapter.coerceToDate(new Date())).not.toBeNull();
317-
expect(() => adapter.coerceToDate(new Date(NaN))).toThrow();
318-
expect(adapter.coerceToDate(moment())).not.toBeNull();
319-
expect(() => adapter.coerceToDate(moment.invalid())).toThrow();
309+
expect(adapter.deserialize('1985-04-12T23:20:50.52Z')).not.toBeNull();
310+
expect(adapter.deserialize('1996-12-19T16:39:57-08:00')).not.toBeNull();
311+
expect(adapter.deserialize('1937-01-01T12:00:27.87+00:20')).not.toBeNull();
312+
expect(() => adapter.deserialize('1990-13-31T23:59:00Z')).toThrow();
313+
expect(() => adapter.deserialize('1/1/2017')).toThrow();
314+
expect(adapter.deserialize('')).toBeNull();
315+
expect(adapter.deserialize(null)).toBeNull();
316+
expect(adapter.deserialize(new Date())).not.toBeNull();
317+
expect(() => adapter.deserialize(new Date(NaN))).toThrow();
318+
expect(adapter.deserialize(moment())).not.toBeNull();
319+
expect(() => adapter.deserialize(moment.invalid())).toThrow();
320320
});
321321

322322
it('setLocale should not modify global moment locale', () => {

src/material-moment-adapter/adapter/moment-date-adapter.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,11 @@ export class MomentDateAdapter extends DateAdapter<Moment> {
175175
}
176176

177177
/**
178-
* Returns the given value if given a valid Moment or null. Coerces valid ISO 8601 strings
179-
* (https://www.ietf.org/rfc/rfc3339.txt) and valid Date objects to valid Moments and empty string
180-
* to null. Throws on all other values.
178+
* Returns the given value if given a valid Moment or null. Deserializes valid ISO 8601 strings
179+
* (https://www.ietf.org/rfc/rfc3339.txt) and valid Date objects into valid Moments and empty
180+
* string into null. Throws on all other values.
181181
*/
182-
coerceToDate(value: any): Moment | null {
182+
deserialize(value: any): Moment | null {
183183
let date;
184184
if (value instanceof Date) {
185185
date = moment(value);
@@ -193,7 +193,7 @@ export class MomentDateAdapter extends DateAdapter<Moment> {
193193
if (date && this.isValid(date)) {
194194
return date;
195195
}
196-
return super.coerceToDate(value);
196+
return super.deserialize(value);
197197
}
198198

199199
isDateInstance(obj: any): boolean {

0 commit comments

Comments
 (0)