Skip to content

Commit

Permalink
Add custom statistics support to coverage calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
ai committed Feb 10, 2018
1 parent 84c2194 commit 4748478
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 14 deletions.
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -407,11 +407,18 @@ browserslist "> 1%, last 2 versions"
You can get total users coverage for selected browsers by JS API:
```js
browserslist.coverage(browserslist('> 1%')) //=> 81.4
browserslist.coverage(browserslist('> 1%'))
//=> 81.4
```
```js
browserslist.coverage(browserslist('> 1% in US'), 'US') //=> 83.1
browserslist.coverage(browserslist('> 1% in US'), 'US')
//=> 83.1
```
```js
browserslist.coverage(browserslist('> 1% in my stats', { stats }), stats)
//=> 82.2
```
Or by CLI:
Expand All @@ -426,6 +433,11 @@ $ browserslist --coverage=US "> 1% in US"
These browsers account for 83.1% of all users in the US
```
```sh
$ browserslist --coverage "> 1% in my stats" --stats=./browserslist-stats.json
These browsers account for 83.1% of all users in custom statistics
```
## Cache
Browserslist caches the configuration it reads from `package.json` and
Expand Down
12 changes: 11 additions & 1 deletion cli.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env node

var fs = require('fs')

var browserslist = require('./')
var pkg = require('./package.json')
var args = process.argv.slice(2)
Expand Down Expand Up @@ -88,12 +90,20 @@ if (isArg('--help') || isArg('-h')) {
process.stdout.write(browser + '\n')
})
} else {
var result = browserslist.coverage(browsers, country)
var stats
if (country) {
stats = country
} else if (opts.stats) {
stats = JSON.parse(fs.readFileSync(opts.stats))
}
var result = browserslist.coverage(browsers, stats)
var round = Math.round(result * 100) / 100.0

var end = 'globally'
if (country && country !== 'global') {
end = 'in the ' + country.toUpperCase()
} else if (opts.stats) {
end = 'in custom statistics'
}

process.stdout.write(
Expand Down
33 changes: 23 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,29 +276,42 @@ browserslist.loadConfig = env.loadConfig
* Return browsers market coverage.
*
* @param {string[]} browsers Browsers names in Can I Use.
* @param {string} [country="global"] Which country statistics should be used.
* @param {string|object} [stats="global"] Which statistics should be used.
* Country code or custom statistics.
*
* @return {number} Total market coverage for all selected browsers.
*
* @example
* browserslist.coverage(browserslist('> 1% in US'), 'US') //=> 83.1
*/
browserslist.coverage = function (browsers, country) {
if (country && country !== 'global') {
if (country.length > 2) {
country = country.toLowerCase()
browserslist.coverage = function (browsers, stats) {
var data
if (typeof stats === 'undefined') {
data = browserslist.usage.global
} else if (typeof stats === 'string') {
if (stats.length > 2) {
stats = stats.toLowerCase()
} else {
country = country.toUpperCase()
stats = stats.toUpperCase()
}
env.loadCountry(browserslist.usage, country)
env.loadCountry(browserslist.usage, stats)
data = browserslist.usage[stats]
} else {
country = 'global'
if ('dataByBrowser' in stats) {
stats = stats.dataByBrowser
}
data = { }
for (var name in stats) {
for (var version in stats[name]) {
data[name + ' ' + version] = stats[name][version]
}
}
}

return browsers.reduce(function (all, i) {
var usage = browserslist.usage[country][i]
var usage = data[i]
if (usage === undefined) {
usage = browserslist.usage[country][i.replace(/ [\d.]+$/, ' 0')]
usage = data[i.replace(/ [\d.]+$/, ' 0')]
}
return all + (usage || 0)
}, 0)
Expand Down
9 changes: 8 additions & 1 deletion test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,15 @@ it('returns usage from config', () => {
})
})

it('support custom stats', () => {
it('supports custom stats', () => {
return run('--stats=' + STATS, '> 5% in my stats').then(out => {
expect(toArray(out)).toEqual(['ie 11', 'ie 10'])
})
})

it('supports custom stats in coverage', () => {
return run('--coverage', '--stats=' + STATS, '> 5% in my stats').then(out => {
expect(out).toEqual(
'These browsers account for 15.7% of all users in custom statistics\n')
})
})
12 changes: 12 additions & 0 deletions test/coverage.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
var browserslist = require('../')

var custom = {
ie: {
8: 3,
9: 10
}
}

var originUsage = browserslist.usage

beforeEach(() => {
Expand Down Expand Up @@ -53,3 +60,8 @@ it('loads continents usage data from Can I Use', () => {
it('fixes statistics of 0 version', () => {
expect(browserslist.coverage(['ie 9'], 'RU')).toEqual(2)
})

it('supports custom statistics', () => {
expect(browserslist.coverage(['ie 9'], custom)).toEqual(10)
expect(browserslist.coverage(['ie 9'], { dataByBrowser: custom })).toEqual(10)
})

0 comments on commit 4748478

Please sign in to comment.