import { cloneDeep } from 'lodash' ; import VueRouter , { Location , Route } from 'vue-router' ; namespace Util { param time return export const sleep = ( time ) : Promise < void > => { return new Promise < void > ( ( resolve ) => { setTimeout ( ( ) => { resolve ( ) ; } , time ) ; } ) ; } ; param url return { [ key : string ] export const getQuery = ( url ) : { [ key ] : string ; } => { const urls = url . split ( '?' ) ; if ( urls . length < 2 ) { return { } ; } const strs = urls [ 1 ] . split ( '&' ) ; const result = { } ; for ( const s of strs ) { const query = s . split ( '=' ) ; if ( query . length === 2 ) { result [ query [ 0 ] ] = query [ 1 ] ; } } return result ; } ; param url param key return export const getQueryParam = ( url , key ) : string | null => { const value = Util . getQuery ( url ) [ key ] ; return typeof value === 'undefined' ? null : decodeURIComponent ( value ) ; } ; param router param location return export const move = async ( router , location ) : Promise < Route | null > => { location = cloneDeep ( location ) ; if ( typeof location . query === 'undefined' ) { location . query = { } ; } if ( typeof location . path === 'undefined' ) { location . path = '/' ; } else if ( location . path . slice ( 0 , 1 ) !== '/' ) { location . path = '/' + location . path ; } if ( router . currentRoute . path === location . path ) { const oldQuery = cloneDeep ( router . currentRoute . query ) ; delete oldQuery . timestamp ; delete location . query . timestamp ; if ( Object . keys ( oldQuery ) . length === Object . keys ( location . query ) . length ) { let isEqual = true ; for ( const key in oldQuery ) { if ( location . query [ key ] !== oldQuery [ key ] ) { isEqual = false ; break ; } } if ( isEqual === true ) { return null ; } } } ( location . query as any ) [ 'timestamp' ] = new Date ( ) . getTime ( ) ; return router . push ( location ) ; } ; return export const getSubDirectory = ( ) : string => { return window . location . pathname . replace ( /\/[^\/]*$/ , '' ) ; } ; param route return export const getPageNum = ( route ) : number => { const page = typeof route . query . page === 'string' ? parseInt ( route . query . page , 10 ) : 1 ; if ( isNaN ( page ) === true ) { throw new Error ( 'PageIsNaN' ) ; } return page ; } ; const fileSizeUnits = [ 'B' , 'KB' , 'MB' , 'GB' , 'TB' , 'PB' ] ; param size return export const getFileSizeStr = ( size ) : string => { let cnt = 0 ; for ( ; cnt <= fileSizeUnits . length ; cnt ++ ) { if ( size < 1000 ) { break ; } size /= 1024 ; } return `${ size . toFixed ( 1 ) }${ fileSizeUnits [ cnt ] }` ; } ; export const getOSDarkTheme = ( ) : boolean => { return window . matchMedia ( '(prefers-color-scheme: dark)' ) . matches ; } ; } export default Util ;