export function array < T > ( values ) { return Array . prototype . slice . call ( values ) ; } export const closest = ( ( ) => { const html = document . documentElement ; type MatchesSelector = ( this : Element , selector : string ) => boolean ; const match = html . matches || ( html as any ) . webkitMatchesSelector || ( html as any ) . msMatchesSelector || ( html as any ) . mozMatchesSelector ; type Closest = ( this : Element , selector : string ) => Element | null ; const closest = html . closest || function ( selector ) { let element = this ; while ( element ) { if ( match . call ( element , selector ) ) { return element ; } else { element = element . parentElement ; } } } ; return function ( element , selector ) { return closest . call ( element , selector ) ; } ; } ) ( ) ; export function defer ( callback ) { setTimeout ( callback , 1 ) ; } export type DispatchOptions = { target : EventTarget ; cancelable : boolean ; data : any ; } ; export function dispatch ( eventName , { target , cancelable , data } = { } ) { const event = document . createEvent ( "Events" ) as Event & { data : any ; } ; event . initEvent ( eventName , true , cancelable == true ) ; event . data = data || { } ; if ( event . cancelable && ! preventDefaultSupported ) { const { preventDefault } = event ; event . preventDefault = function ( ) { if ( ! this . defaultPrevented ) { Object . defineProperty ( this , "defaultPrevented" , { get : ( ) => true } ) ; } preventDefault . call ( this ) ; } ; } ( target || document ) . dispatchEvent ( event ) ; return event ; } const preventDefaultSupported = ( ( ) => { const event = document . createEvent ( "Events" ) ; event . initEvent ( "test" , true , true ) ; event . preventDefault ( ) ; return event . defaultPrevented ; } ) ( ) ; export function unindent ( strings , ... values ) { const lines = trimLeft ( interpolate ( strings , values ) ) . split ( "\n" ) ; const match = lines [ 0 ] . match ( /^\s+/ ) ; const indent = match ? match [ 0 ] . length : 0 ; return lines . map ( line => line . slice ( indent ) ) . join ( "\n" ) ; } function trimLeft ( string ) { return string . replace ( /^\n/ , "" ) ; } function interpolate ( strings , values ) { return strings . reduce ( ( result , string , i ) => { const value = values [ i ] == undefined ? "" : values [ i ] ; return result + string + value ; } , "" ) ; } export function uuid ( ) { return Array . apply ( null , { length : 36 } as any ) . map ( ( _ , i ) => { if ( i == 8 || i == 13 || i == 18 || i == 23 ) { return "-" ; } else if ( i == 14 ) { return "4" ; } else if ( i == 19 ) { return ( Math . floor ( Math . random ( ) * 4 ) + 8 ) . toString ( 16 ) ; } else { return Math . floor ( Math . random ( ) * 15 ) . toString ( 16 ) ; } } ) . join ( "" ) ; }