From f2ce0977cdd0182f73d14897af9e36ab15d5982b Mon Sep 17 00:00:00 2001 From: Sergey Vohmyanin Date: Mon, 20 Dec 2021 15:50:50 +0300 Subject: [PATCH] fix: moment import reworked from wildcard to default --- src/add.pipe.spec.ts | 2 +- src/add.pipe.ts | 6 ++---- src/calendar.pipe.spec.ts | 2 +- src/calendar.pipe.ts | 12 +++++------- src/date-format.pipe.spec.ts | 2 +- src/date-format.pipe.ts | 6 ++---- src/difference.pipe.ts | 8 +++----- src/duration.pipe.ts | 2 +- src/from-unix.pipe.spec.ts | 2 +- src/from-unix.pipe.ts | 2 +- src/from-utc.pipe.spec.ts | 2 +- src/from-utc.pipe.ts | 2 +- src/is-after.pipe.ts | 6 ++---- src/is-before.pipe.ts | 6 ++---- src/local.pipe.spec.ts | 2 +- src/local.pipe.ts | 6 ++---- src/locale.pipe.spec.ts | 2 +- src/locale.pipe.ts | 7 ++----- src/parse-zone.pipe.spec.ts | 2 +- src/parse-zone.pipe.ts | 2 +- src/parse.pipe.spec.ts | 2 +- src/parse.pipe.ts | 6 ++---- src/subtract.pipe.spec.ts | 2 +- src/subtract.pipe.ts | 6 ++---- src/time-ago.pipe.spec.ts | 2 +- src/time-ago.pipe.ts | 16 +++++++--------- src/utc.pipe.spec.ts | 2 +- src/utc.pipe.ts | 6 ++---- tsconfig.json | 2 ++ 29 files changed, 51 insertions(+), 74 deletions(-) diff --git a/src/add.pipe.spec.ts b/src/add.pipe.spec.ts index 98e0e77..933c3fa 100644 --- a/src/add.pipe.spec.ts +++ b/src/add.pipe.spec.ts @@ -1,5 +1,5 @@ import 'reflect-metadata'; -import * as moment from 'moment'; +import moment from 'moment'; import { AddPipe } from './add.pipe'; describe('AddPipe', () => { diff --git a/src/add.pipe.ts b/src/add.pipe.ts index 59ddc55..04489ca 100644 --- a/src/add.pipe.ts +++ b/src/add.pipe.ts @@ -1,9 +1,7 @@ /* ngx-moment (c) 2015, 2016 Uri Shaked / MIT Licence */ import { Pipe, PipeTransform } from '@angular/core'; -import * as moment from 'moment'; - -const momentConstructor = moment; +import moment from 'moment'; @Pipe({ name: 'amAdd' }) export class AddPipe implements PipeTransform { @@ -18,6 +16,6 @@ export class AddPipe implements PipeTransform { ) { throw new Error('AddPipe: missing required arguments'); } - return momentConstructor(value).add(amount, unit); + return moment(value).add(amount, unit); } } diff --git a/src/calendar.pipe.spec.ts b/src/calendar.pipe.spec.ts index 62c3438..bd3bcc8 100644 --- a/src/calendar.pipe.spec.ts +++ b/src/calendar.pipe.spec.ts @@ -1,5 +1,5 @@ import 'reflect-metadata'; -import * as moment from 'moment'; +import moment from 'moment'; import { NgZone } from '@angular/core'; import { CalendarPipe } from './calendar.pipe'; diff --git a/src/calendar.pipe.ts b/src/calendar.pipe.ts index 912aacf..462bffa 100644 --- a/src/calendar.pipe.ts +++ b/src/calendar.pipe.ts @@ -8,11 +8,9 @@ import { OnDestroy, NgZone, } from '@angular/core'; -import * as moment from 'moment'; +import moment from 'moment'; import { Subscription } from 'rxjs'; -const momentConstructor = moment; - @Pipe({ name: 'amCalendar', pure: false }) export class CalendarPipe implements PipeTransform, OnDestroy { /** @@ -47,12 +45,12 @@ export class CalendarPipe implements PipeTransform, OnDestroy { if (typeof args[i] === 'object' && !moment.isMoment(args[i])) { formats = args[i]; } else { - referenceTime = momentConstructor(args[i]); + referenceTime = moment(args[i]); } } } - return momentConstructor(value).calendar(referenceTime, formats); + return moment(value).calendar(referenceTime, formats); } ngOnDestroy(): void { @@ -96,8 +94,8 @@ export class CalendarPipe implements PipeTransform, OnDestroy { } private static _getMillisecondsUntilUpdate() { - const now = momentConstructor(); - const tomorrow = momentConstructor().startOf('day').add(1, 'days'); + const now = moment(); + const tomorrow = moment().startOf('day').add(1, 'days'); const timeToMidnight = tomorrow.valueOf() - now.valueOf(); return timeToMidnight + 1000; // 1 second after midnight } diff --git a/src/date-format.pipe.spec.ts b/src/date-format.pipe.spec.ts index 3cd8545..a5c02cb 100644 --- a/src/date-format.pipe.spec.ts +++ b/src/date-format.pipe.spec.ts @@ -1,4 +1,4 @@ -import * as moment from 'moment'; +import moment from 'moment'; import { DateFormatPipe } from './date-format.pipe'; describe('DateFormatPipe', () => { diff --git a/src/date-format.pipe.ts b/src/date-format.pipe.ts index 5333b4d..1e2dfc5 100644 --- a/src/date-format.pipe.ts +++ b/src/date-format.pipe.ts @@ -1,9 +1,7 @@ /* ngx-moment (c) 2015, 2016 Uri Shaked / MIT Licence */ import { Pipe, PipeTransform } from '@angular/core'; -import * as moment from 'moment'; - -const momentConstructor = moment; +import moment from 'moment'; @Pipe({ name: 'amDateFormat' }) export class DateFormatPipe implements PipeTransform { @@ -11,6 +9,6 @@ export class DateFormatPipe implements PipeTransform { if (!value) { return ''; } - return momentConstructor(value).format(args[0]); + return moment(value).format(args[0]); } } diff --git a/src/difference.pipe.ts b/src/difference.pipe.ts index 4832da0..56dc838 100644 --- a/src/difference.pipe.ts +++ b/src/difference.pipe.ts @@ -1,9 +1,7 @@ /* ngx-moment (c) 2015, 2016 Uri Shaked / MIT Licence */ import { Pipe, PipeTransform } from '@angular/core'; -import * as moment from 'moment'; - -const momentConstructor = moment; +import moment from 'moment'; @Pipe({ name: 'amDifference' }) export class DifferencePipe implements PipeTransform { @@ -13,8 +11,8 @@ export class DifferencePipe implements PipeTransform { unit?: moment.unitOfTime.Diff, precision?: boolean, ): number { - const date = momentConstructor(value); - const date2 = otherValue !== null ? momentConstructor(otherValue) : momentConstructor(); + const date = moment(value); + const date2 = otherValue !== null ? moment(otherValue) : moment(); return date.diff(date2, unit, precision); } diff --git a/src/duration.pipe.ts b/src/duration.pipe.ts index 38da2f8..ae6e80b 100644 --- a/src/duration.pipe.ts +++ b/src/duration.pipe.ts @@ -1,4 +1,4 @@ -import * as moment from 'moment'; +import moment from 'moment'; import { Inject, Optional, Pipe, PipeTransform } from '@angular/core'; import { NGX_MOMENT_OPTIONS, NgxMomentOptions } from './moment-options'; diff --git a/src/from-unix.pipe.spec.ts b/src/from-unix.pipe.spec.ts index 766f32f..b37c7df 100644 --- a/src/from-unix.pipe.spec.ts +++ b/src/from-unix.pipe.spec.ts @@ -1,4 +1,4 @@ -import * as moment from 'moment'; +import moment from 'moment'; import { FromUnixPipe } from './from-unix.pipe'; describe('FromUnixPipe', () => { diff --git a/src/from-unix.pipe.ts b/src/from-unix.pipe.ts index f6cee4a..26eaa9f 100644 --- a/src/from-unix.pipe.ts +++ b/src/from-unix.pipe.ts @@ -1,7 +1,7 @@ /* ngx-moment (c) 2015, 2016 Uri Shaked / MIT Licence */ import { Pipe, PipeTransform } from '@angular/core'; -import * as moment from 'moment'; +import moment from 'moment'; @Pipe({ name: 'amFromUnix' }) export class FromUnixPipe implements PipeTransform { diff --git a/src/from-utc.pipe.spec.ts b/src/from-utc.pipe.spec.ts index 22734bd..db52b8b 100644 --- a/src/from-utc.pipe.spec.ts +++ b/src/from-utc.pipe.spec.ts @@ -1,4 +1,4 @@ -import * as moment from 'moment'; +import moment from 'moment'; import { DateFormatPipe } from './date-format.pipe'; import { FromUtcPipe } from './from-utc.pipe'; diff --git a/src/from-utc.pipe.ts b/src/from-utc.pipe.ts index 2d89fcc..063cc55 100644 --- a/src/from-utc.pipe.ts +++ b/src/from-utc.pipe.ts @@ -1,7 +1,7 @@ /* ngx-moment (c) 2015, 2016 Uri Shaked / MIT Licence */ import { Pipe, PipeTransform } from '@angular/core'; -import * as moment from 'moment'; +import moment from 'moment'; @Pipe({ name: 'amFromUtc' }) export class FromUtcPipe implements PipeTransform { diff --git a/src/is-after.pipe.ts b/src/is-after.pipe.ts index 083bb08..3232a75 100644 --- a/src/is-after.pipe.ts +++ b/src/is-after.pipe.ts @@ -1,9 +1,7 @@ -import * as moment from 'moment'; +import moment from 'moment'; import { Pipe, PipeTransform } from '@angular/core'; -const momentConstructor = moment; - @Pipe({ name: 'amIsAfter', }) @@ -13,6 +11,6 @@ export class IsAfterPipe implements PipeTransform { otherValue: moment.MomentInput, unit?: moment.unitOfTime.StartOf, ): boolean { - return momentConstructor(value).isAfter(momentConstructor(otherValue), unit); + return moment(value).isAfter(moment(otherValue), unit); } } diff --git a/src/is-before.pipe.ts b/src/is-before.pipe.ts index 7f0e552..c057786 100644 --- a/src/is-before.pipe.ts +++ b/src/is-before.pipe.ts @@ -1,9 +1,7 @@ -import * as moment from 'moment'; +import moment from 'moment'; import { Pipe, PipeTransform } from '@angular/core'; -const momentConstructor = moment; - @Pipe({ name: 'amIsBefore', }) @@ -13,6 +11,6 @@ export class IsBeforePipe implements PipeTransform { otherValue: moment.MomentInput, unit?: moment.unitOfTime.StartOf, ): boolean { - return momentConstructor(value).isBefore(momentConstructor(otherValue), unit); + return moment(value).isBefore(moment(otherValue), unit); } } diff --git a/src/local.pipe.spec.ts b/src/local.pipe.spec.ts index 7fa13de..f8cf429 100644 --- a/src/local.pipe.spec.ts +++ b/src/local.pipe.spec.ts @@ -1,4 +1,4 @@ -import * as moment from 'moment'; +import moment from 'moment'; import { DateFormatPipe } from './date-format.pipe'; import { LocalTimePipe } from './local.pipe'; diff --git a/src/local.pipe.ts b/src/local.pipe.ts index 02f0dbe..b7cdf5d 100644 --- a/src/local.pipe.ts +++ b/src/local.pipe.ts @@ -1,11 +1,9 @@ import { Pipe, PipeTransform } from '@angular/core'; -import * as moment from 'moment'; - -const momentConstructor = moment; +import moment from 'moment'; @Pipe({ name: 'amLocal' }) export class LocalTimePipe implements PipeTransform { transform(value: moment.MomentInput): moment.Moment { - return momentConstructor(value).local(); + return moment(value).local(); } } diff --git a/src/locale.pipe.spec.ts b/src/locale.pipe.spec.ts index b4f1ffb..8f76202 100644 --- a/src/locale.pipe.spec.ts +++ b/src/locale.pipe.spec.ts @@ -1,4 +1,4 @@ -import * as moment from 'moment'; +import moment from 'moment'; import { DateFormatPipe } from './date-format.pipe'; import { LocalePipe } from './locale.pipe'; diff --git a/src/locale.pipe.ts b/src/locale.pipe.ts index 94f86ce..071fb5f 100644 --- a/src/locale.pipe.ts +++ b/src/locale.pipe.ts @@ -1,12 +1,9 @@ import { Pipe, PipeTransform } from '@angular/core'; -import * as moment from 'moment'; - -// See https://github.com/ng-packagr/ng-packagr/issues/217 for why this is needed: -const momentConstructor = moment; +import moment from 'moment'; @Pipe({ name: 'amLocale' }) export class LocalePipe implements PipeTransform { transform(value: moment.MomentInput, locale: string): moment.Moment { - return momentConstructor(value).locale(locale); + return moment(value).locale(locale); } } diff --git a/src/parse-zone.pipe.spec.ts b/src/parse-zone.pipe.spec.ts index ea4e9e4..c147ed5 100644 --- a/src/parse-zone.pipe.spec.ts +++ b/src/parse-zone.pipe.spec.ts @@ -1,4 +1,4 @@ -import * as moment from 'moment'; +import moment from 'moment'; import { ParseZonePipe } from './parse-zone.pipe'; describe('ParseZonePipe', () => { diff --git a/src/parse-zone.pipe.ts b/src/parse-zone.pipe.ts index a3d67aa..17a1e9f 100644 --- a/src/parse-zone.pipe.ts +++ b/src/parse-zone.pipe.ts @@ -1,5 +1,5 @@ import { Pipe, PipeTransform } from '@angular/core'; -import * as moment from 'moment'; +import moment from 'moment'; @Pipe({ name: 'amParseZone' }) export class ParseZonePipe implements PipeTransform { diff --git a/src/parse.pipe.spec.ts b/src/parse.pipe.spec.ts index fd9fd7e..f61ea4b 100644 --- a/src/parse.pipe.spec.ts +++ b/src/parse.pipe.spec.ts @@ -1,4 +1,4 @@ -import * as moment from 'moment'; +import moment from 'moment'; import { DateFormatPipe } from './date-format.pipe'; import { ParsePipe } from './parse.pipe'; diff --git a/src/parse.pipe.ts b/src/parse.pipe.ts index cd2d1f9..dfe1b23 100644 --- a/src/parse.pipe.ts +++ b/src/parse.pipe.ts @@ -1,11 +1,9 @@ import { Pipe, PipeTransform } from '@angular/core'; -import * as moment from 'moment'; - -const momentConstructor = moment; +import moment from 'moment'; @Pipe({ name: 'amParse' }) export class ParsePipe implements PipeTransform { transform(value: moment.MomentInput, formats: string | string[]): moment.Moment { - return momentConstructor(value, formats); + return moment(value, formats); } } diff --git a/src/subtract.pipe.spec.ts b/src/subtract.pipe.spec.ts index 0320b1b..48ea362 100644 --- a/src/subtract.pipe.spec.ts +++ b/src/subtract.pipe.spec.ts @@ -1,4 +1,4 @@ -import * as moment from 'moment'; +import moment from 'moment'; import { SubtractPipe } from './subtract.pipe'; describe('SubtractPipe', () => { diff --git a/src/subtract.pipe.ts b/src/subtract.pipe.ts index b20fad6..dceeb60 100644 --- a/src/subtract.pipe.ts +++ b/src/subtract.pipe.ts @@ -1,9 +1,7 @@ /* ngx-moment (c) 2015, 2016 Uri Shaked / MIT Licence */ import { Pipe, PipeTransform } from '@angular/core'; -import * as moment from 'moment'; - -const momentConstructor = moment; +import moment from 'moment'; @Pipe({ name: 'amSubtract' }) export class SubtractPipe implements PipeTransform { @@ -18,6 +16,6 @@ export class SubtractPipe implements PipeTransform { ) { throw new Error('SubtractPipe: missing required arguments'); } - return momentConstructor(value).subtract(amount, unit); + return moment(value).subtract(amount, unit); } } diff --git a/src/time-ago.pipe.spec.ts b/src/time-ago.pipe.spec.ts index 72afc8a..07a3b93 100644 --- a/src/time-ago.pipe.spec.ts +++ b/src/time-ago.pipe.spec.ts @@ -1,6 +1,6 @@ import { NgZone } from '@angular/core'; import { TimeAgoPipe } from './time-ago.pipe'; -import * as moment from 'moment'; +import moment from 'moment'; import 'moment/min/locales'; declare var global: any; diff --git a/src/time-ago.pipe.ts b/src/time-ago.pipe.ts index 1084d80..bdd1d55 100644 --- a/src/time-ago.pipe.ts +++ b/src/time-ago.pipe.ts @@ -1,9 +1,7 @@ /* ngx-moment (c) 2015, 2016 Uri Shaked / MIT Licence */ import { Pipe, ChangeDetectorRef, PipeTransform, OnDestroy, NgZone } from '@angular/core'; -import * as moment from 'moment'; - -const momentConstructor = moment; +import moment from 'moment'; @Pipe({ name: 'amTimeAgo', pure: false }) export class TimeAgoPipe implements PipeTransform, OnDestroy { @@ -19,7 +17,7 @@ export class TimeAgoPipe implements PipeTransform, OnDestroy { constructor(private cdRef: ChangeDetectorRef, private ngZone: NgZone) {} format(m: moment.Moment) { - return m.from(momentConstructor(), this.lastOmitSuffix); + return m.from(moment(), this.lastOmitSuffix); } transform( @@ -35,7 +33,7 @@ export class TimeAgoPipe implements PipeTransform, OnDestroy { this.formatFn = formatFn || this.format.bind(this); this.removeTimer(); this.createTimer(); - this.lastText = this.formatFn(momentConstructor(value)); + this.lastText = this.formatFn(moment(value)); } else { this.createTimer(); } @@ -52,13 +50,13 @@ export class TimeAgoPipe implements PipeTransform, OnDestroy { return; } - const momentInstance = momentConstructor(this.lastValue); + const momentInstance = moment(this.lastValue); const timeToUpdate = this.getSecondsUntilUpdate(momentInstance) * 1000; this.currentTimer = this.ngZone.runOutsideAngular(() => { if (typeof window !== 'undefined') { return window.setTimeout(() => { - this.lastText = this.formatFn(momentConstructor(this.lastValue)); + this.lastText = this.formatFn(moment(this.lastValue)); this.currentTimer = null; this.ngZone.run(() => this.cdRef.markForCheck()); @@ -77,7 +75,7 @@ export class TimeAgoPipe implements PipeTransform, OnDestroy { } private getSecondsUntilUpdate(momentInstance: moment.Moment) { - const howOld = Math.abs(momentConstructor().diff(momentInstance, 'minute')); + const howOld = Math.abs(moment().diff(momentInstance, 'minute')); if (howOld < 1) { return 1; } else if (howOld < 60) { @@ -103,7 +101,7 @@ export class TimeAgoPipe implements PipeTransform, OnDestroy { } else if (moment.isMoment(value)) { return value.valueOf(); } else { - return momentConstructor(value).valueOf(); + return moment(value).valueOf(); } } diff --git a/src/utc.pipe.spec.ts b/src/utc.pipe.spec.ts index 0469869..7579e73 100644 --- a/src/utc.pipe.spec.ts +++ b/src/utc.pipe.spec.ts @@ -1,4 +1,4 @@ -import * as moment from 'moment'; +import moment from 'moment'; import { DateFormatPipe } from './date-format.pipe'; import { UtcPipe } from './utc.pipe'; diff --git a/src/utc.pipe.ts b/src/utc.pipe.ts index f062f37..eb61a33 100644 --- a/src/utc.pipe.ts +++ b/src/utc.pipe.ts @@ -1,11 +1,9 @@ import { Pipe, PipeTransform } from '@angular/core'; -import * as moment from 'moment'; - -const momentConstructor = moment; +import moment from 'moment'; @Pipe({ name: 'amUtc' }) export class UtcPipe implements PipeTransform { transform(value: moment.MomentInput): moment.Moment { - return momentConstructor(value).utc(); + return moment(value).utc(); } } diff --git a/tsconfig.json b/tsconfig.json index fef8830..10df425 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -17,6 +17,8 @@ "inlineSources": true, "downlevelIteration": true, "experimentalDecorators": true, + "esModuleInterop": true, + "allowSyntheticDefaultImports": true, "moduleResolution": "node", "importHelpers": true, "target": "es2017",