From d75f84820a302f579606f893e00cdf9c56445954 Mon Sep 17 00:00:00 2001 From: Faisal Salman Date: Sat, 16 Mar 2013 02:09:12 +0700 Subject: [PATCH] Some minor revisions --- component.json | 9 ++-- package.json | 6 ++- readme.md | 102 ++++++++++++++++++++++++++++----------- src/ua-parser.js | 68 +++++++++++++++++++------- src/ua-parser.min.js | 7 +++ ua-parser-js.jquery.json | 4 +- 6 files changed, 143 insertions(+), 53 deletions(-) create mode 100644 src/ua-parser.min.js diff --git a/component.json b/component.json index ae0c12b..bd20916 100644 --- a/component.json +++ b/component.json @@ -1,13 +1,14 @@ { "name": "ua-parser-js", - "version": "0.5.15", + "version": "0.5.20", "description": "Lightweight JavaScript-based user-agent string parser", "keywords": ["user-agent", "parser", "browser", "engine", "os", "device"], - "scripts": ["ua-parser.js"], - "main": "ua-parser.js", + "scripts": ["src/ua-parser.js"], + "main": "src/ua-parser.js", "license": "MIT", "development": { + "jshint/jshint": "*", "visionmedia/mocha": "*", - "mishoo/UglifyJS2": "*" + "mishoo/uglifyjs2": "*" } } diff --git a/package.json b/package.json index 47db6db..906416f 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "title": "UA-Parser.JS", "name": "ua-parser-js", - "version": "0.5.15", + "version": "0.5.20", "author": "Faisal Salman (http://faisalman.com)", "description": "Lightweight JavaScript-based user-agent string parser", "keywords": [ @@ -25,6 +25,7 @@ "test": "./node_modules/mocha/bin/mocha -R spec" }, "devDependencies": { + "jshint": ">= 1.1.0", "mocha": ">= 1.7.1", "uglify-js": ">= 1.3.4" }, @@ -53,7 +54,8 @@ "node": "*" }, "directories": { - "lib": "." + "lib": "src", + "test": "test" }, "files": [ "" diff --git a/readme.md b/readme.md index 4b179cd..5525858 100644 --- a/readme.md +++ b/readme.md @@ -1,13 +1,12 @@ -# UA-Parser.JS +# UAParser.js Lightweight JavaScript-based User-Agent string parser [![Build Status](https://travis-ci.org/faisalman/ua-parser-js.png)](https://travis-ci.org/faisalman/ua-parser-js) * Author : Faisalman <> -* Home : http://faisalman.github.com/ua-parser-js +* Demo : http://faisalman.github.com/ua-parser-js * Source : https://github.com/faisalman/ua-parser-js -* License : GPLv2 & MIT ## Features @@ -18,21 +17,28 @@ Extract detailed type of web browser, layout engine, operating system, and devic ## Methods * `getBrowser()` + * returns `{ name: '', major: '', version: '' }` * `getDevice()` + * returns `{ model: '', type: '', vendor: '' }` * `getEngine()` + * returns `{ name: '', version: '' }` * `getOS()` + * returns `{ name: '', version: '' }` * `getResult()` + * returns `{ browser: {}, device: {}, engine: {}, os: {} }` * `getUA()` + * returns UA string of current instance * `setUA(uastring)` + * set & parse UA string ## Example ```html - + ``` ## Using requirejs -If you're using requirejs, you can load UA-Parser like any other module. - ```js require(['ua-parser'], function(UAParser) { var parser = new UAParser(); @@ -93,18 +109,32 @@ $ npm install ua-parser-js ``` ```js -var UAParser = require('ua-parser'); -var parser = new UAParser(); -var uaString = 'Mozilla/5.0 (PlayBook; U; RIM Tablet OS 1.0.0; en-US) AppleWebKit/534.11 (KHTML, like Gecko) Version/7.1.0.7 Safari/534.11'; +var UAParser = require('ua-parser'); +var parser = new UAParser(); +console.log(parser.getResult()); +``` -console.log(parser.setUA(uaString).getDevice().model); // "PlayBook" -console.log(parser.getOS()) // {name: "RIM Tablet OS", version: "1.0.0"} -console.log(parser.getEngine().name); // "WebKit" +## Using component + +```sh +$ component install faisalman/ua-parser-js +``` + +```js +var UAParser = require('ua-parser-js'); +var parser = new UAParser(); +console.log(parser.getResult()); +``` + +## Using bower + +```sh +$ bower install ua-parser-js ``` ## Using jQuery -If you're using jQuery, `$.ua` object will be created automatically based on container's user-agent. Use `$.ua.get()` / `$.ua.set(uastring)` to get/set user-agent. In case you need, `UAParser` constructor is still present in global though. +If you're using jQuery, `$.ua` object will be created automatically based on browser's user-agent. In case you need, `UAParser` constructor is still present in global though. Getter / setter: `$.ua.get()` / `$.ua.set(uastring)`. ```js // In browser with default user-agent: 'Mozilla/5.0 (Linux; U; Android 2.3.4; en-us; Sprint APA7373KT Build/GRJ22) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0': @@ -125,8 +155,26 @@ console.log($.ua.browser.version); // "4.0" console.log(parseInt($.ua.browser.version.split('.')[0], 10)); // 4 ``` +## Development + +Install dependencies + +```sh +$ npm install jshint +$ npm install mocha +$ npm install uglify-js +``` + +Verify, test, & minify script + +```sh +$ ./build/build.sh +``` + ## License +Dual licensed under GPLv2 & MIT + Copyright © 2012-2013 Faisalman <> Permission is hereby granted, free of charge, to any person obtaining a copy of diff --git a/src/ua-parser.js b/src/ua-parser.js index 92c508a..c36cd54 100644 --- a/src/ua-parser.js +++ b/src/ua-parser.js @@ -1,14 +1,19 @@ -// UA-Parser.JS v0.5.15 +// UAParser.js v0.5.20 // Lightweight JavaScript-based User-Agent string parser // https://github.com/faisalman/ua-parser-js // // Copyright © 2012-2013 Faisalman // Dual licensed under GPLv2 & MIT -(function (global, undefined) { +(function (window, undefined) { 'use strict'; + ////////////// + // Constants + ///////////// + + var EMPTY = '', FUNC = 'function', UNDEF = 'undefined', @@ -23,6 +28,12 @@ MOBILE = 'mobile', TABLET = 'tablet'; + + /////////////// + // Map helper + ////////////// + + var mapper = { regex : function () { @@ -96,6 +107,12 @@ } }; + + /////////////// + // String map + ////////////// + + var maps = { browser : { @@ -147,6 +164,12 @@ } }; + + ////////////// + // Regex map + ///////////// + + var regexes = { browser : [[ @@ -155,7 +178,7 @@ /(opera\smini)\/((\d+)?[\w\.-]+)/i, // Opera Mini /(opera\s[mobiletab]+).+version\/((\d+)?[\w\.-]+)/i, // Opera Mobi/Tablet /(opera).+version\/((\d+)?[\w\.]+)/i, // Opera > 9.80 - /(opera)[\/\s]+((\d+)?[\w\.]+)/i, // Opera < 9.80 + /(opera)[\/\s]+((\d+)?[\w\.]+)/i // Opera < 9.80 ], [NAME, VERSION, MAJOR], [ @@ -374,26 +397,31 @@ ] }; + + ///////////////// + // Constructor + //////////////// + + var UAParser = function UAParser (uastring) { - var ua = uastring || ((global && global.navigator && global.navigator.userAgent) ? global.navigator.userAgent : EMPTY); + var ua = uastring || ((window && window.navigator && window.navigator.userAgent) ? window.navigator.userAgent : EMPTY); + if (!(this instanceof UAParser)) { + return new UAParser(uastring).getResult(); + } this.getBrowser = function () { return mapper.regex.apply(this, regexes.browser); }; - this.getDevice = function () { return mapper.regex.apply(this, regexes.device); }; - this.getEngine = function () { return mapper.regex.apply(this, regexes.engine); }; - this.getOS = function () { return mapper.regex.apply(this, regexes.os); }; - this.getResult = function() { return { browser : this.getBrowser(), @@ -402,20 +430,23 @@ device : this.getDevice() }; }; - this.getUA = function() { return ua; }; - this.setUA = function (uastring) { ua = uastring; return this; }; - this.setUA(ua); }; - // check js environment + + /////////// + // Export + ////////// + + + // check js environment if (typeof exports !== UNDEF && !/\[object\s[DOM]*Window\]/.test(global.toString())) { // nodejs env if (typeof module !== UNDEF && module.exports) { @@ -429,21 +460,22 @@ }); } else { // browser env - global.UAParser = UAParser; + window.UAParser = UAParser; // jQuery specific - if (typeof global.jQuery !== UNDEF) { + if (typeof window.jQuery !== UNDEF) { var parser = new UAParser(); - global.jQuery.ua = parser.getResult(); - global.jQuery.ua.get = function() { + window.jQuery.ua = parser.getResult(); + window.jQuery.ua.get = function() { return parser.getUA(); }; - global.jQuery.ua.set = function(uastring) { + window.jQuery.ua.set = function(uastring) { parser.setUA(uastring); var result = parser.getResult(); for (var prop in result) { - global.jQuery.ua[prop] = result[prop]; + window.jQuery.ua[prop] = result[prop]; } }; } } + })(this); diff --git a/src/ua-parser.min.js b/src/ua-parser.min.js new file mode 100644 index 0000000..a166f5a --- /dev/null +++ b/src/ua-parser.min.js @@ -0,0 +1,7 @@ +// UAParser.js v0.5.20 +// Lightweight JavaScript-based User-Agent string parser +// https://github.com/faisalman/ua-parser-js +// +// Copyright © 2012-2013 Faisalman +// Dual licensed under GPLv2 & MIT +(function(e,t){"use strict";var n="",r="function",i="undefined",s="object",o="major",u="model",a="name",f="type",l="vendor",c="version",h="console",p="mobile",d="tablet",v={regex:function(){var e,o,u,a,f,l,c=arguments;for(o=0;o0){for(var o=0;o