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

ua-parser2

v0.5.1

Published

An improved user agent parser.

Downloads

7,657

Readme

ua-parser2

NPM version Build Status

This is an improved fork from ua-parser which contains the following changes documented in the Changelog.

The crux of the original parser --the data collected by Steve Souders over the years-- has been extracted into a separate YAML file so as to be reusable as is by implementations in other programming languages.

The main differences to ua-parser are:

  • Parsing Engines - Detection of MSIE compatibility modes
  • Grouping of regexes - Speeds up User-Agent detection by > 200%
  • Adding type attribute - tag a user-agent category with a specific type such as. 'bot', 'mail', 'feedreader', 'app', ...
  • Bundled tools for contribution.
  • Running your own regexes.yaml file
  • Backwards Compatibility using the "old" UAParser result object is broken.

Contributing Changes to regexes.yaml

Please read the contributors' guide

Specification

A Specification, e.g. for porting into other computer languages of the parsing rules for the regexes.yaml file is available.

Usage

API Documentation

var uaParser = require('ua-parser2')(/* [optional] path to your regexes.yaml file */);

var res,
    userAgent = "Mozilla/5.0 (Linux; Android 4.3.1; LG-E980 Build/JLS36I) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.59 Mobile Safari/537.36";

res = uaParser.parse(userAgent);

console.log(res);

There is a sample in ./js/test/sample.js which can be executed from the commandline.

node js/test/sample.js "Mozilla/5.0 (Linux; Android 4.3.1; LG-E980 Build/JLS36I) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.59 Mobile Safari/537.36"
#> {
  "ua": {
    "family": "Chrome Mobile",
    "major": "31",
    "minor": "0",
    "patch": "1650"
  },
  "engine": {
    "family": "Blink",
    "major": "31",
    "minor": "0",
    "patch": "1650"
  },
  "os": {
    "family": "Android",
    "major": "4",
    "minor": "3",
    "patch": "1",
    "patchMinor": null
  },
  "device": {
    "family": "LG-E980",
    "brand": "LG",
    "model": "E980",
    "type": "smartphone"
  },
  "string": "Mozilla/5.0 (Linux; Android 4.3.1; LG-E980 Build/JLS36I) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.59 Mobile Safari/537.36"
}

node js/test/sample.js "AdsBot-Google-Mobile ( http://www.google.com/mobile/adsbot.html) Mozilla (iPhone; U; CPU iPhone OS 3 0 like Mac OS X) AppleWebKit (KHTML, like Gecko)"
#> {
  "ua": {
    "family": "AdsBot-Google-Mobile",
    ...
    "type": "bot"
  },
  "engine": {
    "family": "Webkit",
    ...
    "type": "Apple"
  },
  "os": {
    "family": "Other",
    ...
  },
  "device": {
    "family": "iPhone",
    "brand": "Apple",
    "model": "iPhone",
    "type": "smartphone"    
  },
  "string": "AdsBot-Google-Mobile ( http://www.google.com/mobile/adsbot.html) Mozilla (iPhone; U; CPU iPhone OS 3 0 like Mac OS X) AppleWebKit (KHTML, like Gecko)"
}

Note if you're only interested in one of the ua, device or os objects, you will getter better performance by using the more specific methods (uaParser.parseUA, uaParser.parseOS and uaParser.parseDevice respectively), e.g.:

var p = require('ua-parser2')();

var userAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) CriOS/27.0.1453.10 Mobile/9B179 Safari/7534.48.3";

console.log(p.parseUA(userAgent).toString());
//> "Chrome Mobile iOS 27.0.1453"
console.log(p.parseEngine(userAgent).toString());
//> "AppleWebkit 534.46"
console.log(p.parseOS(userAgent).toString());
//> "iOS 5.1"
console.log(p.parseDevice(userAgent).toString());
//> "Apple iPhone"

Benchmarks

In folder benchmarks you'll find a benchmark test which compares useragent, node-uap with ua-parser2.

Results on my laptop:

Starting the benchmark, parsing 63 useragent strings per run

Executed benchmark against node module: "useragent"
Count (3), Cycles (2), Elapsed (5.782), Hz (40.84696387495286)

Executed benchmark against node module: "node-uap"
Count (3), Cycles (2), Elapsed (5.628), Hz (41.23226067803832)

Executed benchmark against node module: "ua-parser2"
Count (7), Cycles (4), Elapsed (5.568), Hz (108.4435776239968)

Module: "ua-parser2" is the user agent fastest parser.

Contribution and License Agreement

If you contribute code to this project, you are implicitly allowing your code to be distributed under the MIT license. For contribution to the regexes.yaml you are implicitly allowing your code to be distributed under the Apache License license You are also implicitly verifying that all code is your original work.

License

The data contained in regexes.yaml is Copyright 2014 commenthol, 2009 Google Inc. and available under the Apache License, Version 2.0.

The JS port is Copyright 2014 commenthol, 2010 Tobie Langel and is available under your choice of MIT or Apache Version 2.0 license.