ngx-moment icon indicating copy to clipboard operation
ngx-moment copied to clipboard

amFromUtc and amParse to accept a string or an array of strings with formats

Open gigadie opened this issue 7 years ago • 4 comments

Did you search for duplicate issue? [Yes]

Both implementation of pipes FromUtcPipe and ParsePipe, are not accepting format / formats as parameters, while moment instead provides the option.

from-utc.pipe.ts

import {Pipe, PipeTransform} from '@angular/core';
import * as moment from 'moment';

@Pipe({ name: 'amFromUtc' })
export class FromUtcPipe implements PipeTransform {
  transform(value: any, ...args: string[]): any {
    return moment.utc(value);
  }
}

could return instead

import {Pipe, PipeTransform} from '@angular/core';
import * as moment from 'moment';

@Pipe({ name: 'amFromUtc' })
export class FromUtcPipe implements PipeTransform {
  transform(value: any, formats: string|string[], ...args: string[]): any {
    return moment.utc(value, formats);
  }
}

the same is true for the ParsePipe which only accepts a string format and not an array:

parse.pipe.ts

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
const momentConstructor: (value?: any) => moment.Moment = (<any>moment).default || moment;

@Pipe({ name: 'amParse' })
export class ParsePipe implements PipeTransform {
  transform(value: string, format: string): moment.Moment {
    return moment(value, format);
  }
}

while it could be:

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
const momentConstructor: (value?: any) => moment.Moment = (<any>moment).default || moment;

@Pipe({ name: 'amParse' })
export class ParsePipe implements PipeTransform {
  transform(value: string, formats: string|string[]): moment.Moment {
    return moment(value, formats);
  }
}

gigadie avatar Jan 02 '18 16:01 gigadie

Thanks! Can you please send a pull request (with relevant unit-tests for the new functionality) ?

If you need help in getting the unit tests to work just ping here and I'll try to help

urish avatar Jan 03 '18 00:01 urish

Hi, I'll try my best to do it asap.

cheers

gigadie avatar Jan 05 '18 16:01 gigadie

Any progress? This feature would be nice.

rroex avatar May 16 '18 08:05 rroex

I submitted a PR https://github.com/urish/ngx-moment/pull/215

gigadie avatar Jul 31 '19 16:07 gigadie