export function isUpperCase ( str ) { return /^[A-Z\d]+$/ . test ( str ) ; } export function isLowerCase ( str ) { return /^[a-z\d]+$/ . test ( str ) ; } export function isTitleCase ( str ) { return isUpperCase ( str [ 0 ] ) && isLowerCase ( str . slice ( 1 ) ) ; } export function toUpperCase ( str ) { return str . toUpperCase ( ) ; } export function toLowerCase ( str ) { return str . toLowerCase ( ) ; } export function toTitleCase ( str ) { return `${ str [ 0 ] . toUpperCase ( ) }${ str . slice ( 1 ) . toLowerCase ( ) }` ; } export function toLowerCaseIfNotUpperCase ( str ) { return isUpperCase ( str ) ? str : str . toLowerCase ( ) ; } export function toTitleCaseIfNotUpperCase ( str ) { return isUpperCase ( str ) ? str : toTitleCase ( str ) ; } export function duplicate ( str , n ) { const strArr = [ ] ; for ( let i = 0 ; i < n ; i += 1 ) { strArr . push ( str ) ; } return strArr . join ( '' ) ; } export function trimStart ( str , toTrim ) { const step = toTrim . length ; let prefixEndIndex = 0 ; for ( let i = 0 ; i < str . length ; i += step ) { const s = str . slice ( i , i + step ) ; if ( s !== toTrim ) { prefixEndIndex = i ; break ; } } return str . slice ( prefixEndIndex ) ; } export function trimEnd ( str , toTrim ) { const step = toTrim . length ; let suffixStartIndex = 0 ; for ( let i = str . length ; i > 0 ; i -= step ) { const s = str . slice ( i - step , i ) ; if ( s !== toTrim ) { suffixStartIndex = i ; break ; } } return str . slice ( 0 , suffixStartIndex ) ; } export function trim ( str , toTrim ) { return trimEnd ( trimStart ( str , toTrim ) , toTrim ) ; } export const escapeRegExpSpecialChars = ( function getEscaper ( ) : ( str : string ) => string { const pattern = /[\^\$\.\*\?\+\|\/\\\[\]\(\)\{\}]/g ; return ( str ) : string => str . replace ( pattern , m => `\\${ m }` ) ; } ) ( ) ; export const words = ( function getWords ( ) : ( str : string ) => string [ ] { const wordPattern = /[a-z\d]+|[A-Z]\d*[a-z][a-z\d]*|[A-Z][A-Z\d]*(?=[A-Z]\d*[a-z][a-z\d]*|[^a-zA-Z\d]|$)/g ; return ( str ) : string [ ] => str . match ( wordPattern ) || [ ] ; } ) ( ) ; export function compare ( str1 , str2 ) { if ( str1 < str2 ) { return - 1 ; } if ( str1 > str2 ) { return 1 ; } return 0 ; }