npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ad2302/browserslist-useragent-regexp

v3.1.2

Published

A utility to compile browserslist query to a RegExp to test browser useragent.

Downloads

40

Readme

browserslist-useragent-regexp

NPM version Node version Dependencies status Build status Coverage status Dependabot badge Documentation badge

A utility to compile browserslist query to a RegExp to test browser useragent. Simplest example: you can detect supported browsers on client-side.

  1. Create .browserslistrc config, for example like this:
last 2 versions
not dead
  1. Add script to package.json:
{
  "scripts": {
    "supportedBrowsers": "browserslist-useragent-regexp --allowHigherVersions -o supportedBrowsers.js"
  }
}

or supportedBrowsers.ts for typescript.

  1. Run this script, to compile RegExp:
npm run supportedBrowsers
# or
yarn supportedBrowsers

supportedBrowsers.js:

module.exports = /((CPU[ +]OS|iPhone[ +]OS|CPU[ +]iPhone|CPU IPhone OS)[ +]+(11[_\.](3|4)|12[_\.](0|1))(?:[_\.]\d+)?)|(OperaMini(?:\/att)?\/?(\d+)?(?:\.\d+)?(?:\.\d+)?)|(Opera\/.+Opera Mobi.+Version\/46\.0)|(Opera\/46\.0.+Opera Mobi)|(Opera Mobi.+Opera(?:\/|\s+)46\.0)|(SamsungBrowser\/(8|9)\.2)|(Edge\/(17|18)(?:\.0)?)|(HeadlessChrome(?:\/(72|73)\.0\.\d+)?)|((Chromium|Chrome)\/(72|73)\.0(?:\.\d+)?)|(IEMobile[ \/]11\.0)|(Version\/12\.(0|1)(?:\.\d+)?.*Safari\/)|(Trident\/7\.0)|(Firefox\/(65|66)\.0\.\d+)|(Firefox\/(65|66)\.0(pre|[ab]\d+[a-z]*)?)|(([MS]?IE) 11\.0)/;
  1. Import RegExp from created file:
const {SupportedBrowsers} = require('./supportedBrowsers');

if (SupportedBrowsers.test(navigator.userAgent)) {
    alert('Your browser is supported.');
}

for ts

import {SupportedBrowsers} from './supportedBrowsers';

Install

npm i -D browserslist-useragent-regexp
# or
yarn add -D browserslist-useragent-regexp

Why?

As was written in article "Smart Bundling: Shipping legacy code to only legacy browsers": you can determinate, which bundle you should give to browser from server with browserslist-useragent. But in this case you must have your own server with special logic. Now, with browserslist-useragent-regexp, you can move that to client-side.

Development was inspired by this proposal from Mathias Bynens.

How to make differential resource loading and other optimizations with browserslist-useragent-regexp you can read in article "Speed up with Browserslist".

Demo (sources, build script).

CLI

npx browserslist-useragent-regexp [query] [...options]
# or
yarn exec -- browserslist-useragent-regexp [query] [...options]

| Option | Description | Default | |--------|-------------|---------| | query | Manually provide a browserslist query. Specifying this overrides the browserslist configuration specified in your project. | | | ‑‑help, -h | Print this message. | | | ‑‑verbose, -v | Print additional info about RegExps. | | | ‑‑ignorePatch | Ignore differences in patch browser numbers. | true | | ‑‑ignoreMinor | Ignore differences in minor browser versions. | false | | ‑‑allowHigherVersions | For all the browsers in the browserslist query, return a match if the useragent version is equal to or higher than the one specified in browserslist. | false | | ‑‑allowZeroSubversions | Ignore match of patch or patch and minor, if they are 0. | false |

JS API basics

Module exposes two main methods:

getUserAgentRegExps(options)

Compile browserslist query to RegExps for each browser.

getUserAgentRegExp(options)

Compile browserslist query to one RegExp.

Description of all methods you can find in Documentation.

Options

| Option | Type | Default | Description | |--------|------|---------|-------------| | browsers | string \| string[] | — | Manually provide a browserslist query (or an array of queries). Specifying this overrides the browserslist configuration specified in your project. | | env | string | — | When multiple browserslist environments are specified, pick the config belonging to this environment. | | ignorePatch | boolean | true | Ignore differences in patch browser numbers. | | ignoreMinor | boolean | false | Ignore differences in minor browser versions. | | allowHigherVersions | boolean | false | For all the browsers in the browserslist query, return a match if the useragent version is equal to or higher than the one specified in browserslist. | | allowZeroSubversions | boolean | false | Ignore match of patch or patch and minor, if they are 0. |

RegExp info object

| Property | Type | Description | |----------|------|-------------| | family | string | Browser family. | | requestVersions | [number, number, number][] | Versions provided by browserslist. | | regExp | RegExp | RegExp to match useragent with family and versions. | | sourceRegExp | RegExp | Original useragent RegExp, without versions. | | resultFixedVersion | [number, number, number] \| null | Useragent version of RegExp. | | resultMinVersion | [number, number, number] \| null | Useragent min version of RegExp. | | resultMaxVersion | [number, number, number] \| null | Useragent max version of RegExp. |

Other