Jezyk Danzowki Jezyk Danzowki - 2 months ago 39
TypeScript Question

Angular2 Typescript Trim File Extensions from array

I was wondering if it is possible to trim file extensions from an array on Angular 2 TS.

I know it is easy to trim within JS, but wondered how easy it is to do within TS.

Lets same i have this data returned:

[ '1111111.pdf', '1111112.pdf' ]


How would I approach to trim the '.pdf' from the array?

I know in js you can use
String.prototype.trim.apply
but yano... any suggestions would be apprecaited

Answer

I'd use custom pipe.

Working Demo: https://plnkr.co/edit/I45tLyFfXyjKIq5A7Mqu?p=preview

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

@Pipe({ name: 'extension' })

export class RemoveExtension implements PipeTransform {
  transform(item) {
    return item.substring(0, item.indexOf('.'))
  }

}

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  template: `      
      <h1>Remove Extension:</h1>      
      <li *ngFor="let item of myData">{{item|extension}}</li>
  `
})
export class AppComponent {
 myData=[ 'abc.pdf', 'xyz.pdf' ];
}
Comments