Skip to content

Commit add6ff0

Browse files
committed
address comments
1 parent 5c7ba67 commit add6ff0

File tree

4 files changed

+18
-161
lines changed

4 files changed

+18
-161
lines changed

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

Lines changed: 2 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -10,156 +10,14 @@ import {MomentDateAdapter} from './moment-date-adapter';
1010
import {async, inject, TestBed} from '@angular/core/testing';
1111
import {MomentDateModule} from './index';
1212
import {DateAdapter, MAT_DATE_LOCALE} from '@angular/material';
13-
import * as moment from 'moment';
1413
import {LOCALE_ID} from '@angular/core';
14+
import * as moment from 'moment';
1515

1616

1717
// Month constants for more readable tests.
1818
const JAN = 0, FEB = 1, MAR = 2, DEC = 11;
1919

2020

21-
// Add some locales for testing. These definitions come from Moment.js's fr.js and ja.js locale
22-
// files. (We don't want to the version of moment that comes with locales because it's a lot of
23-
// extra bytes to include in our tests.)
24-
moment.defineLocale('fr', {
25-
months: 'janvier_février_mars_avril_mai_juin_juillet_août_septembre_octobre_novembre_décembre'
26-
.split('_'),
27-
monthsShort: 'janv._févr._mars_avr._mai_juin_juil._août_sept._oct._nov._déc.'.split('_'),
28-
monthsParseExact: true,
29-
weekdays: 'dimanche_lundi_mardi_mercredi_jeudi_vendredi_samedi'.split('_'),
30-
weekdaysShort: 'dim._lun._mar._mer._jeu._ven._sam.'.split('_'),
31-
weekdaysMin: 'Di_Lu_Ma_Me_Je_Ve_Sa'.split('_'),
32-
weekdaysParseExact: true,
33-
longDateFormat: {
34-
LT: 'HH:mm',
35-
LTS: 'HH:mm:ss',
36-
L: 'DD/MM/YYYY',
37-
LL: 'D MMMM YYYY',
38-
LLL: 'D MMMM YYYY HH:mm',
39-
LLLL: 'dddd D MMMM YYYY HH:mm'
40-
},
41-
calendar: {
42-
sameDay: '[Aujourd’hui à] LT',
43-
nextDay: '[Demain à] LT',
44-
nextWeek: 'dddd [à] LT',
45-
lastDay: '[Hier à] LT',
46-
lastWeek: 'dddd [dernier à] LT',
47-
sameElse: 'L'
48-
},
49-
relativeTime: {
50-
future: 'dans %s',
51-
past: 'il y a %s',
52-
s: 'quelques secondes',
53-
m: 'une minute',
54-
mm: '%d minutes',
55-
h: 'une heure',
56-
hh: '%d heures',
57-
d: 'un jour',
58-
dd: '%d jours',
59-
M: 'un mois',
60-
MM: '%d mois',
61-
y: 'un an',
62-
yy: '%d ans'
63-
},
64-
dayOfMonthOrdinalParse: /\d{1,2}(er|)/,
65-
// tslint:disable-next-line:variable-name
66-
ordinal: function (number, period) {
67-
switch (period) {
68-
// TODO: Return 'e' when day of month > 1. Move this case inside
69-
// block for masculine words below.
70-
// See https://github.com/moment/moment/issues/3375
71-
case 'D':
72-
return number + (number === 1 ? 'er' : '');
73-
74-
// Words with masculine grammatical gender: mois, trimestre, jour
75-
default:
76-
case 'M':
77-
case 'Q':
78-
case 'DDD':
79-
case 'd':
80-
return number + (number === 1 ? 'er' : 'e');
81-
82-
// Words with feminine grammatical gender: semaine
83-
case 'w':
84-
case 'W':
85-
return number + (number === 1 ? 're' : 'e');
86-
}
87-
},
88-
week: {
89-
dow: 1, // Monday is the first day of the week.
90-
doy: 4 // The week that contains Jan 4th is the first week of the year.
91-
}
92-
} as any);
93-
94-
moment.defineLocale('ja', {
95-
months: '1月_2月_3月_4月_5月_6月_7月_8月_9月_10月_11月_12月'.split('_'),
96-
monthsShort: '1月_2月_3月_4月_5月_6月_7月_8月_9月_10月_11月_12月'.split('_'),
97-
weekdays: '日曜日_月曜日_火曜日_水曜日_木曜日_金曜日_土曜日'.split('_'),
98-
weekdaysShort: '日_月_火_水_木_金_土'.split('_'),
99-
weekdaysMin: '日_月_火_水_木_金_土'.split('_'),
100-
longDateFormat: {
101-
LT: 'HH:mm',
102-
LTS: 'HH:mm:ss',
103-
L: 'YYYY/MM/DD',
104-
LL: 'YYYY年M月D日',
105-
LLL: 'YYYY年M月D日 HH:mm',
106-
LLLL: 'YYYY年M月D日 HH:mm dddd',
107-
l: 'YYYY/MM/DD',
108-
ll: 'YYYY年M月D日',
109-
lll: 'YYYY年M月D日 HH:mm',
110-
llll: 'YYYY年M月D日 HH:mm dddd'
111-
},
112-
meridiemParse: /|/i,
113-
isPM: function (input) {
114-
return input === '午後';
115-
},
116-
meridiem: function (hour) {
117-
if (hour < 12) {
118-
return '午前';
119-
} else {
120-
return '午後';
121-
}
122-
},
123-
calendar: {
124-
sameDay: '[今日] LT',
125-
nextDay: '[明日] LT',
126-
nextWeek: '[来週]dddd LT',
127-
lastDay: '[昨日] LT',
128-
lastWeek: '[前週]dddd LT',
129-
sameElse: 'L'
130-
},
131-
dayOfMonthOrdinalParse: /\d{1,2}/,
132-
// tslint:disable-next-line:variable-name
133-
ordinal: function (number, period) {
134-
switch (period) {
135-
case 'd':
136-
case 'D':
137-
case 'DDD':
138-
return number + '日';
139-
default:
140-
return number;
141-
}
142-
},
143-
relativeTime: {
144-
future: '%s後',
145-
past: '%s前',
146-
s: '数秒',
147-
m: '1分',
148-
mm: '%d分',
149-
h: '1時間',
150-
hh: '%d時間',
151-
d: '1日',
152-
dd: '%d日',
153-
M: '1ヶ月',
154-
MM: '%dヶ月',
155-
y: '1年',
156-
yy: '%d年'
157-
}
158-
} as any);
159-
160-
moment.locale('en');
161-
162-
16321
describe('MomentDateAdapter', () => {
16422
let adapter: MomentDateAdapter;
16523

@@ -170,6 +28,7 @@ describe('MomentDateAdapter', () => {
17028
}));
17129

17230
beforeEach(inject([DateAdapter], (d: MomentDateAdapter) => {
31+
moment.locale('en');
17332
adapter = d;
17433
adapter.setLocale('en');
17534
}));

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

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ function range<T>(length: number, valueFunction: (index: number) => T): T[] {
2929
/** Adapts Moment.js Dates for use with Angular Material. */
3030
@Injectable()
3131
export class MomentDateAdapter extends DateAdapter<Moment> {
32-
// Note: all of the methods that accept a `Moment` input parameter immediately call
33-
// `this.clone` on it. This is to ensure that we're working with a `Moment` that has the
34-
// correct locale setting while avoiding mutating the original object passed to us.
32+
// Note: all of the methods that accept a `Moment` input parameter immediately call `this.clone`
33+
// on it. This is to ensure that we're working with a `Moment` that has the correct locale setting
34+
// while avoiding mutating the original object passed to us. Just calling `.locale(...)` on the
35+
// input would mutate the object.
3536

3637
private _localeData: {
3738
firstDayOfWeek: number,
@@ -48,22 +49,19 @@ export class MomentDateAdapter extends DateAdapter<Moment> {
4849
this.setLocale(dateLocale || moment.locale());
4950
}
5051

51-
setLocale(locale: any) {
52+
setLocale(locale: string) {
5253
super.setLocale(locale);
5354

54-
// Temporarily change the global locale to get the data we need, then change it back.
55-
let globalLocale = moment.locale();
56-
moment.locale(locale);
55+
let momentLocaleData = moment.localeData(locale);
5756
this._localeData = {
58-
firstDayOfWeek: moment.localeData().firstDayOfWeek(),
59-
longMonths: moment.months(),
60-
shortMonths: moment.monthsShort(),
57+
firstDayOfWeek: momentLocaleData.firstDayOfWeek(),
58+
longMonths: momentLocaleData.months(),
59+
shortMonths: momentLocaleData.monthsShort(),
6160
dates: range(31, (i) => this.createDate(2017, 0, i + 1).format('D')),
62-
longDaysOfWeek: moment.weekdays(),
63-
shortDaysOfWeek: moment.weekdaysShort(),
64-
narrowDaysOfWeek: moment.weekdaysMin(),
61+
longDaysOfWeek: momentLocaleData.weekdays(),
62+
shortDaysOfWeek: momentLocaleData.weekdaysShort(),
63+
narrowDaysOfWeek: momentLocaleData.weekdaysMin(),
6564
};
66-
moment.locale(globalLocale);
6765
}
6866

6967
getYear(date: Moment): number {
@@ -118,8 +116,8 @@ export class MomentDateAdapter extends DateAdapter<Moment> {
118116
}
119117

120118
createDate(year: number, month: number, date: number): Moment {
121-
// Check for invalid month and date (except upper bound on date which we have to check after
122-
// creating the Date).
119+
// Moment.js will create an invalid date if any of the components are out of bounds, but we
120+
// explicitly check each case so we can throw more descriptive errors.
123121
if (month < 0 || month > 11) {
124122
throw Error(`Invalid month index "${month}". Month index has to be between 0 and 11.`);
125123
}

test/karma-test-shim.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ System.config({
2020
map: {
2121
'rxjs': 'node:rxjs',
2222
'main': 'main.js',
23-
'moment': 'node:moment/min/moment.min.js',
23+
'moment': 'node:moment/min/moment-with-locales.min.js',
2424

2525
// Angular specific mappings.
2626
'@angular/core': 'node:@angular/core/bundles/core.umd.js',

test/karma.conf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ module.exports = (config) => {
2626
{pattern: 'node_modules/zone.js/dist/fake-async-test.js', included: true, watched: false},
2727
{pattern: 'node_modules/hammerjs/hammer.min.js', included: true, watched: false},
2828
{pattern: 'node_modules/hammerjs/hammer.min.js.map', included: false, watched: false},
29-
{pattern: 'node_modules/moment/min/moment.min.js', included: true, watched: false},
29+
{pattern: 'node_modules/moment/min/moment-with-locales.min.js', included: true, watched: false},
3030

3131
// Include all Angular dependencies
3232
{pattern: 'node_modules/@angular/**/*', included: false, watched: false},

0 commit comments

Comments
 (0)