license import { UAParser } from 'ua-parser-js' ; /**
 * A feature supported by a web browser.
 */ export type BrowserCapability = 'es2015' | 'es2016' | 'es2017' | 'es2018' | 'push' | 'serviceworker' | 'modules' ; export type UserAgentPredicate = ( ua : InstanceType < typeof UAParser > ) => boolean ; const chrome = { es2015 : since ( 49 ) , es2016 : since ( 58 ) , es2017 : since ( 58 ) , es2018 : since ( 64 ) , push : since ( 41 ) , serviceworker : since ( 45 ) , modules : since ( 64 ) , } ; const browserPredicates = { 'Chrome' : chrome , 'Chromium' : chrome , 'Chrome Headless' : chrome , 'Opera' : { es2015 : since ( 36 ) , es2016 : since ( 45 ) , es2017 : since ( 45 ) , es2018 : since ( 51 ) , push : since ( 28 ) , serviceworker : since ( 32 ) , modules : since ( 48 ) , } , 'Vivaldi' : { es2015 : since ( 1 ) , es2016 : since ( 1 , 14 ) , es2017 : since ( 1 , 14 ) , es2018 : since ( 1 , 14 ) , push : since ( 1 ) , serviceworker : since ( 1 ) , modules : since ( 1 , 14 ) , } , 'Mobile Safari' : { es2015 : sinceOS ( 10 ) , es2016 : sinceOS ( 10 , 3 ) , es2017 : sinceOS ( 10 , 3 ) , es2018 : ( ) => false , push : sinceOS ( 9 , 2 ) , serviceworker : sinceOS ( 11 , 3 ) , modules : sinceOS ( 11 , 3 ) , } , 'Safari' : { es2015 : since ( 10 ) , es2016 : since ( 10 , 1 ) , es2017 : since ( 10 , 1 ) , es2018 : ( ) => false , push : ( ua ) => { return versionAtLeast ( [ 9 ] , parseVersion ( ua . getBrowser ( ) . version ) ) && versionAtLeast ( [ 10 , 11 ] , parseVersion ( ua . getOS ( ) . version ) ) ; } , serviceworker : since ( 11 , 1 ) , modules : since ( 11 , 1 ) , } , 'Edge' : { es2015 : since ( 15 , 15063 ) , es2016 : since ( 15 , 15063 ) , es2017 : since ( 15 , 15063 ) , es2018 : ( ) => false , push : since ( 12 ) , serviceworker : ( ) => false , modules : ( ) => false , } , 'Firefox' : { es2015 : since ( 51 ) , es2016 : since ( 52 ) , es2017 : since ( 52 ) , es2018 : since ( 58 ) , push : since ( 63 ) , serviceworker : since ( 44 ) , modules : since ( 67 ) , } , } ; /**
 * Return the set of capabilities for a user agent string.
 */ export function browserCapabilities ( userAgent ) { const ua = new UAParser ( userAgent ) ; const capabilities = new Set < BrowserCapability > ( ) ; let browserName = ua . getBrowser ( ) . name || '' ; if ( browserName === 'Chrome' && ua . getOS ( ) . name === 'iOS' ) { browserName = 'Mobile Safari' ; } const predicates = browserPredicates [ browserName ] || { } ; for ( const capability of Object . keys ( predicates ) as BrowserCapability [ ] ) { if ( predicates [ capability ] ( ua ) ) { capabilities . add ( capability ) ; } } return capabilities ; } /**
 * Parse a "x.y.z" version string of any length into integer parts. Returns -1
 * for a part that doesn't parse.
 */ export function parseVersion ( version ) { if ( version == null ) { return [ ] ; } return version . split ( '.' ) . map ( ( part ) => { const i = parseInt ( part , 10 ) ; return isNaN ( i ) ? - 1 : i ; } ) ; } /**
 * Return whether `version` is at least as high as `atLeast`.
 */ export function versionAtLeast ( atLeast , version ) { for ( let i = 0 ; i < atLeast . length ; i ++ ) { const r = atLeast [ i ] ; const v = version . length > i ? version [ i ] : 0 ; if ( v > r ) { return true ; } if ( v < r ) { return false ; } } return true ; } /**
 * Make a predicate that checks if the browser version is at least this high.
 */ function since ( ... atLeast ) { return ( ua ) => versionAtLeast ( atLeast , parseVersion ( ua . getBrowser ( ) . version ) ) ; } /**
 * Make a predicate that checks if the OS version is at least this high.
 */ function sinceOS ( ... atLeast ) { return ( ua ) => versionAtLeast ( atLeast , parseVersion ( ua . getOS ( ) . version ) ) ; }