import { ThumbnailsData , ShareConfig } from '../../@types/alltypes' ; import { format } from 'date-fns' ; export const preventDefault = ( func = ( e ) => { } ) => ( e ) => { e . preventDefault ( ) ; func ( e ) ; } ; export const stopPropagation = ( func = ( e ) => { } ) => ( e ) => { e . stopPropagation ( ) ; func && func ( e ) ; } ; export const getThumbnails = ( thumbnails , size ) : string => { const { [ size ] : { url = '' } } = thumbnails ; return url . replace ( 'http:' , 'https:' ) ; } ; export const formatDate = ( date , formatString ) => format ( new Date ( date ) , formatString ) ; export const parseDuration = ( PT ) => { if ( ! PT ) { return 0 ; } const matches = PT . match ( /P(?:(\d*)Y)?(?:(\d*)M)?(?:(\d*)W)?(?:(\d*)D)?T?(?:(\d*)H)?(?:(\d*)M)?(?:(\d*)S)?/i ) ?? [ ] ; const parts = [ { pos : 1 , multiplier : 86400 * 365 } , { pos : 2 , multiplier : 86400 * 30 } , { pos : 3 , multiplier : 604800 } , { pos : 4 , multiplier : 86400 } , { pos : 5 , multiplier : 3600 } , { pos : 6 , multiplier : 60 } , { pos : 7 , multiplier : 1 } ] ; let durationInSec = 0 ; for ( const { pos , multiplier } of parts ) { const { [ pos ] : time } = matches ; if ( time ) { durationInSec += parseInt ( time ) * multiplier ; } } return durationInSec ; } ; export const formatTime = ( t ) => { const hours = Math . floor ( t / 3600 ) ; const minutes = Math . floor ( ( t - hours * 3600 ) / 60 ) ; const seconds = Math . floor ( t - ( hours * 3600 + minutes * 60 ) ) ; const units = [ minutes , seconds ] ; if ( hours ) units . unshift ( hours ) ; return units . map ( ( unit ) => `${ unit }` . padStart ( 2 , '0' ) ) . join ( ':' ) ; } ; export const isMobile = ( ) => { const { userAgent = '' } = navigator ; return ! ! userAgent . match ( /(Android|webOS|iPhone|iPad|iPod|BlackBerry|Windows Phone)/i ) ; } ; export const parseVideoId = ( url ) => { const parts = url . split ( /(vi\/|v%3D|v=|\/v\/|youtu\.be\/|\/embed\/)/ ) ; return parts [ 2 ] !== undefined ? parts [ 2 ] . split ( /[^0-9a-z_\-]/i ) [ 0 ] : parts [ 0 ] ; } ; export const delay = ( t ) => new Promise ( ( resolve ) => setTimeout ( resolve , t ) ) ; export const splitLines = ( str ) => str . match ( /[^\r\n]+/g ) || [ ] ; export const chunk = ( array = [ ] , size ) => { const chunks = [ ] ; let index = 0 ; while ( index < array . length ) { chunks . push ( array . slice ( index , size + index ) ) ; index += size ; } return chunks ; } ; export const throttle = ( callback , delay = 50 ) => { let lastCall = 0 ; return ( ... args ) => { const now = Date . now ( ) ; if ( now - lastCall >= delay ) { lastCall = now ; callback ( ... args ) ; } } ; } ; export const debounce = ( callback , delay ) => { let timer ; return ( ... args ) => { clearTimeout ( timer ) ; timer = setTimeout ( ( ) => callback ( ... args ) , delay ) ; } ; } ; export function pick < T extends object , K extends keyof T > ( base , ... keys ) { const entries = keys . map ( ( key ) => [ key , base [ key ] ] ) ; return Object . fromEntries ( entries ) ; } export function omit < T extends object , K extends keyof T > ( base , ... keys ) { const result = { ... base } ; for ( const key of keys ) delete result [ key ] ; return result ; } export const wrapURLs = ( text ) => { const urlPattern = /\b(?:https?|ftp):\/\/[a-z0-9-+&@#\/%?=~_|!:,.;]*[a-z0-9-+&@#\/%=~_|]/gim ; const pseudoUrlPattern = /(^|[^\/])(www\.[\S]+(\b|$))/gim ; const emailAddressPattern = /[\w.]+@[a-zA-Z_-]+?(?:\.[a-zA-Z]{2,6})+/gim ; return text . replace ( urlPattern , '<a href="$&">$&</a>' ) . replace ( pseudoUrlPattern , '$1<a href="http://$2">$2</a>' ) . replace ( emailAddressPattern , '<a href="mailto:$&">$&</a>' ) ; } ; export const loadScript = ( src ) => { return new Promise ( ( resolve , reject ) => { try { if ( document . querySelector ( `script[src="${ src }"]` ) ) { resolve ( ) ; } else { const js = document . createElement ( 'script' ) ; document . body . appendChild ( js ) ; js . src = src ; js . onload = resolve ; } } catch ( err ) { reject ( err ) ; } } ) ; } ; export const setImmediateInterval = ( handler , timeout ? ) : number => { handler ( ) ; return setInterval ( handler , timeout ) ; } ; export const getVideoURL = ( id ) => `https://youtu.be/${ id }` ; export const getPlaylistURL = ( id ) => `https://youtube.com/playlist?list=${ id }` ; export const shareURL = ( config ) => navigator . share ( config ) ; export const copyText = ( text ) => navigator . clipboard . writeText ( text ) ; const isObject = ( item ) => item !== null && typeof item === 'object' && ! Array . isArray ( item ) ; export const mergeDeep = ( target , ... sources ) : Object => { if ( ! sources . length ) return target ; const source = sources . shift ( ) ; if ( isObject ( target ) && isObject ( source ) ) { for ( const key in source ) { if ( isObject ( source [ key ] ) ) { if ( ! target [ key ] ) Object . assign ( target , { [ key ] : { } } ) ; mergeDeep ( target [ key ] , source [ key ] ) ; } else { Object . assign ( target , { [ key ] : source [ key ] } ) ; } } } return mergeDeep ( target , ... sources ) ; } ;