Skip to content

Instantly share code, notes, and snippets.

View safwendr's full-sized avatar

Safwen Derouiche safwendr

View GitHub Profile
@safwendr
safwendr / slugify.pipe.ts
Created January 4, 2022 11:13 — forked from djabif/slugify.pipe.ts
Angular Pipe to transform a string into a slug
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({name: 'slugify'})
export class SlugifyPipe implements PipeTransform {
transform(input: string): string {
return input.toString().toLowerCase()
.replace(/\s+/g, '-') // Replace spaces with -
.replace(/[^\w\-]+/g, '') // Remove all non-word chars
.replace(/\-\-+/g, '-') // Replace multiple - with single -
.replace(/^-+/, '') // Trim - from start of text