How to use the coverage function from browserslist

Find comprehensive JavaScript browserslist.coverage code examples handpicked from public code repositorys.

browserslist.coverage is a tool that analyzes a website or application's usage statistics to determine which browsers are most commonly used by visitors or users.

51
52
53
54
55
56
57
58
59
60
  let list = versions[browser]
  list = list.sort((a, b) => parseFloat(b) - parseFloat(a))
  out += `  ${browser}: ${list.join(', ')}\n`
}

let coverage = browserslist.coverage(prefixes.browsers.selected)
let round = Math.round(coverage * 100) / 100.0
out += `\nThese browsers account for ${round}% of all users globally\n`

let atrules = []
fork icon3
star icon0
watch icon0

64
65
66
67
68
69
70
71
72
    return parseFloat(b) - parseFloat(a);
  });
  out += "  " + browser + ": " + list.join(', ') + "\n";
}

var coverage = browserslist.coverage(prefixes.browsers.selected);
var round = Math.round(coverage * 100) / 100.0;
out += "\nThese browsers account for " + round + "% of all users globally\n";
var atrules = [];
fork icon0
star icon2
watch icon0

How does browserslist.coverage work?

browserslist.coverage is a feature provided by the browserslist library that helps developers determine which browsers to support when developing web applications or websites.

When browserslist.coverage is called with a list of browsers and a usage statistics file or URL, it calculates the percentage of users who are able to access the website or application using each browser.

To perform this calculation, browserslist.coverage uses the browserslist library's built-in support for Can I Use database to obtain the latest browser versions and their corresponding global usage statistics.

browserslist.coverage then compares this data against the usage statistics provided for the specific website or application, and generates a report that shows which browsers are used most frequently by the website's visitors or users, and which browsers have low or negligible usage.

By analyzing this data, developers can make informed decisions about which browsers to prioritize and support when developing new features or optimizing existing ones, based on the actual usage patterns of their users. This can help improve the user experience and ensure that the website or application is accessible to the widest possible audience.

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const browserslist = require("browserslist");

const browsers = [
  "last 2 Chrome versions",
  "last 2 Firefox versions",
  "last 2 Safari versions",
  "last 2 Edge versions",
];

const coverage = browserslist.coverage({
  browsers,
  stats: "https://my-website.com/stats.json",
});

console.log(coverage);

In this example, we import the browserslist library and define a list of browsers to check for support. We then call browserslist.coverage with two arguments: the browsers array, which specifies which browsers to check, and a stats URL that provides usage statistics for the website or application. browserslist.coverage then calculates the browser usage coverage of the website or application based on the specified browser list and usage statistics, and returns an object that shows the percentage of users who can access the website or application using each browser. The resulting coverage object might look something like this: json Copy code