Skip to content

Commit

Permalink
Ignore null usage value (fix browserslist#658) (browserslist#660)
Browse files Browse the repository at this point in the history
* Ignore `null` usage value (fix browserslist#658)

* Add more tests
  • Loading branch information
g-plane authored and zhouyu9527 committed Jul 4, 2022
1 parent 4db44ba commit 77f47d3
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 16 deletions.
39 changes: 27 additions & 12 deletions index.js
Expand Up @@ -831,19 +831,24 @@ var QUERIES = [
}
var usage = context.customUsage
return Object.keys(usage).reduce(function (result, version) {
var percentage = usage[version]
if (percentage == null) {
return result
}

if (sign === '>') {
if (usage[version] > popularity) {
if (percentage > popularity) {
result.push(version)
}
} else if (sign === '<') {
if (usage[version] < popularity) {
if (percentage < popularity) {
result.push(version)
}
} else if (sign === '<=') {
if (usage[version] <= popularity) {
if (percentage <= popularity) {
result.push(version)
}
} else if (usage[version] >= popularity) {
} else if (percentage >= popularity) {
result.push(version)
}
return result
Expand All @@ -866,19 +871,24 @@ var QUERIES = [
}
var usage = context.customUsage
return Object.keys(usage).reduce(function (result, version) {
var percentage = usage[version]
if (percentage == null) {
return result
}

if (sign === '>') {
if (usage[version] > popularity) {
if (percentage > popularity) {
result.push(version)
}
} else if (sign === '<') {
if (usage[version] < popularity) {
if (percentage < popularity) {
result.push(version)
}
} else if (sign === '<=') {
if (usage[version] <= popularity) {
if (percentage <= popularity) {
result.push(version)
}
} else if (usage[version] >= popularity) {
} else if (percentage >= popularity) {
result.push(version)
}
return result
Expand All @@ -897,19 +907,24 @@ var QUERIES = [
env.loadCountry(browserslist.usage, place, browserslist.data)
var usage = browserslist.usage[place]
return Object.keys(usage).reduce(function (result, version) {
var percentage = usage[version]
if (percentage == null) {
return result
}

if (sign === '>') {
if (usage[version] > popularity) {
if (percentage > popularity) {
result.push(version)
}
} else if (sign === '<') {
if (usage[version] < popularity) {
if (percentage < popularity) {
result.push(version)
}
} else if (sign === '<=') {
if (usage[version] <= popularity) {
if (percentage <= popularity) {
result.push(version)
}
} else if (usage[version] >= popularity) {
} else if (percentage >= popularity) {
result.push(version)
}
return result
Expand Down
4 changes: 2 additions & 2 deletions test/and.test.js
@@ -1,5 +1,5 @@
let { test } = require('uvu')
let { is, equal } = require('uvu/assert')
let { equal } = require('uvu/assert')
let { join } = require('path')

delete require.cache[require.resolve('..')]
Expand Down Expand Up @@ -36,7 +36,7 @@ test('query composition with AND operator', () => {
})

test('correctly works with not and one-version browsers as AND query', () => {
is(browserslist('last 1 Baidu version and not <2% in AT').length, 0)
equal(browserslist('last 1 Baidu version and not <2% in AT'), ['baidu 7.12'])
})

test('reads config from package.json', () => {
Expand Down
1 change: 1 addition & 0 deletions test/country.test.js
Expand Up @@ -9,6 +9,7 @@ let originUsage = browserslist.usage
test.before.each(() => {
browserslist.usage = {
US: {
'chrome 999': null, // unreleased
'ie 8': 1,
'ie 9': 5,
'ie 10': 10.1,
Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/stats.json
Expand Up @@ -10,7 +10,8 @@
"34": 0.2,
"35": 0.7,
"36": 2.3,
"37": 3.5
"37": 3.5,
"999": null
}
}
}
2 changes: 1 addition & 1 deletion test/main.test.js
Expand Up @@ -257,7 +257,7 @@ test('uses production environment by default', () => {
})

test('correctly works with not and one-version browsers', () => {
is(browserslist('last 1 Baidu version, not <2% in AT').length, 0)
equal(browserslist('last 1 Baidu version, not <2% in AT'), ['baidu 7.12'])
})

test.run()
9 changes: 9 additions & 0 deletions test/shareable-stats.test.js
Expand Up @@ -47,6 +47,15 @@ test('takes stats and queries from shareable config', async () => {
equal(browserslist('extends browserslist-config-test2'), ['ie 11'])
})

test('ignores null usage value', async () => {
await mock(
'browserslist-config-null-test',
undefined,
{ chrome: { 90: 3, 999: null } }
)
equal(browserslist('< 5% in browserslist-config-null-test stats'), ['chrome 90'])
})

test('works with non-prefixed stats with dangerousExtend', async () => {
await mock('pkg', undefined, { chrome: { 78: 6 } })
equal(
Expand Down

0 comments on commit 77f47d3

Please sign in to comment.