Skip to content

Instantly share code, notes, and snippets.

@romelgomez
Created December 9, 2016 11:41
Show Gist options
  • Save romelgomez/9a008a9c67cef6033f85350f9f302981 to your computer and use it in GitHub Desktop.
Save romelgomez/9a008a9c67cef6033f85350f9f302981 to your computer and use it in GitHub Desktop.
TypeScript Filters
import { Pipe, PipeTransform } from '@angular/core';
/**
* @Description Slug or lispCase; All letters are downCased and spaces and specialChars are replaced by hyphens '-'.
*
* EXAMPLE: {{ 'Your best days are not behind you; your best days are out in front of you.' | slug }} // your-best-days-are-not-behind-you-your-best-days-are-out-in-front-of-you
*
* @param {String}
* @return {String}
* */
@Pipe({name: 'slug'})
export class SlugPipe implements PipeTransform {
transform(input: string): string {
return (!!input) ? String(input).toLowerCase().replace(/[^a-zá-źA-ZÁ-Ź0-9]/g, ' ').trim().replace(/\s{2,}/g, ' ').replace(/\s+/g, '-') : '';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment