ua-parser2

0.5.1 • Public • Published

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:

  • node-uap@0.0.3
  • useragent@2.2.1
  • ua-parser2@0.3.2
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.

Package Sidebar

Install

npm i ua-parser2

Weekly Downloads

2,338

Version

0.5.1

License

(MIT OR Apache-2.0)

Unpacked Size

304 kB

Total Files

18

Last publish

Collaborators

  • commenthol