namespace utils { export function getNowTimeStamp ( ) { return Date . now ( ) ; } export function getOSVersion ( ) { const appVersion = navigator . userAgent ; let detectedOS = "Unknown" ; if ( appVersion . indexOf ( "Win" ) != - 1 ) { detectedOS = "Windows" ; } else if ( appVersion . indexOf ( "Mac" ) != - 1 ) { detectedOS = "MacOS" ; } else if ( appVersion . indexOf ( "Linux" ) != - 1 ) { detectedOS = "Linux" ; } return detectedOS ; } export function getTimeStampByDate ( t ) { if ( typeof t === "string" ) { t = t . replaceAll ( "-" , "/" ) ; } const d = new Date ( t ) ; return d . getTime ( ) ; } export function getDateStampByDate ( t ) { const d = new Date ( getTimeStampByDate ( t ) ) ; return new Date ( d . getFullYear ( ) , d . getMonth ( ) , d . getDate ( ) ) . getTime ( ) ; } export function getDateString ( t ) { const d = new Date ( getTimeStampByDate ( t ) ) ; const year = d . getFullYear ( ) ; const month = d . getMonth ( ) + 1 ; const date = d . getDate ( ) ; return `${ year }/${ month }/${ date }` ; } export function getTimeString ( t ) { const d = new Date ( getTimeStampByDate ( t ) ) ; const hours = d . getHours ( ) ; const mins = d . getMinutes ( ) ; const hoursStr = hours < 10 ? "0" + hours : hours ; const minsStr = mins < 10 ? "0" + mins : mins ; return `${ hoursStr }:${ minsStr }` ; } export function getDateTimeString ( t ) { const d = new Date ( getTimeStampByDate ( t ) ) ; const year = d . getFullYear ( ) ; const month = d . getMonth ( ) + 1 ; const date = d . getDate ( ) ; const hours = d . getHours ( ) ; const mins = d . getMinutes ( ) ; const secs = d . getSeconds ( ) ; const monthStr = month < 10 ? "0" + month : month ; const dateStr = date < 10 ? "0" + date : date ; const hoursStr = hours < 10 ? "0" + hours : hours ; const minsStr = mins < 10 ? "0" + mins : mins ; const secsStr = secs < 10 ? "0" + secs : secs ; return `${ year }/${ monthStr }/${ dateStr } ${ hoursStr }:${ minsStr }:${ secsStr }` ; } export function dedupe < T > ( data ) { return Array . from ( new Set ( data ) ) ; } export function dedupeObjectWithId < T extends { id : string ; } > ( data ) { const idSet = new Set < string > ( ) ; const result = [ ] ; for ( const d of data ) { if ( ! idSet . has ( d . id ) ) { idSet . add ( d . id ) ; result . push ( d ) ; } } return result ; } export function debounce ( fn , delay ) { let timer = null ; return ( ) => { if ( timer ) { clearTimeout ( timer ) ; timer = setTimeout ( fn , delay ) ; } else { timer = setTimeout ( fn , delay ) ; } } ; } export function throttle ( fn , delay ) { let valid = true ; return ( ) => { if ( ! valid ) { return false ; } valid = false ; setTimeout ( ( ) => { fn ( ) ; valid = true ; } , delay ) ; } ; } export function transformObjectToParamsString ( object ) { const params = [ ] ; const keys = Object . keys ( object ) . sort ( ) ; for ( const key of keys ) { const val = object [ key ] ; if ( val ) { if ( typeof val === "object" ) { params . push ( ... transformObjectToParamsString ( val ) . split ( "&" ) ) ; } else { params . push ( `${ key }=${ val }` ) ; } } } return params . join ( "&" ) ; } export function transformParamsStringToObject ( paramsString ) { const object = { } ; const params = paramsString . split ( "&" ) ; for ( const p of params ) { const [ key , val ] = p . split ( "=" ) ; if ( key && val ) { object [ key ] = val ; } } return object ; } export function filterObjectNullKeys ( object ) { if ( ! object ) { return { } ; } const finalObject = { } ; const keys = Object . keys ( object ) . sort ( ) ; for ( const key of keys ) { const val = object [ key ] ; if ( typeof val === "object" ) { const temp = filterObjectNullKeys ( JSON . parse ( JSON . stringify ( val ) ) ) ; if ( temp && Object . keys ( temp ) . length > 0 ) { finalObject [ key ] = temp ; } } else { if ( Boolean ( val ) ) { finalObject [ key ] = val ; } } } return finalObject ; } export async function copyTextToClipboard ( text ) { if ( navigator . clipboard && navigator . clipboard . writeText ) { try { await navigator . clipboard . writeText ( text ) ; } catch ( error ) { console . warn ( "Copy to clipboard failed." , error ) ; } } else { console . warn ( "Copy to clipboard failed, methods not supports." ) ; } } export function getImageSize ( src ) { return new Promise ( ( resolve ) => { const imgEl = new Image ( ) ; imgEl . onload = ( ) => { const { width , height } = imgEl ; if ( width > 0 && height > 0 ) { resolve ( { width , height } ) ; } else { resolve ( { width : 0 , height : 0 } ) ; } } ; imgEl . onerror = ( ) => { resolve ( { width : 0 , height : 0 } ) ; } ; imgEl . className = "hidden" ; imgEl . src = src ; document . body . appendChild ( imgEl ) ; imgEl . remove ( ) ; } ) ; } } export default utils ;