import propOr from '@tinkoff/utils/object/propOr' ; import compose from '@tinkoff/utils/function/compose' ; import toLower from '@tinkoff/utils/string/toLower' ; import { UAParser } from 'ua-parser-js' ; import type { UserAgent } from './types' ; const toLowerName = compose ( toLower , propOr ( 'name' , '' ) ) ; const uaParserExtensions = [ [ /((?:\S+)bot(?:-[imagevdo]{5})?)\/([\w.]+)/i ] , [ UAParser . BROWSER . NAME , UAParser . BROWSER . VERSION , [ 'type' , 'bot' ] ] , [ /[\s;(](adsbot[-\w]*?[\s;)])/i ] , [ UAParser . BROWSER . NAME , [ UAParser . BROWSER . VERSION , 'unknown' ] , [ 'type' , 'bot' ] ] , [ /fxios\/([\w\\.-]+)/i ] , [ [ UAParser . BROWSER . NAME , 'Firefox Focus' ] , UAParser . BROWSER . VERSION ] , ] ; export const parse = ( userAgent ) : UserAgent => { const uaParser = new UAParser ( '' , { browser : uaParserExtensions } ) ; const { ua , ... result } = uaParser . setUA ( userAgent ) . getResult ( ) ; const { browser , os , engine } = result ; const browserName = toLowerName ( browser ) ; const engineName = toLowerName ( engine ) ; let mobileOS ; if ( browserName === 'opera mobi' ) { result . device . type = 'mobile' ; } switch ( os . name ) { case 'Windows Phone' : mobileOS = 'winphone' ; break ; case 'Android' : mobileOS = 'android' ; break ; case 'iOS' : mobileOS = 'ios' ; break ; case 'BlackBerry' : case 'RIM Tablet OS' : mobileOS = 'blackberry' ; break ; } let browserEngine ; switch ( true ) { case browserName === 'firefox' : browserEngine = 'firefox' ; break ; case browserName === 'safari' : browserEngine = 'safari' ; break ; case engineName === 'webkit' || engineName === 'blink' : browserEngine = 'chrome' ; break ; default : browserEngine = 'other' ; } return { ... result , mobileOS , browser : { ... browser , browserEngine , name : browserName , } , } ; } ;