Created
October 28, 2016 09:43
-
-
Save AndrewAllison/4d33ebdbcc854a837b674ca7c61b8c2c to your computer and use it in GitHub Desktop.
Common Pipes for angular 2 in typescript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Pipe, PipeTransform } from '@angular/core'; | |
@Pipe({ | |
name: 'jsonDate' | |
}) | |
export class JsonDatePipe implements PipeTransform { | |
transform(value: any, args?: any): any { | |
if (value) { | |
return new Date(parseInt(value.substr(6))); | |
} else { | |
return value; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Pipe, PipeTransform } from '@angular/core'; | |
@Pipe({ | |
name: 'search' | |
}) | |
export class SearchPipe implements PipeTransform { | |
transform(value: any, args?: any): any { | |
let filter: Array<any> = []; | |
if ((value instanceof Array && value.length > 0) && (args !== undefined && args !== '')) { | |
value.forEach(function (item) { | |
let searchResult = item['Name'].toLowerCase().search(args.toLowerCase()); | |
if (searchResult >= 0) { | |
filter.push(item); | |
} | |
}); | |
} else { | |
filter = value; | |
} | |
return filter; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment