Skip to content

Commit

Permalink
Merge pull request #274 from Delagen/moment-update-import
Browse files Browse the repository at this point in the history
fix: moment import reworked from wildcard to default
  • Loading branch information
Delagen committed Dec 22, 2021
2 parents 2db80bb + f2ce097 commit 41083d6
Show file tree
Hide file tree
Showing 29 changed files with 51 additions and 74 deletions.
2 changes: 1 addition & 1 deletion src/add.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'reflect-metadata';
import * as moment from 'moment';
import moment from 'moment';
import { AddPipe } from './add.pipe';

describe('AddPipe', () => {
Expand Down
6 changes: 2 additions & 4 deletions src/add.pipe.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/calendar.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
12 changes: 5 additions & 7 deletions src/calendar.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion src/date-format.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as moment from 'moment';
import moment from 'moment';
import { DateFormatPipe } from './date-format.pipe';

describe('DateFormatPipe', () => {
Expand Down
6 changes: 2 additions & 4 deletions src/date-format.pipe.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
/* 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 {
transform(value: moment.MomentInput, ...args: any[]): string {
if (!value) {
return '';
}
return momentConstructor(value).format(args[0]);
return moment(value).format(args[0]);
}
}
8 changes: 3 additions & 5 deletions src/difference.pipe.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/duration.pipe.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
2 changes: 1 addition & 1 deletion src/from-unix.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as moment from 'moment';
import moment from 'moment';
import { FromUnixPipe } from './from-unix.pipe';

describe('FromUnixPipe', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/from-unix.pipe.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/from-utc.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
2 changes: 1 addition & 1 deletion src/from-utc.pipe.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
6 changes: 2 additions & 4 deletions src/is-after.pipe.ts
Original file line number Diff line number Diff line change
@@ -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',
})
Expand All @@ -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);
}
}
6 changes: 2 additions & 4 deletions src/is-before.pipe.ts
Original file line number Diff line number Diff line change
@@ -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',
})
Expand All @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/local.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as moment from 'moment';
import moment from 'moment';
import { DateFormatPipe } from './date-format.pipe';
import { LocalTimePipe } from './local.pipe';

Expand Down
6 changes: 2 additions & 4 deletions src/local.pipe.ts
Original file line number Diff line number Diff line change
@@ -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();
}
}
2 changes: 1 addition & 1 deletion src/locale.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as moment from 'moment';
import moment from 'moment';
import { DateFormatPipe } from './date-format.pipe';
import { LocalePipe } from './locale.pipe';

Expand Down
7 changes: 2 additions & 5 deletions src/locale.pipe.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
2 changes: 1 addition & 1 deletion src/parse-zone.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as moment from 'moment';
import moment from 'moment';
import { ParseZonePipe } from './parse-zone.pipe';

describe('ParseZonePipe', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/parse-zone.pipe.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/parse.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as moment from 'moment';
import moment from 'moment';
import { DateFormatPipe } from './date-format.pipe';
import { ParsePipe } from './parse.pipe';

Expand Down
6 changes: 2 additions & 4 deletions src/parse.pipe.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
2 changes: 1 addition & 1 deletion src/subtract.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as moment from 'moment';
import moment from 'moment';
import { SubtractPipe } from './subtract.pipe';

describe('SubtractPipe', () => {
Expand Down
6 changes: 2 additions & 4 deletions src/subtract.pipe.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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);
}
}
2 changes: 1 addition & 1 deletion src/time-ago.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
16 changes: 7 additions & 9 deletions src/time-ago.pipe.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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(
Expand All @@ -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();
}
Expand All @@ -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());
Expand All @@ -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) {
Expand All @@ -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();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/utc.pipe.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as moment from 'moment';
import moment from 'moment';
import { DateFormatPipe } from './date-format.pipe';
import { UtcPipe } from './utc.pipe';

Expand Down
6 changes: 2 additions & 4 deletions src/utc.pipe.ts
Original file line number Diff line number Diff line change
@@ -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();
}
}
2 changes: 2 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"inlineSources": true,
"downlevelIteration": true,
"experimentalDecorators": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"moduleResolution": "node",
"importHelpers": true,
"target": "es2017",
Expand Down

0 comments on commit 41083d6

Please sign in to comment.