Skip to content

Commit

Permalink
fix: correct input types for pipes
Browse files Browse the repository at this point in the history
use `moment.MomentInput` type consistently across all pipes
  • Loading branch information
urish committed Jun 12, 2020
1 parent b977359 commit 906e40c
Show file tree
Hide file tree
Showing 15 changed files with 20 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/add.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const momentConstructor = moment;

@Pipe({ name: 'amAdd' })
export class AddPipe implements PipeTransform {
transform(value: any, amount: moment.DurationInputArg1, unit?: moment.DurationInputArg2): any {
transform(value: moment.MomentInput, amount: moment.DurationInputArg1, unit?: moment.DurationInputArg2): any {
if (typeof amount === 'undefined' || (typeof amount === 'number' && typeof unit === 'undefined')) {
throw new Error('AddPipe: missing required arguments');
}
Expand Down
2 changes: 1 addition & 1 deletion src/calendar.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class CalendarPipe implements PipeTransform, OnDestroy {
});
}

transform(value: Date | moment.Moment, ...args: any[]): any {
transform(value: moment.MomentInput, ...args: any[]): any {
let formats: any = null;
let referenceTime: any = null;

Expand Down
2 changes: 1 addition & 1 deletion src/date-format.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const momentConstructor = moment;

@Pipe({ name: 'amDateFormat' })
export class DateFormatPipe implements PipeTransform {
transform(value: Date | moment.Moment | string | number, ...args: any[]): string {
transform(value: moment.MomentInput, ...args: any[]): string {
if (!value) { return ''; }
return momentConstructor(value).format(args[0]);
}
Expand Down
4 changes: 2 additions & 2 deletions src/difference.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ const momentConstructor = moment;

@Pipe({ name: 'amDifference' })
export class DifferencePipe implements PipeTransform {
transform(value: Date | moment.Moment,
otherValue: Date | moment.Moment,
transform(value: moment.MomentInput,
otherValue: moment.MomentInput,
unit?: moment.unitOfTime.Diff,
precision?: boolean): number {

Expand Down
2 changes: 1 addition & 1 deletion src/duration.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class DurationPipe implements PipeTransform {
this._applyOptions(momentOptions);
}

transform(value: any, ...args: string[]): string {
transform(value: moment.DurationInputArg1, ...args: string[]): string {
if (typeof args === 'undefined' || args.length !== 1) {
throw new Error('DurationPipe: missing required time unit argument');
}
Expand Down
7 changes: 2 additions & 5 deletions src/from-unix.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import * as moment from 'moment';

@Pipe({ name: 'amFromUnix' })
export class FromUnixPipe implements PipeTransform {
transform(value: any, ...args: string[]): any {
if (typeof value === 'string') {
value = +value;
}
return moment.unix(value);
transform(value: number | string, ...args: string[]): any {
return (typeof value === 'string') ? moment.unix(parseInt(value, 10)) : moment.unix(value);
}
}
2 changes: 1 addition & 1 deletion src/from-utc.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as moment from 'moment';

@Pipe({ name: 'amFromUtc' })
export class FromUtcPipe implements PipeTransform {
transform(value: any, formats?: string|string[], ...args: string[]): any {
transform(value: moment.MomentInput, formats?: string|string[], ...args: string[]): any {
return formats ? moment.utc(value, formats) : moment.utc(value);
}
}
4 changes: 2 additions & 2 deletions src/is-after.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ const momentConstructor = moment;
})
export class IsAfterPipe implements PipeTransform {

transform(value: Date | moment.Moment,
otherValue: Date | moment.Moment,
transform(value: moment.MomentInput,
otherValue: moment.MomentInput,
unit?: moment.unitOfTime.StartOf): boolean {
return momentConstructor(value).isAfter(momentConstructor(otherValue), unit);
}
Expand Down
4 changes: 2 additions & 2 deletions src/is-before.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ const momentConstructor = moment;
})
export class IsBeforePipe implements PipeTransform {

transform(value: Date | moment.Moment,
otherValue: Date | moment.Moment,
transform(value: moment.MomentInput,
otherValue: moment.MomentInput,
unit?: moment.unitOfTime.StartOf): boolean {
return momentConstructor(value).isBefore(momentConstructor(otherValue), unit);
}
Expand Down
2 changes: 1 addition & 1 deletion src/local.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const momentConstructor = moment;

@Pipe({ name: 'amLocal' })
export class LocalTimePipe implements PipeTransform {
transform(value: Date | moment.Moment | string | number): moment.Moment {
transform(value: moment.MomentInput): moment.Moment {
return momentConstructor(value).local();
}
}
4 changes: 2 additions & 2 deletions src/locale.pipe.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Pipe, PipeTransform } from '@angular/core';
import * as moment from 'moment';

// under systemjs, moment is actually exported as the default export, so we account for that
// See https://github.com/ng-packagr/ng-packagr/issues/217 for why this is needed:
const momentConstructor = moment;

@Pipe({ name: 'amLocale' })
export class LocalePipe implements PipeTransform {
transform(value: string | Date, locale: string): moment.Moment {
transform(value: moment.MomentInput, locale: string): moment.Moment {
return momentConstructor(value).locale(locale);
}
}
2 changes: 1 addition & 1 deletion src/parse-zone.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as moment from 'moment';

@Pipe({ name: 'amParseZone' })
export class ParseZonePipe implements PipeTransform {
transform(value: string): moment.Moment {
transform(value: moment.MomentInput): moment.Moment {
return moment.parseZone(value);
}
}
2 changes: 1 addition & 1 deletion src/parse.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const momentConstructor = moment;

@Pipe({ name: 'amParse' })
export class ParsePipe implements PipeTransform {
transform(value: string, formats: string|string[]): moment.Moment {
transform(value: moment.MomentInput, formats: string|string[]): moment.Moment {
return momentConstructor(value, formats);
}
}
2 changes: 1 addition & 1 deletion src/subtract.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const momentConstructor = moment;

@Pipe({ name: 'amSubtract' })
export class SubtractPipe implements PipeTransform {
transform(value: any, amount: moment.DurationInputArg1, unit?: moment.DurationInputArg2): any {
transform(value: moment.MomentInput, amount: moment.DurationInputArg1, unit?: moment.DurationInputArg2): any {
if (typeof amount === 'undefined' || (typeof amount === 'number' && typeof unit === 'undefined')) {
throw new Error('SubtractPipe: missing required arguments');
}
Expand Down
2 changes: 1 addition & 1 deletion src/utc.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const momentConstructor = moment;

@Pipe({ name: 'amUtc' })
export class UtcPipe implements PipeTransform {
transform(value: Date | moment.Moment | string | number): moment.Moment {
transform(value: moment.MomentInput): moment.Moment {
return momentConstructor(value).utc();
}
}

0 comments on commit 906e40c

Please sign in to comment.