1
0
mirror of synced 2025-12-21 02:46:50 -05:00
Files
docs/components/lib/user-agent.ts
Grace Park 8ba413cabc React Cleanup: Documentation (#20517)
* initial documentation changes

* editing layouts terminology

* update to match other files

* move javascripts to components/lib

* fix: dockerfile

* update based on Mikes updates to the javascripts directory

* update components/README.md

Co-authored-by: Mike Surowiec <mikesurowiec@users.noreply.github.com>
2021-07-27 09:07:18 -07:00

31 lines
949 B
TypeScript

// A tiny user agent checking RegExp for analytics purposes
// The order matters with these
const OS_REGEXPS = [
/(iphone os|ipad os) ([^);]+)/i,
/(mac) os x ([^);]+)/i,
/(windows) ([^);]+)/i,
/(android) ([^);]+)/i,
/(cros) ([^);]+)/i,
/(linux) ([^);]+)/i,
]
// The order matters with these
const BROWSER_REGEXPS = [
/(firefox)\/([^\s)]+)/i,
/(edge)\/([^\s)]+)/i,
/(chrome)\/([^\s)]+)/i,
/(safari)\/([^\s)]+)/i,
/ms(ie)\/([^\s)]+)/i,
]
export default function parseUserAgent(ua = navigator.userAgent) {
ua = ua.toLowerCase()
const osRe = OS_REGEXPS.find((re) => re.test(ua))
let [, os = 'other', os_version = '0'] = (osRe && ua.match(osRe)) || []
if (os === 'iphone os' || os === 'ipad os') os = 'ios'
const browserRe = BROWSER_REGEXPS.find((re) => re.test(ua))
const [, browser = 'other', browser_version = '0'] = (browserRe && ua.match(browserRe)) || []
return { os, os_version, browser, browser_version }
}