import getValue from 'get-value' ; import { Track , Source } from './types' ; export function isTrack ( arg ) { return arg && arg . label && typeof arg . label === 'string' ; } export function isSource ( arg ) { return arg && arg . url && typeof arg . url === 'string' ; } /**
 * updates a with values from b, recursively
 */ type Obj = Record < string , any > ; export function deepUpdate ( a , b ) { for ( const prop of Object . keys ( b ) ) { if ( prop in a && typeof b [ prop ] === 'object' && typeof a [ prop ] === 'object' ) { deepUpdate ( a [ prop ] , b [ prop ] ) ; } else if ( typeof a [ prop ] === 'undefined' || typeof b [ prop ] !== 'undefined' ) { a [ prop ] = b [ prop ] ; } } return a ; } param template param fillWith returns export function fillTemplate ( template , fillWith ) { return template . replace ( /\{([\w\s.]+)\}/g , ( match , varName ) : string => { varName = varName . replace ( /\s+/g , '' ) ; const fill = getValue ( fillWith , varName ) ; if ( fill !== undefined ) { if ( typeof fill === 'function' ) { return fill ( varName ) ; } return fill ; } if ( fillWith . callback ) { const v = fillWith . callback . call ( this , varName ) ; if ( v !== undefined ) { return v ; } } return match ; } ) ; } param src export function clone ( src ) { if ( ! src || typeof src !== 'object' || Object . prototype . toString . call ( src ) === '[object Function]' ) { return src ; } if ( src . nodeType && 'cloneNode' in src ) { return src . cloneNode ( true ) ; } if ( src instanceof Date ) { return new Date ( src . getTime ( ) ) ; } if ( src instanceof RegExp ) { return new RegExp ( src ) ; } let r ; let i ; let l ; if ( Array . isArray ( src ) ) { r = [ ] ; for ( i = 0 , l = src . length ; i < l ; ++ i ) { if ( i in src ) { r [ i ] = clone ( src [ i ] ) ; } } } else { r = src . constructor ? new src . constructor ( ) : { } ; } return mixin ( r , src , clone ) ; } param dest param source param copyFunc returns function mixin ( dest , source , copyFunc ) { let name ; let s ; const empty = { } ; for ( name in source ) { s = source [ name ] ; if ( ! ( name in dest ) || ( dest [ name ] !== s && ( ! ( name in empty ) || empty [ name ] !== s ) ) ) { dest [ name ] = copyFunc ? copyFunc ( s ) : s ; } } return dest ; }