import { PipeTransform, Pipe } from '@angular/core'; @Pipe({ name: 'dashCase' }) export class DashCasePipe implements PipeTransform { transform(ProductName: string, brandName?: string): string { let convertedBrandName; let convertedProductName; if (!!brandName) { convertedBrandName = this.allReplaces(brandName); convertedProductName = '-' + this.allReplaces(ProductName); } else { convertedBrandName = ''; convertedProductName = '-' + this.allReplaces(ProductName); } return `${convertedBrandName}${convertedProductName}`; } allReplaces(name: string) { return name.toLowerCase() .replace(/<(?:.|\n)*?>/gm, '') .replace(/[àâä]/g, 'a') .replace(/[èéêë]/g, 'e') .replace(/[ùûü]/g, 'u') .replace(/[ô]/g, 'o') .replace(/[œ]/g, 'oe') .replace(/[îï]/g, 'i') .replace(/[ç]/g, 'c') .replace(/[ÿ]/g, 'y') .replace(/[æ]/g, 'ae') .replace(/(?=\D)\.(?=\D)/g, '-') // replace . that is not surrounded with numbers .replace(/(?=\D)\,(?=\D)/g, '-') // replace , that is not surrounded with numbers .replace(/[^\w.,]+/g, '-'); } }