Skip to content

Instantly share code, notes, and snippets.

@coskuncakir
Forked from djabif/slugify.pipe.ts
Last active May 29, 2023 12:28
Show Gist options
  • Save coskuncakir/3378d0147968e4297eb20a1d214296eb to your computer and use it in GitHub Desktop.
Save coskuncakir/3378d0147968e4297eb20a1d214296eb to your computer and use it in GitHub Desktop.
Angular Pipe to transform a string into a slug with turkish character support.
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({ name: 'slugify' })
export class SlugifyPipe implements PipeTransform {
transform(input: string): string {
const trChars = {
'çÇ': 'c',
'ğĞ': 'g',
'şŞ': 's',
'üÜ': 'u',
'ıİ': 'i',
'öÖ': 'o'
};
for (const key of Object.keys(trChars)) {
input = input.replace(new RegExp('[' + key + ']', 'g'), trChars[key]);
}
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
.replace(/-+$/, ''); // Trim - from end of text
}
}
import { SlugifyPipe } from '../../shared/slugify.pipe'; //import it from your path
//your other imports
@Component({
selector: 'app-your-component',
templateUrl: './your-component.component.html',
styleUrls: ['./your-component.component.css'],
providers: [SlugifyPipe]
})
export class YourComponent{
constructor(private slugifyPipe: SlugifyPipe){}
slugify(input: string){
const your_new_slug = this.slugifyPipe.transform(input);
}
}
@coskuncakir
Copy link
Author

Thanks, @ruslanguns. I'm glad you use it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment