Giter Club home page Giter Club logo

ua-parser-caps's Introduction

ua-parser-caps

Adding capabilities to ua-parser2

NOTE: This projects data-sets (yaml files) are outdated as not beiing maintained any more...

NPM version Build Status

ua-parser-caps is a parser build upon the extracted data provided by ua-parser2. It adds capabilities to User-Agent String(s) with the following features:

  • Capabilities can be added using the dimensions OS, User-Agent and device.
  • Capabilities can be applied per regular-expression either on complete User-Agent strings or on any extracted value provided by the parsing result from ua-parser2.
  • Capabilities can be extended per OS, User-Agent and Device.
    • Extends can be chained (use capability3 based on capability2 based on capability1 ...).
  • Capabilities can be overwritten if criterias from another dimension match.
  • Capabilities can be attributed within separate files. This allows to choose the proper capabilities for your use-case.

Table of Contents

Capability Files

Within this project the following capability-files are provided:

  • caps_device_type.yaml contains a device-type classification of a User-Agent String.
  • caps_user_view.yaml contains a user view default preference classification.
  • caps_ie_compatibility.yaml contains information on Internet Explorer capability mode.

Details of the capabilities can be found within the files.

The set of capability files you want to use in your project can be changed within the file js/config.js.

Fastloading

To speed up the loading and parsing of the YAML files a preparsed js-module is generated.

Use js/bin/caps2js.js -c or make caps to generate a new version if the YAML files have changed.

npm packages always contain the latest capability files.

Usage

A sample usage is provided using the file js/test/sample.js.

var uaparser = require('ua-parser2')();
var capsparser = require('ua-parser-caps')();
var 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';

var uaparsed = uaparser.parse(userAgent);
var capabilities = capsparser.parse(uaparsed);
console.log(capabilities);

outputs:

{ device: { type: 'phablet' },
  user: { view: 'mobile' },
  info:
   { href:
      { '1': 'http://www.lg.com/uk/mobile',
        '2': 'http://www.lg.com/us/',
        '3': 'https://support.google.com/googleplay/answer/1727131?hl=en#L' } },
  screen: { size: 5.5, width: 1080, height: 1920 } }

See sample.js.

Asynchronous Loading

var 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";

var uaparser = require('ua-parser2')();
require('ua-parser-caps')(function(err, capsparser){
  /// async loading completed
  if (!err) {
    var uaparsed = uaparser.parse(userAgent);
    var capabilities = capsparser.parse(uaparsed);
    console.log(capabilities);
  }
});

See sampleAsync.js.

Specification

If you are interested to contribute data or even write your own capability files then please take a look into the specification.

Files

  • ./

    • caps_*.yaml capability files
  • ./doc

    • specification.md Specification on capability files.
  • ./js

    • index.js A node.js implementation of ua-parser-cap.
    • config.js Configuration file for the parser.
  • ./js/test

    • test files for the parser.
  • ./test/resources/parser

    • test files as yaml files to test a ua-parser-cap implementation.

Contribution and License Agreement

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

If you contribute data to this project, you are implicitly allowing your code to be distributed under the CC-BY-4.0 license. You are also implicitly verifying that all data is your original work.

Please read the contributors' guide.

License

Copyright (c) 2013 commenthol

Software is released under MIT.
Data provided within Yaml-Files is released under CC-BY-4.0.

ua-parser-caps's People

Contributors

commenthol avatar mrjgreen avatar

Stargazers

Mani Gandham avatar  avatar Marcelo Mármol avatar  avatar

Watchers

James Cloos avatar  avatar Marcelo Mármol avatar  avatar  avatar  avatar

Forkers

haxorton

ua-parser-caps's Issues

Huawei Media Pad

Hi - not sure if this is the best way for me to keep feeding back - please let me know if theres a format you would prefer, or extra info you would like me to include etc..

The huawei media pad GT01 is identifying as a smart phone, when it should probably be a tablet.

It looks like there is a regex to catch this for the MediaPad, but for some reason the js library isn't picking that up... When I run it against my PHP version, it does catch that regex.

model:
    regexes:
        - regex: "(MediaPad|SpringBoard)"
          capabilities:
               device:
               type: tablet

I've also created a gist, with the full output trace through the tree from my tests: https://gist.github.com/mrjgreen/2d7e8c359bb2dfb2b0e1

Huawei

http://www.pocketdroid.net/2012/06/05/e-mobile-announces-huawei-mediapad-gt01-10-1-fullhd-1920x1200p-resolution-android-tablet/

===================================
Test Data:
    "os": {
        "family": "Android",
        "major": "4",
        "minor": "0",
        "patch": "4",
        "patchMinor": null
    },
    "ua": {
        "family": "Android",
        "major": "4",
        "minor": "0",
        "patch": "4"
    },
    "device": {
        "family": "GT01",
        "brand": "Huawei",
        "model": "GT01"
    },
    "string": "Mozilla\/5.0 (Linux; U; Android 4.0.4; en-us; GT01 Build\/HuaweiMediaPad) AppleWebKit\/534.30 (KHTML, like Gecko) Version\/4.0 Safari\/534.30"
===================================
Expected:
    "device": {
        "type": "smartphone"
    },
    "user": {
        "view": "tablet"
    }
===================================
Actual:
    "device": {
        "type": "tablet"
    },
    "user": {
        "view": "tablet"
    }
===================================

Sony Tablet P - Tests expect "smartphone"

Looks like there are 4 failing expectations for the sony tablet p because the tests are expecting "smartphone"

http://i.imgur.com/hUEVHaU.jpg - Looks like a tablet, so the library is correct, just the tests wrong.

Will submit a PR shortly so you can merge if you agree it is a tablet.

✘ 11743 / 62364 Mozilla/5.0 (Linux; Android 4.0.3; Sony Tablet P Build/TISU0144) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166  Safari/535.19
===================================
Test Data:
    "os": {
        "family": "Android",
        "major": "4",
        "minor": "0",
        "patch": "3",
        "patchMinor": null
    },
    "ua": {
        "family": "Chrome",
        "major": "18",
        "minor": "0",
        "patch": "1025"
    },
    "device": {
        "family": "Sony Tablet P",
        "brand": "Sony",
        "model": "Tablet P"
    },
    "string": "Mozilla\/5.0 (Linux; Android 4.0.3; Sony Tablet P Build\/TISU0144) AppleWebKit\/535.19 (KHTML, like Gecko) Chrome\/18.0.1025.166  Safari\/535.19"
===================================
Expected:
    "device": {
        "type": "smartphone"
    },
    "user": {
        "view": "tablet"
    }
===================================
Actual:
    "device": {
        "type": "tablet"
    },
    "user": {
        "view": "tablet"
    }
===================================

✘ 26731 / 62364 Mozilla/5.0 (Linux; U; Android 3.2; de-de; Sony Tablet P Build/THMAS11001) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13
===================================
Test Data:
    "os": {
        "family": "Android",
        "major": "3",
        "minor": "2",
        "patch": null,
        "patchMinor": null
    },
    "ua": {
        "family": "Android",
        "major": "3",
        "minor": "2",
        "patch": null
    },
    "device": {
        "family": "Sony Tablet P",
        "brand": "Sony",
        "model": "Tablet P"
    },
    "string": "Mozilla\/5.0 (Linux; U; Android 3.2; de-de; Sony Tablet P Build\/THMAS11001) AppleWebKit\/534.13 (KHTML, like Gecko) Version\/4.0 Safari\/534.13"
===================================
Expected:
    "device": {
        "type": "smartphone"
    },
    "user": {
        "view": "tablet"
    }
===================================
Actual:
    "device": {
        "type": "tablet"
    },
    "user": {
        "view": "tablet"
    }
===================================

✘ 27899 / 62364 Mozilla/5.0 (Linux; U; Android 4.0.3; pl-pl; Sony Tablet P Build/TISU0144) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Safari/534.30
===================================
Test Data:
    "os": {
        "family": "Android",
        "major": "4",
        "minor": "0",
        "patch": "3",
        "patchMinor": null
    },
    "ua": {
        "family": "Android",
        "major": "4",
        "minor": "0",
        "patch": "3"
    },
    "device": {
        "family": "Sony Tablet P",
        "brand": "Sony",
        "model": "Tablet P"
    },
    "string": "Mozilla\/5.0 (Linux; U; Android 4.0.3; pl-pl; Sony Tablet P Build\/TISU0144) AppleWebKit\/534.30 (KHTML, like Gecko) Version\/4.0 Safari\/534.30"
===================================
Expected:
    "device": {
        "type": "smartphone"
    },
    "user": {
        "view": "tablet"
    }
===================================
Actual:
    "device": {
        "type": "tablet"
    },
    "user": {
        "view": "tablet"
    }
===================================

✘ 33013 / 62364 Mozilla/5.0 (Linux; U; Android 4.1.1; en-gb; Sony tablet p Build/JRO03C) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Safari/534.30
===================================
Test Data:
    "os": {
        "family": "Android",
        "major": "4",
        "minor": "1",
        "patch": "1",
        "patchMinor": null
    },
    "ua": {
        "family": "Android",
        "major": "4",
        "minor": "1",
        "patch": "1"
    },
    "device": {
        "family": "Sony tablet p",
        "brand": "Sony",
        "model": "tablet p"
    },
    "string": "Mozilla\/5.0 (Linux; U; Android 4.1.1; en-gb; Sony tablet p Build\/JRO03C) AppleWebKit\/534.30 (KHTML, like Gecko) Version\/4.0 Safari\/534.30"
===================================
Expected:
    "device": {
        "type": "smartphone"
    },
    "user": {
        "view": "tablet"
    }
===================================
Actual:
    "device": {
        "type": "tablet"
    },
    "user": {
        "view": "tablet"
    }
===================================

Cheers

TypeError when using your package

Hi, I'm getting this error when using your package var caps = require('ua-parser-caps')()

error: Sending 500 ("Server Error") response: 
 TypeError: Cannot assign to read only property 'greybg' of Z3735G Atom
    at /src/node_modules/ua-parser-caps/js/lib/tree.js:247:16
    at Object.<anonymous> (/src/node_modules/ua-parser-caps/node_modules/js-select/index.js:18:51)
    at Object.<anonymous> (/src/node_modules/ua-parser-caps/node_modules/js-select/index.js:67:19)
    at walker (/src/node_modules/ua-parser-caps/node_modules/js-select/node_modules/traverse/index.js:258:22)
    at /src/node_modules/ua-parser-caps/node_modules/js-select/node_modules/traverse/index.js:274:29
    at Array.forEach (native)
    at walker (/src/node_modules/ua-parser-caps/node_modules/js-select/node_modules/traverse/index.js:269:24)
    at /src/node_modules/ua-parser-caps/node_modules/js-select/node_modules/traverse/index.js:274:29
    at Array.forEach (native)
    at walker (/src/node_modules/ua-parser-caps/node_modules/js-select/node_modules/traverse/index.js:269:24)
    at /src/node_modules/ua-parser-caps/node_modules/js-select/node_modules/traverse/index.js:274:29
    at Array.forEach (native)
    at walker (/src/node_modules/ua-parser-caps/node_modules/js-select/node_modules/traverse/index.js:269:24)
    at /src/node_modules/ua-parser-caps/node_modules/js-select/node_modules/traverse/index.js:274:29
    at Array.forEach (native)
    at walker (/src/node_modules/ua-parser-caps/node_modules/js-select/node_modules/traverse/index.js:269:24) [TypeError: Cannot assign to read only property 'greybg' of Z3735G Atom]

Separate yam files from project?

@commenthol @mrjgreen @haxorton I will starting a Java implementation on this project. Do you guys care to separete the base yaml files to a core project from the javascript implementation?
I think it will help to make things clearer. Also it may be helpful to have a list of data and results expected to make commons test between different implementations. Does it make any sense?

Regards,

Help with logic?

Hey - this is probably not the right place to do this but I wondered if you could help with a PHP implementation I've been working on here https://github.com/mrjgreen/ua-parser-caps-php

I'm down to 5 failing tests, and they all seem to be related to the regex logic here. I can't quite work out what its supposed to be doing. Could you explain what the path is through the tree once its matched a regex? https://github.com/commenthol/ua-parser-caps/blob/master/js/lib/parser.js#L104

Appreciate any help you can offer!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.