Skip to content

Commit cbeb48e

Browse files
committed
fix moment import issues with tests
1 parent e5f3d05 commit cbeb48e

File tree

5 files changed

+26
-27
lines changed

5 files changed

+26
-27
lines changed

src/demo-app/tsconfig-aot.json

-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
{
55
"extends": "./tsconfig-build",
66
"compilerOptions": {
7-
// Needed for Moment.js since it doesn't have a default export.
8-
"allowSyntheticDefaultImports": true,
97
"experimentalDecorators": true,
108
// TODO(paul): Remove once Angular has been upgraded and supports noUnusedParameters in AOT.
119
"noUnusedParameters": false,

src/demo-app/tsconfig-build.json

-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// since the demo-app will be served in the browser.
33
{
44
"compilerOptions": {
5-
// Needed for Moment.js since it doesn't have a default export.
6-
"allowSyntheticDefaultImports": true,
75
"declaration": false,
86
"emitDecoratorMetadata": true,
97
"experimentalDecorators": true,

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ 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 moment from 'moment';
13+
import * as moment from 'moment';
1414
import {LOCALE_ID} from '@angular/core';
1515

1616

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

+25-20
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@
88

99
import {Inject, Injectable, Optional} from '@angular/core';
1010
import {DateAdapter, MAT_DATE_LOCALE} from '@angular/material';
11-
import moment from 'moment';
11+
12+
// Depending on whether rollup is used, moment needs to be imported differently.
13+
// TODO(mmalerba): See if we can clean this up at some point.
14+
import {default as _rollupMoment, Moment} from 'moment';
15+
import * as _moment from 'moment';
16+
const moment = _rollupMoment || _moment;
1217

1318

1419
/** Creates an array and fills it with values. */
@@ -23,9 +28,9 @@ function range<T>(length: number, valueFunction: (index: number) => T): T[] {
2328

2429
/** Adapts Moment.js Dates for use with Angular Material. */
2530
@Injectable()
26-
export class MomentDateAdapter extends DateAdapter<moment.Moment> {
27-
// Note: all of the methods that accept a `moment.Moment` input parameter immediately call
28-
// `this.clone` on it. This is to ensure that we're working with a `moment.Moment` that has the
31+
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
2934
// correct locale setting while avoiding mutating the original object passed to us.
3035

3136
private _localeData: {
@@ -61,19 +66,19 @@ export class MomentDateAdapter extends DateAdapter<moment.Moment> {
6166
moment.locale(globalLocale);
6267
}
6368

64-
getYear(date: moment.Moment): number {
69+
getYear(date: Moment): number {
6570
return this.clone(date).year();
6671
}
6772

68-
getMonth(date: moment.Moment): number {
73+
getMonth(date: Moment): number {
6974
return this.clone(date).month();
7075
}
7176

72-
getDate(date: moment.Moment): number {
77+
getDate(date: Moment): number {
7378
return this.clone(date).date();
7479
}
7580

76-
getDayOfWeek(date: moment.Moment): number {
81+
getDayOfWeek(date: Moment): number {
7782
return this.clone(date).day();
7883
}
7984

@@ -96,23 +101,23 @@ export class MomentDateAdapter extends DateAdapter<moment.Moment> {
96101
return this._localeData.narrowDaysOfWeek;
97102
}
98103

99-
getYearName(date: moment.Moment): string {
104+
getYearName(date: Moment): string {
100105
return this.clone(date).format('YYYY');
101106
}
102107

103108
getFirstDayOfWeek(): number {
104109
return this._localeData.firstDayOfWeek;
105110
}
106111

107-
getNumDaysInMonth(date: moment.Moment): number {
112+
getNumDaysInMonth(date: Moment): number {
108113
return this.clone(date).daysInMonth();
109114
}
110115

111-
clone(date: moment.Moment): moment.Moment {
116+
clone(date: Moment): Moment {
112117
return date.clone().locale(this.locale);
113118
}
114119

115-
createDate(year: number, month: number, date: number): moment.Moment {
120+
createDate(year: number, month: number, date: number): Moment {
116121
// Check for invalid month and date (except upper bound on date which we have to check after
117122
// creating the Date).
118123
if (month < 0 || month > 11) {
@@ -133,46 +138,46 @@ export class MomentDateAdapter extends DateAdapter<moment.Moment> {
133138
return result;
134139
}
135140

136-
today(): moment.Moment {
141+
today(): Moment {
137142
return moment().locale(this.locale);
138143
}
139144

140-
parse(value: any, parseFormat: string | string[]): moment.Moment | null {
145+
parse(value: any, parseFormat: string | string[]): Moment | null {
141146
if (value && typeof value == 'string') {
142147
return moment(value, parseFormat, this.locale);
143148
}
144149
return value ? moment(value).locale(this.locale) : null;
145150
}
146151

147-
format(date: moment.Moment, displayFormat: string): string {
152+
format(date: Moment, displayFormat: string): string {
148153
date = this.clone(date);
149154
if (!this.isValid(date)) {
150155
throw Error('MomentDateAdapter: Cannot format invalid date.');
151156
}
152157
return date.format(displayFormat);
153158
}
154159

155-
addCalendarYears(date: moment.Moment, years: number): moment.Moment {
160+
addCalendarYears(date: Moment, years: number): Moment {
156161
return this.clone(date).add({years});
157162
}
158163

159-
addCalendarMonths(date: moment.Moment, months: number): moment.Moment {
164+
addCalendarMonths(date: Moment, months: number): Moment {
160165
return this.clone(date).add({months});
161166
}
162167

163-
addCalendarDays(date: moment.Moment, days: number): moment.Moment {
168+
addCalendarDays(date: Moment, days: number): Moment {
164169
return this.clone(date).add({days});
165170
}
166171

167-
getISODateString(date: moment.Moment): string {
172+
getISODateString(date: Moment): string {
168173
return this.clone(date).format();
169174
}
170175

171176
isDateInstance(obj: any): boolean {
172177
return moment.isMoment(obj);
173178
}
174179

175-
isValid(date: moment.Moment): boolean {
180+
isValid(date: Moment): boolean {
176181
return this.clone(date).isValid();
177182
}
178183
}

src/material-moment-adapter/tsconfig-tests.json

-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
{
55
"extends": "./tsconfig-build",
66
"compilerOptions": {
7-
// Needed for Moment.js since it doesn't have a default export.
8-
"allowSyntheticDefaultImports": true,
97
"importHelpers": false,
108
"module": "commonjs",
119
"target": "es5",

0 commit comments

Comments
 (0)