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

user-agent-bag

v0.3.0

Published

RFC-respecting User-Agent serialization and deserialization

Downloads

134,411

Readme

User Agent Bag

Parse User-Agents per RFC7231. Doesn't handle all the weirdness around real User-Agents, just parses things per the spec.

const UserAgentBag = require("user-agent-bag");

const firefoxBag = new UserAgentBag(
  "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:47.0) Gecko/20100101 Firefox/47.0"
);
firefoxBag.get("Mozilla");
// => '5.0'
firefoxBag.has("Gecko");
// => true

const customBag = new UserAgentBag([
  ["Foo", "bar"],
  ["Baz", null],
]);
customBag.toString();
// => 'Foo/bar Baz'

API documentation

Creates a new UserAgentBag by parsing string as a User-Agent per RFC7231. string must have a length of 256 characters or less (this limit may be configurable in the future). If there are any errors in parsing, the bag will be empty.

const validBag = new UserAgentBag("Foo/1.2");
validBag.get("Foo");
// => '1.2'

const invalidStringBag = new UserAgentBag(
  "Foo/1.2 IsInvalidBecauseVersionIsMissing/"
);
invalidStringBag.get("Foo");
// => undefined

Creates a new UserAgentBag from iterable. Elements of iterable are key-value pairs.

const bagFromEntries = new UserAgentBag([
  ["Foo", "1.2"],
  ["Bar", null],
]);
bagFromEntries.toString();
// => 'Foo/1.2 Bar'

const myMap = new Map();
myMap.set("Baz", "5");
myMap.set("Qux", "6");
const bagFromMap = new UserAgentBag(myMap);
bagFromMap.toString();
// => 'Baz/5 Qux/6'

Returns an iterable yielding each of the product-version pairs in the bag. Like Map.prototype.entries.

const bag = new UserAgentBag("Foo/1.2 Bar Baz/3.4");

for (const [product, version] of bag.entries()) {
  console.log(product + " version " + version);
}
// Logs:
// Foo version 1.2
// Bar version null
// Baz version 3.4

Returns the version of the product. If product is in the bag multiple times, only the first value is returned. If no version is specified, null is returned. If the product is missing from the bag, undefined is returned.

const bag = new UserAgentBag("Foo/1.2 Bar/4.5 Bar/6.7 Baz");

bag.get("Foo");
// => '1.2'

bag.get("Bar");
// => '4.5'

bag.get("Baz");
// => null

bag.get("missing");
// => undefined

bag.get("foo");
// => undefined

Returns all specified versions of the product as an array. null represents the absence of a version. If the product is missing from the bag, the empty array is returned.

const bag = new UserAgentBag("Foo/1.2 Bar/4.5 Bar/null");

bag.getAll("Foo");
// => ['1.2']

bag.getAll("Bar");
// => ['4.5', null]

bag.get("missing");
// => []

Returns true if product is in the bag, false otherwise.

const bag = new UserAgentBag("Foo/1.2 Bar");

bag.has("Foo");
// => true

bag.has("Bar");
// => true

bag.has("missing");
// => false

Returns the number of products in the bag.

const bag = new UserAgentBag("Foo/1.2 (ignored comment) Bar/3 Bar/4");
bag.size();
// => 3

Converts the bag to a string. Useful when constructing your own User-Agents.

const bag = new UserAgentBag([
  ["Foo", "1.2"],
  ["Bar", null],
]);
bag.toString();
// => 'Foo/1.2 Bar'