import { canUseDOM as canUseDOMBool } from 'exenv' ; import { Locale } from './types' ; export const canUseDOM = ( ) => canUseDOMBool ; export const STATUS = { ERROR : 'ERROR' , IDLE : 'IDLE' , INITIALIZING : 'INITIALIZING' , READY : 'READY' , RUNNING : 'RUNNING' , UNSUPPORTED : 'UNSUPPORTED' , } ; export const TYPE = { DEVICE : 'device_update' , FAVORITE : 'favorite_update' , PLAYER : 'player_update' , PROGRESS : 'progress_update' , STATUS : 'status_update' , TRACK : 'track_update' , } ; export function getLocale ( locale ? ) { return { devices : 'Devices' , next : 'Next' , pause : 'Pause' , play : 'Play' , previous : 'Previous' , title : '{name} on SPOTIFY' , volume : 'Volume' , ... locale , } ; } export function getSpotifyLink ( uri ) { const [ , type = '' , id = '' ] = uri . split ( ':' ) ; return `https://open.spotify.com/${ type }/${ id }` ; } export function getSpotifyLinkTitle ( name , locale ) { return locale . replace ( '{name}' , name ) ; } export function getSpotifyURIType ( uri ) { const [ , type = '' ] = uri . split ( ':' ) ; return type ; } export function isEqualArray ( A ? , B ? ) { if ( ! Array . isArray ( A ) || ! Array . isArray ( B ) || A . length !== B . length ) { return false ; } let result = true ; A . forEach ( ( a ) => B . forEach ( ( b ) => { result = a === b ; } ) ) ; return result ; } export function isNumber ( value ) { return typeof value === 'number' ; } export function loadSpotifyPlayer ( ) { return new Promise < void > ( ( resolve , reject ) => { const scriptTag = document . getElementById ( 'spotify-player' ) ; if ( ! scriptTag ) { const script = document . createElement ( 'script' ) ; script . id = 'spotify-player' ; script . type = 'text/javascript' ; script . async = false ; script . defer = true ; script . src = 'https://sdk.scdn.co/spotify-player.js' ; script . onload = ( ) => resolve ( ) ; script . onerror = ( error ) => reject ( new Error ( `loadScript: ${ error . message }` ) ) ; document . head . appendChild ( script ) ; } else { resolve ( ) ; } } ) ; } export function parseVolume ( value ? ) { if ( ! isNumber ( value ) ) { return 1 ; } if ( value > 1 ) { return value / 100 ; } return value ; } /**
 * Round decimal numbers
 */ export function round ( number , digits = 2 ) { const factor = 10 ** digits ; return Math . round ( number * factor ) / factor ; } export function validateURI ( input ) { const validTypes = [ 'album' , 'artist' , 'playlist' , 'show' , 'track' ] ; if ( input && input . indexOf ( ':' ) > - 1 ) { const [ key , type , id ] = input . split ( ':' ) ; if ( key === 'spotify' && validTypes . indexOf ( type ) >= 0 && id . length === 22 ) { return true ; } } return false ; }