import { ReactionType } from 'components/Comment/Reactions' ; export const formatDate = ( date ) : string => new Date ( date ) . toDateString ( ) . slice ( 4 , 10 ) . toLowerCase ( ) ; export const timeAgo = ( time ) : string | number => { const MS_PER_SECOND = 1000 ; const MS_PER_MINUTE = 60 * 1000 ; const MS_PER_HOUR = MS_PER_MINUTE * 60 ; const MS_PER_DAY = MS_PER_HOUR * 24 ; const MS_PER_YEAR = MS_PER_DAY * 365 ; let current = new Date ( ) ; let previous = new Date ( time ) ; let elapsed = current - previous ; if ( elapsed < MS_PER_MINUTE ) { return Math . round ( elapsed / MS_PER_SECOND ) + 's ago' ; } else if ( elapsed < MS_PER_HOUR ) { return Math . round ( elapsed / MS_PER_MINUTE ) + 'm ago' ; } else if ( elapsed < MS_PER_DAY ) { return Math . round ( elapsed / MS_PER_HOUR ) + 'h ago' ; } else if ( elapsed < MS_PER_YEAR ) { return Math . round ( elapsed / MS_PER_DAY ) + 'd ago' ; } else { return Math . round ( elapsed / MS_PER_YEAR ) + 'y ago' ; } } ; export const getTimeDiff = ( dt1 ) : string | number => { let date1 = new Date ( dt1 ) . getTime ( ) ; let now = Date . now ( ) ; let millsec = date1 - now ; if ( date1 < now ) { millsec = now - date1 ; } const days = Math . floor ( millsec / 1000 / 60 / ( 60 * 24 ) ) ; const hours = Math . floor ( millsec / 1000 / 60 / 60 ) ; const min = Math . floor ( millsec / 1000 / 60 ) ; if ( hours >= 24 ) return days + ' days ago' ; if ( hours < 1 ) return min + ' min ago' ; return hours + ' hours ago' ; } ; export const MENTION_REGEX = /(?:^|[^a-zA-Z0-9_!@#$%&*])(?:(?:@)(?!\/))([a-zA-Z0-9/_.]{1,40})(?:\b(?!@)|$)/gm ; export const REFERENCE_REGEX = /\B#(\d{1,10})(?:\b)/gm ; description export const getRefsOrMentions = ( markdown , quantifier = '#' ) : string [ ] => { let regex = REFERENCE_REGEX ; if ( quantifier === '@' ) regex = MENTION_REGEX ; let matched = markdown . match ( regex ) ?. map ( ( ref ) : any => ref . replace ( quantifier , '' ) . trim ( ) ) ?. filter ( ( value , index , arr ) => arr . indexOf ( value ) === index ) ; return matched || [ ] ; } ; description export const parseRefsAndMentions = ( markdown ) : string => { return markdown ?. replace ( MENTION_REGEX , ` [@$1](/profiles/$1) ` ) . replace ( REFERENCE_REGEX , ` [#$1](/dashboard/bugs/$1) ` ) ; } ; description export const htmlDecode = ( input ) : string => { if ( typeof input !== 'string' ) return '' ; const textarea = document . createElement ( 'textarea' ) ; textarea . innerHTML = input ; return textarea . value ; } ; param markdown export const renderMarkdown = ( markdown ) : string => htmlDecode ( parseRefsAndMentions ( markdown ) ) ; export const copyToClipboard = ( str ) => { let el = document . createElement ( 'textarea' ) ; el . value = str ; el . setAttribute ( 'readonly' , '' ) ; el . style . position = 'absolute' ; el . style . left = '-9999px' ; document . body . appendChild ( el ) ; el . select ( ) ; document . execCommand ( 'copy' ) ; document . body . removeChild ( el ) ; } ; export const toggleArrayItem = ( arr , value ) => { let item = JSON . stringify ( value ) ; return arr . some ( a => item === JSON . stringify ( a ) ) ? arr . filter ( i => JSON . stringify ( i ) !== item ) : [ ... arr , value ] ; } ; export const calculateReputation = ( reactions ) => { const REPUTATION_MAP = { ':+1:' : 30 , ':-1:' : - 20 , ':smile:' : 20 , ':heart:' : 30 , ':confused:' : - 10 , ':tada:' : 20 , } ; let avg = 0 ; reactions ?. forEach ( react => { avg += REPUTATION_MAP [ react . emoji ] * react . users . length ; } ) ; return avg / 5 || 0 ; } ;