Skip to content

Commit

Permalink
Fixes #41 Minor version ranges not respected
Browse files Browse the repository at this point in the history
  • Loading branch information
pastelsky committed Jul 31, 2019
1 parent c6ff3c6 commit a868100
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 43 deletions.
48 changes: 38 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,28 @@ const semverify = (version) => {
return split.join('.')
}

function flatten(arr) {
return arr.reduce(function (flat, toFlatten) {
return flat.concat(Array.isArray(toFlatten) ? flatten(toFlatten) : toFlatten);
}, []);
}

// 10.0-10.2 -> 10.0, 10.1, 10.2
function generateSemversInRange(versionRange) {
const [start, end] = versionRange.split('-')
const startSemver = semverify(start)
const endSemver = semverify(end)
const versionsInRange = [];
let curVersion = startSemver;

while (semver.gte(endSemver, curVersion)) {
versionsInRange.push(curVersion)
curVersion = semver.inc(curVersion, 'minor')
}

return versionsInRange;
}

function normalizeQuery(query) {
let normalizedQuery = query
const regex = `(${Object.keys(browserNameMap).join('|')})`
Expand All @@ -143,7 +165,7 @@ function normalizeQuery(query) {
}

const parseBrowsersList = (browsersList) => {
return browsersList.map(browser => {
const browsers = browsersList.map(browser => {
const [browserName, browserVersion] = browser.split(' ')

let normalizedName = browserName
Expand All @@ -153,17 +175,22 @@ const parseBrowsersList = (browsersList) => {
normalizedName = browserNameMap[browserName]
}

try {
// Browser version can return as "10.0-10.2"
normalizedVersion = browserVersion.split('-')[0]
} catch (e) {
}

return {
family: normalizedName,
version: normalizedVersion,
// browserslist might return ranges (9.0-9.2), unwrap them
// see /~https://github.com/browserslist/browserslist-useragent/issues/41
if (browserVersion.indexOf('-') > 0) {
return generateSemversInRange(browserVersion).map(version => ({
family: normalizedName,
version,
}))
} else {
return {
family: normalizedName,
version: normalizedVersion,
}
}
})

return flatten(browsers);
}

const compareBrowserSemvers = (versionA, versionB, options) => {
Expand Down Expand Up @@ -196,6 +223,7 @@ const matchesUA = (uaString, opts = {}) => {
path: process.cwd()
})
const parsedBrowsers = parseBrowsersList(browsers)

const resolvedUserAgent = resolveUserAgent(uaString)

const options = {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
"build": "mkdir -p lib && babel index.js --out-file lib/index.js"
},
"dependencies": {
"browserslist": "^4.3.6",
"semver": "^5.6.0",
"browserslist": "^4.6.6",
"semver": "^6.3.0",
"useragent": "^2.3.0"
},
"devDependencies": {
Expand Down
32 changes: 24 additions & 8 deletions tests/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ const ua = require('useragent-generator')
const { resolveUserAgent, matchesUA, normalizeQuery } = require('../index')

const CustomUserAgentString = {
YANDEX: "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 YaBrowser/18.1.1.839 Yowser/2.5 Safari/537.36",
SAMSUNG_BROWSER_7_2: "Mozilla/5.0 (Linux; Android 6.0.1; SAMSUNG SM-N930F Build/MMB29K) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/7.2 Chrome/63.0.3239.111 Mobile Safari/537.36",
YANDEX: 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 YaBrowser/18.1.1.839 Yowser/2.5 Safari/537.36',
SAMSUNG_BROWSER_7_2: 'Mozilla/5.0 (Linux; Android 6.0.1; SAMSUNG SM-N930F Build/MMB29K) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/7.2 Chrome/63.0.3239.111 Mobile Safari/537.36',
}

it('normalizes queries properly', () => {
Expand Down Expand Up @@ -174,12 +174,12 @@ it('detects if browserslist matches UA', () => {
.toBeTruthy()

const modernList = [
"Firefox >= 53",
"Edge >= 15",
"Chrome >= 58",
"iOS >= 10",
"Safari >= 11",
"Samsung >= 7"
'Firefox >= 53',
'Edge >= 15',
'Chrome >= 58',
'iOS >= 10',
'Safari >= 11',
'Samsung >= 7'
]

expect(matchesUA(ua.safari.iOS(9), { browsers: modernList }))
Expand Down Expand Up @@ -278,3 +278,19 @@ it('parses semvers liberally', () => {
matchesUA('Opera/9.80 (Windows NT 6.1; U; es-ES) Presto/2.9.181 Version/12.0.0001', { browsers: ['opera >= 12'] }))
.toBeTruthy()
})

it('can deal with version ranges (if returned by browserslist)', () => {
expect(
matchesUA(ua.safari.iOS('9.1.0'), { browsers: ['ios_saf >= 9'] }))
.toBeTruthy()

expect(
matchesUA(ua.safari.iOS('9.0.0'), { browsers: ['ios_saf >= 9'] }))
.toBeTruthy()

// This should fail
// see /~https://github.com/browserslist/browserslist/issues/402
expect(
matchesUA(ua.safari.iOS('9.1.0'), { browsers: ['ios_saf >= 9.2'] }))
.toBeTruthy() // <-- should actually be falsy
})
46 changes: 23 additions & 23 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -901,14 +901,14 @@ browser-resolve@^1.11.3:
dependencies:
resolve "1.1.7"

browserslist@^4.3.6:
version "4.3.6"
resolved "https://registry.yarnpkg.com/browserslist/-/browserslist-4.3.6.tgz#0f9d9081afc66b36f477c6bdf3813f784f42396a"
integrity sha512-kMGKs4BTzRWviZ8yru18xBpx+CyHG9eqgRbj9XbE3IMgtczf4aiA0Y1YCpVdvUieKGZ03kolSPXqTcscBCb9qw==
browserslist@^4.6.6:
version "4.6.6"
resolved "https://registry.yarnpkg.com/browserslist/-/browserslist-4.6.6.tgz#6e4bf467cde520bc9dbdf3747dafa03531cec453"
integrity sha512-D2Nk3W9JL9Fp/gIcWei8LrERCS+eXu9AM5cfXA8WEZ84lFks+ARnZ0q/R69m2SV3Wjma83QDDPxsNKXUwdIsyA==
dependencies:
caniuse-lite "^1.0.30000921"
electron-to-chromium "^1.3.92"
node-releases "^1.1.1"
caniuse-lite "^1.0.30000984"
electron-to-chromium "^1.3.191"
node-releases "^1.1.25"

bser@^2.0.0:
version "2.0.0"
Expand Down Expand Up @@ -950,10 +950,10 @@ camelcase@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/camelcase/-/camelcase-4.1.0.tgz#d545635be1e33c542649c69173e5de6acfae34dd"

caniuse-lite@^1.0.30000921:
version "1.0.30000921"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30000921.tgz#7a607c1623444b22351d834e093aedda3c42fbe8"
integrity sha512-Bu09ciy0lMWLgpYC77I0YGuI8eFRBPPzaSOYJK1jTI64txCphYCqnWbxJYjHABYVt/TYX/p3jNjLBR87u1Bfpw==
caniuse-lite@^1.0.30000984:
version "1.0.30000987"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30000987.tgz#bc6b47217afd8226a2b1964635c6bff62cdf5738"
integrity sha512-O3VrjtRMTxoU5Cn5/QSmXeIR1gkVps4j9jqfIm4FLaQ5JzqBlVjMUG1xWnoYFv8N+H3Lp++aa05TekyIbjHL7g==

caseless@~0.12.0:
version "0.12.0"
Expand Down Expand Up @@ -1241,10 +1241,10 @@ ecc-jsbn@~0.1.1:
dependencies:
jsbn "~0.1.0"

electron-to-chromium@^1.3.92:
version "1.3.92"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.92.tgz#9027b5abaea400045edd652c0e4838675c814399"
integrity sha512-En051LMzMl3/asMWGZEtU808HOoVWIpmmZx1Ep8N+TT9e7z/X8RcLeBU2kLSNLGQ+5SuKELzMx+MVuTBXk6Q9w==
electron-to-chromium@^1.3.191:
version "1.3.206"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.206.tgz#f72a8694dce5c669170c78097177d7b601977c10"
integrity sha512-djq9Ehxs2rwPh7pyY1GMf8vkOWPoK8eoMp913E4t0UaEXvbnOsiSowBBHI8YoN4aKVQJjvxQp5I0LdZkT3Uzmw==

error-ex@^1.2.0:
version "1.3.1"
Expand Down Expand Up @@ -2728,10 +2728,10 @@ node-pre-gyp@^0.6.36:
tar "^2.2.1"
tar-pack "^3.4.0"

node-releases@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/node-releases/-/node-releases-1.1.1.tgz#8fff8aea1cfcad1fb4205f805149054fbf73cafd"
integrity sha512-2UXrBr6gvaebo5TNF84C66qyJJ6r0kxBObgZIDX3D3/mt1ADKiHux3NJPWisq0wxvJJdkjECH+9IIKYViKj71Q==
node-releases@^1.1.25:
version "1.1.26"
resolved "https://registry.yarnpkg.com/node-releases/-/node-releases-1.1.26.tgz#f30563edc5c7dc20cf524cc8652ffa7be0762937"
integrity sha512-fZPsuhhUHMTlfkhDLGtfY80DSJTjOcx+qD1j5pqPkuhUHVS7xHZIg9EE4DHK8O3f0zTxXHX5VIkDG8pu98/wfQ==
dependencies:
semver "^5.3.0"

Expand Down Expand Up @@ -3334,10 +3334,10 @@ semver@^5.4.1, semver@^5.5.0:
version "5.5.0"
resolved "https://registry.yarnpkg.com/semver/-/semver-5.5.0.tgz#dc4bbc7a6ca9d916dee5d43516f0092b58f7b8ab"

semver@^5.6.0:
version "5.6.0"
resolved "https://registry.yarnpkg.com/semver/-/semver-5.6.0.tgz#7e74256fbaa49c75aa7c7a205cc22799cac80004"
integrity sha512-RS9R6R35NYgQn++fkDWaOmqGoj4Ek9gGs+DPxNUZKuwE183xjJroKvyo1IzVFeXvUrvmALy6FWD5xrdJT25gMg==
semver@^6.3.0:
version "6.3.0"
resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.0.tgz#ee0a64c8af5e8ceea67687b133761e1becbd1d3d"
integrity sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==

set-blocking@^2.0.0, set-blocking@~2.0.0:
version "2.0.0"
Expand Down

0 comments on commit a868100

Please sign in to comment.