import sha1 from 'sha1-es' ; import _truncate from 'lodash/truncate' ; import markdown from 'markdown-it' ; import latinize from 'latinize' ; import Translation from './translation' ; export default class Strings { protected locale : string ; protected translator : Translation ; constructor ( locale = 'en-US' ) { this . locale = locale ; this . translator = new Translation ( this . locale ) ; } /**
     * Generates a random string of 15 characters
     */ public random ( ) { let text = '' ; const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' ; for ( let i = 0 ; i < 15 ; i ++ ) { text += possible . charAt ( Math . floor ( Math . random ( ) * possible . length ) ) ; } return text ; } param string public ascii ( string ) { return latinize ( string ) ; } param string public markdown ( string ) { return markdown ( { typographer : true } ) . renderInline ( string ) ; } param string param breaks public markdownBlock ( string , breaks = true ) { return markdown ( { breaks , typographer : true } ) . render ( string ) ; } param string param length param options public truncate ( string , length = 140 , options ) { return _truncate ( string , { ... { length , omission : '…' } , ... options } ) ; } param string param replacements public set ( string , ... replacements ) { const replace = typeof replacements [ 0 ] === 'object' ? replacements [ 0 ] : replacements ; return string . replace ( /:(\d+|[a-z]+)/gi , function ( match , index ) { return typeof replace [ index ] !== 'undefined' ? replace [ index ] : match ; } ) ; } param strings param amount public plural ( strings , amount ) { return this . set ( this . translator . getPlural ( strings , amount ) , { n : amount } ) ; } param object public from ( object ) { if ( typeof object === 'string' ) { return object ; } return sha1 . hash ( JSON . stringify ( object ) ) ; } }