refactor: use scope variable memo for modern utils (#6954)

This commit is contained in:
Xin Du (Clark) 2020-02-09 11:44:54 +00:00 committed by GitHub
parent 6ae7718a42
commit 13cc924f02

View File

@ -1,3 +1,5 @@
import UAParser from 'ua-parser-js'
export const ModernBrowsers = {
Edge: '16',
Firefox: '60',
@ -12,29 +14,36 @@ export const ModernBrowsers = {
'Mobile Safari': '10.3'
}
const modernBrowsers = new Proxy(ModernBrowsers, {
get (browsers, prop) {
if (browsers[prop] && !browsers[prop].version) {
const coerce = require('semver/functions/coerce')
browsers[prop] = coerce(browsers[prop])
}
return browsers[prop]
let semver
let __modernBrowsers
const getModernBrowsers = () => {
if (__modernBrowsers) {
return __modernBrowsers
}
})
__modernBrowsers = Object.keys(ModernBrowsers)
.reduce((allBrowsers, browser) => {
allBrowsers[browser] = semver.coerce(ModernBrowsers[browser])
return allBrowsers
}, {})
return __modernBrowsers
}
export const isModernBrowser = (ua) => {
if (!ua) {
return false
}
const coerce = require('semver/functions/coerce')
const gte = require('semver/functions/gte')
const UAParser = require('ua-parser-js')
if (!semver) {
semver = require('semver')
}
const { browser } = UAParser(ua)
const browserVersion = coerce(browser.version)
const browserVersion = semver.coerce(browser.version)
if (!browserVersion) {
return false
}
return Boolean(modernBrowsers[browser.name] && gte(browserVersion, modernBrowsers[browser.name]))
const modernBrowsers = getModernBrowsers()
return Boolean(modernBrowsers[browser.name] && semver.gte(browserVersion, modernBrowsers[browser.name]))
}
export const isModernRequest = (req, modernMode = false) => {