import { ChangeEvent } from 'react' ; import { APP_NAME } from 'config' ; export function setDocumentTitle ( names ) { document . title = `${ APP_NAME } | ${ names . join ( ' | ' ) }` ; } export function getTextIdentifier ( name ) { const segments = name . split ( ' ' ) ; return segments . length > 1 ? segments . map ( ( n ) => n [ 0 ] ) . slice ( 0 , 2 ) . join ( '' ) : name . slice ( 0 , 2 ) ; } export function isExist ( findIndex ) { return findIndex > - 1 ; } /** Getting a random integer between two values.
 *  The maximum is inclusive and the minimum is inclusive.
 *  */ export function getRandomInt ( min , max ) { const minValue = Math . ceil ( min ) ; const maxValue = Math . floor ( max ) ; return Math . floor ( Math . random ( ) * ( maxValue - minValue + 1 ) + minValue ) ; } export function getTimestamp ( date = new Date ( ) ) { return date . valueOf ( ) ; } export function generateId ( ) { const seed = new Uint32Array ( 4 ) ; const cryptoObj = window ?. crypto || ( ( window as any ) . msCrypto as Crypto ) ; return cryptoObj . getRandomValues ( seed ) . join ( '-' ) ; } export function capitalizeFirstLetter ( str ) { return str . charAt ( 0 ) . toUpperCase ( ) + str . slice ( 1 ) ; } export function calcSum < T > ( items , propName ) { return items . reduce ( ( prev , current ) => { const prop = current [ propName ] || 0 ; const propValue = typeof prop === 'number' ? prop : 0 ; return prev + propValue ; } , 0 ) ; } export function mergeAndSum < T , K extends keyof T > ( items , groupKey , sumKey ) { const result = [ ] ; items . forEach ( ( item ) => { const updItem = { [ groupKey ] : item [ groupKey ] , sum : 0 } ; const index = result . findIndex ( ( entity ) => entity [ groupKey ] === item [ groupKey ] ) ; if ( isExist ( index ) ) return ; updItem . sum = calcSum < T > ( items . filter ( ( entity ) => entity [ groupKey ] === item [ groupKey ] ) , sumKey ) ; result . push ( updItem ) ; } ) ; return result ; } export function average ( items ) { return items . length > 0 ? items . reduce ( ( prev , curr ) => prev + curr , 0 ) / items . length : 0 ; } export function round ( num , decimalPlaces = 0 ) { const d = decimalPlaces ; const m = 10 ** d ; const n = + ( d ? num * m : num ) . toFixed ( 8 ) ; const i = Math . floor ( n ) ; const f = n - i ; const e = 1e-8 ; const r = f > 0.5 - e && f < 0.5 + e ? i + ( i % 2 ) : Math . round ( n ) ; return d ? r / m : r ; } export function encodeImage ( event , cb ) { if ( ! event . currentTarget . files ?. length ) return ; const file = event . currentTarget . files [ 0 ] ; const reader = new FileReader ( ) ; reader . onloadend = ( ) => { const { result } = reader ; if ( typeof result === 'string' ) { cb ( result ) ; } } ; reader . readAsDataURL ( file ) ; } export function exportToJsonFile ( jsonData ) { const dataStr = JSON . stringify ( jsonData ) ; const dataUri = `data:application/json;charset=utf-8,${ encodeURIComponent ( dataStr ) }` ; const exportFileDefaultName = 'items.json' ; const linkElement = document . createElement ( 'a' ) ; linkElement . setAttribute ( 'href' , dataUri ) ; linkElement . setAttribute ( 'download' , exportFileDefaultName ) ; linkElement . click ( ) ; linkElement . remove ( ) ; }