1
0
mirror of synced 2025-12-23 11:54:18 -05:00
Files
docs/components/lib/user-agent.ts
Kevin Heis 303d5190db Create a translations test suite (#30000)
* Scaffold files for migration

* Move user-agent into unit suite

* Nothing to move from browser suite

* Migrate tests to translations/content

* Migrate existing translation test to meta

* No graphql tests to migrate

* Migrate lint-translation-reporter

* Migrate lint-translation-reporter

* Remove languages-schema, unused

* Restore languages-schema

* Restore languages-schema

* Migrate rendering

* Migrate routing

* Migrate most of unit

* Remove dead files, comment out tests that aren't expected to work yet

* Migrate from get-redirect

* Migrate page and pages

* Migrate linting code

* Fix lint issues

* Found a few more

* Run prettier

* Move crowdin-config test and helper

* Update crowdin-config.js

* Remove translation linting, crowdin config lint, reduce file count

* Remove test that's been skipped for a year

* Restore linting with note to remove later

* Update lint-translation-reporter.js

* Clean up rendering suite

* Update rendering.js

* Remove excessive describe blocks

* Redirect tests

* Clean up unit

* Remove test that's never called

* Don't compare early access

* Rename test suites

* Update "content" tests

* Update files.js

* Update search.js

* Update files.js

* Update files.js
2022-08-25 12:38:03 -07:00

31 lines
941 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 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 }
}