import { stopWords , charMap } from '../constants' ; export class SlugHelper { param articleTitle public static createSlug ( articleTitle ) { if ( ! articleTitle ) { return null ; } let cleanTitle = this . removePunctuation ( articleTitle ) ; if ( cleanTitle ) { cleanTitle = cleanTitle . toLowerCase ( ) ; let words = cleanTitle . split ( /\s/ ) ; words = this . removeStopWords ( words ) ; cleanTitle = words . join ( "-" ) ; cleanTitle = this . replaceCharacters ( cleanTitle ) ; return cleanTitle ; } return null ; } param value private static removePunctuation ( value ) { if ( typeof value !== "string" ) { return "" ; } const punctuationless = value ?. replace ( /[\.,-\/#!$@%\^&\*;:{}=\-_`'"~()+\?<>]/g , " " ) ; return punctuationless ?. replace ( /\s{2,}/g , " " ) ; } param words private static removeStopWords ( words ) { const validWords = [ ] ; for ( const word of words ) { if ( stopWords . indexOf ( word . toLowerCase ( ) ) === - 1 ) { validWords . push ( word ) ; } } return validWords ; } param value private static replaceCharacters ( value ) { const characters = [ ... value ] ; return characters . map ( c => charMap [ c ] || c ) . join ( "" ) ; } }