Compare commits

..

11 Commits

Author SHA1 Message Date
Faisal Salman
dc20c12b63 Add CPU test & increment minor version 2013-04-04 13:46:09 +07:00
Faisal Salman
ecf8f7a849 Merge branch 'dev-cpu' of github.com:faisalman/ua-parser-js 2013-04-04 13:10:59 +07:00
Faisal Salman
3bf60cc830 Merge branch 'master' of github.com:faisalman/ua-parser-js 2013-04-04 13:05:46 +07:00
Faisal Salman
8c94018351 Update readme.md modify node example 2013-04-04 13:05:21 +07:00
Faisal Salman
9098ad501f Update readme.md regarding CPU detection 2013-04-03 21:03:28 +07:00
Faisal Salman
376541a383 Add new getCPU() method to detect CPU architecture 2013-04-03 18:00:27 +07:00
Faisal Salman
9cad6a0f2d Detect new IE 11 user-agent 2013-03-26 13:35:10 +07:00
Faisal Salman
84a8eedad4 Exclude unintended char from being captured 2013-03-26 11:10:06 +07:00
Faisal Salman
30e1e7a98f Increment version 2013-03-26 10:29:43 +07:00
Faisal Salman
c1df106dca Fix closing braces 2013-03-26 10:27:24 +07:00
Faisal Salman
edcd5402a6 Fixes issue #15 - Attach UAParser to window 2013-03-25 23:16:40 +07:00
9 changed files with 178 additions and 32 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "ua-parser-js",
"version": "0.5.25",
"version": "0.6.0",
"description": "Lightweight JavaScript-based user-agent string parser",
"keywords": ["user-agent", "parser", "browser", "engine", "os", "device"],
"scripts": ["src/ua-parser.js"],

View File

@@ -1,7 +1,7 @@
{
"title": "UAParser.js",
"name": "ua-parser-js",
"version": "0.5.25",
"version": "0.6.0",
"author": "Faisal Salman <fyzlman@gmail.com> (http://faisalman.com)",
"description": "Lightweight JavaScript-based user-agent string parser",
"keywords": [

View File

@@ -73,8 +73,16 @@ Windows [Phone/Mobile], Zenwalk
# 'os.version' determined dynamically
```
* `getCPU()`
* returns `{ architecture: '' }`
```
# Possible 'cpu.architecture'
68k, amd64, arm, ia32, ia64, irix, mips, pa-risc, ppc, sparc
```
* `getResult()`
* returns `{ browser: {}, device: {}, engine: {}, os: {} }`
* returns `{ browser: {}, cpu: {}, device: {}, engine: {}, os: {} }`
* `getUA()`
* returns UA string of current instance
* `setUA(uastring)`
@@ -113,6 +121,9 @@ Windows [Phone/Mobile], Zenwalk
model: "",
type: "",
vendor: ""
},
cpu: {
architecture: ""
}
}
*/
@@ -130,6 +141,7 @@ Windows [Phone/Mobile], Zenwalk
console.log(result.os); // {name: "Ubuntu", version: "11.10"}
console.log(result.os.version); // "11.10"
console.log(result.engine.name); // "WebKit"
console.log(result.cpu.architecture); // "amd64"
// do some other tests
var uastring2 = "Mozilla/5.0 (compatible; Konqueror/4.1; OpenBSD) KHTML/4.1.4 (like Gecko)";
@@ -149,15 +161,6 @@ Windows [Phone/Mobile], Zenwalk
</html>
```
### Using requirejs
```js
require(['ua-parser'], function(UAParser) {
var parser = new UAParser();
console.log(parser.getResult());
});
```
### Using node.js
```sh
@@ -166,8 +169,18 @@ $ npm install ua-parser-js
```js
var UAParser = require('ua-parser-js');
var parser = new UAParser();
var ua = request.headers['user-agent']; // user-agent header from an HTTP request
console.log(parser.setUA(ua).getResult());
```
### Using requirejs
```js
require(['ua-parser'], function(UAParser) {
var parser = new UAParser();
console.log(parser.getResult());
});
```
### Using component
@@ -190,7 +203,7 @@ $ bower install ua-parser-js
### Using jQuery.ua
Although written in vanilla js (which means it doesn't depends on jQuery), if you're using jQuery, this library will automatically detect and create `$.ua` object based on browser's user-agent (although in case you need, `window.UAParser` constructor is still present). To get/set user-agent you can use: `$.ua.get()` / `$.ua.set(uastring)`.
Although written in vanilla js (which means it doesn't depends on jQuery), this library will automatically detect if jQuery is present and create `$.ua` object based on browser's user-agent (although in case you need, `window.UAParser` constructor is still present). To get/set user-agent you can use: `$.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':

View File

@@ -1,4 +1,4 @@
// UAParser.js v0.5.25
// UAParser.js v0.6.0
// Lightweight JavaScript-based User-Agent string parser
// https://github.com/faisalman/ua-parser-js
//
@@ -25,6 +25,7 @@
TYPE = 'type',
VENDOR = 'vendor',
VERSION = 'version',
ARCHITECTURE= 'architecture',
CONSOLE = 'console',
MOBILE = 'mobile',
TABLET = 'tablet';
@@ -38,6 +39,9 @@
var util = {
has : function (str1, str2) {
return str2.toLowerCase().indexOf(str1.toLowerCase()) !== -1;
},
lowerize : function (str) {
return str.toLowerCase();
}
};
@@ -78,10 +82,15 @@
match = matches[++k];
q = props[p];
// check if given property is actually array
if (typeof(q) === OBJ_TYPE) {
if (typeof(q) === OBJ_TYPE && q.length > 0) {
if (q.length == 2) {
if (typeof(q[1]) == FUNC_TYPE) {
// assign modified match
result[q[0]] = q[1].call(this, match);
} else {
// assign given value, ignore regex match
result[q[0]] = q[1];
}
} else if (q.length == 3) {
// check whether function or regex
if (typeof(q[1]) === FUNC_TYPE && !(q[1].exec && q[1].test)) {
@@ -91,6 +100,8 @@
// sanitize match using given regex
result[q[0]] = match ? match.replace(q[1], q[2]) : undefined;
}
} else if (q.length == 4) {
result[q[0]] = match ? q[3].call(this, match.replace(q[1], q[2])) : undefined;
}
} else {
result[q] = match ? match : undefined;
@@ -209,7 +220,7 @@
// Trident based
/(avant\s|iemobile|slim|baidu)(?:browser)?[\/\s]?((\d+)?[\w\.]*)/i,
// Avant/IEMobile/SlimBrowser/Baidu
/ms(ie)\s((\d+)?[\w\.]+)/i, // Internet Explorer
/(?:ms|\()(ie)\s((\d+)?[\w\.]+)/i, // Internet Explorer
// Webkit/KHTML based
/(rekonq)((?:\/)[\w\.]+)*/i, // Rekonq
@@ -266,6 +277,25 @@
], [NAME, VERSION, MAJOR]
],
cpu : [[
/(?:(amd|x(?:(?:86|64)[_-])?|wow|win)64)[;\)]/i // AMD64
], [[ARCHITECTURE, 'amd64']], [
/((?:i[346]|x)86)[;\)]/i // IA32
], [[ARCHITECTURE, 'ia32']], [
/((?:ppc|powerpc)(?:64)?)(?:\smac|;|\))/i // PowerPC
], [[ARCHITECTURE, /ower/, '', util.lowerize]], [
/(sun4\w)[;\)]/i // SPARC
], [[ARCHITECTURE, 'sparc']], [
/(ia64(?=;)|68k(?=\))|arm(?=v\d+;)|(?:irix|mips|sparc)(?:64)?(?=;)|pa-risc)/i
// IA64, 68K, ARM, IRIX, MIPS, SPARC, PA-RISC
], [ARCHITECTURE, util.lowerize]
],
device : [[
/\((ipad|playbook);[\w\s\);-]+(rim|apple)/i // iPad/PlayBook
@@ -306,7 +336,7 @@
/(htc)[;_\s-]+([\w\s]+(?=\))|\w+)*/i, // HTC
/(zte)-(\w+)*/i, // ZTE
/(alcatel|geeksphone|huawei|lenovo|nexian|panasonic|;\ssony)[_\s-]?([\w-]+)*/i
/(alcatel|geeksphone|huawei|lenovo|nexian|panasonic|(?=;\s)sony)[_\s-]?([\w-]+)*/i
// Alcatel/GeeksPhone/Huawei/Lenovo/Nexian/Panasonic/Sony
], [VENDOR, [MODEL, /_/g, ' '], [TYPE, MOBILE]], [
@@ -431,6 +461,9 @@
this.getBrowser = function () {
return mapper.rgx.apply(this, regexes.browser);
};
this.getCPU = function () {
return mapper.rgx.apply(this, regexes.cpu);
};
this.getDevice = function () {
return mapper.rgx.apply(this, regexes.device);
};
@@ -445,7 +478,8 @@
browser : this.getBrowser(),
engine : this.getEngine(),
os : this.getOS(),
device : this.getDevice()
device : this.getDevice(),
cpu : this.getCPU()
};
};
this.getUA = function () {
@@ -471,14 +505,15 @@
exports = module.exports = UAParser;
}
exports.UAParser = UAParser;
} else if (typeof(define) === FUNC_TYPE && define.amd) {
// requirejs env
define(function() {
return UAParser;
});
} else {
// browser env
window.UAParser = UAParser;
// requirejs env (optional)
if (typeof(define) === FUNC_TYPE && define.amd) {
define(function () {
return UAParser;
});
}
// jQuery specific (optional)
if (typeof(window.jQuery) !== UNDEF_TYPE) {
var $ = window.jQuery;

File diff suppressed because one or more lines are too long

View File

@@ -239,6 +239,16 @@
"major" : "7"
}
},
{
"desc" : "IE 11",
"ua" : "Mozilla/5.0 (IE 11.0; Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko",
"expect" :
{
"name" : "IE",
"version" : "11.0",
"major" : "11"
}
},
{
"desc" : "K-Meleon",
"ua" : "Mozilla/5.0 (Windows; U; Win98; en-US; rv:1.5) Gecko/20031016 K-Meleon/0.8.2",

81
test/cpu-test.json Normal file
View File

@@ -0,0 +1,81 @@
[
{
"desc" : "i686",
"ua" : "Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:19.0) Gecko/20100101 Firefox/19.0",
"expect" :
{
"architecture" : "ia32"
}
},
{
"desc" : "i386",
"ua" : "Mozilla/5.0 (X11; U; FreeBSD i386; en-US; rv:1.7) Gecko/20040628 Epiphany/1.2.6",
"expect" :
{
"architecture" : "ia32"
}
},
{
"desc" : "x86-64",
"ua" : "Opera/9.80 (X11; Linux x86_64; U; Linux Mint; en) Presto/2.2.15 Version/10.10",
"expect" :
{
"architecture" : "amd64"
}
},
{
"desc" : "win64",
"ua" : "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.2; Win64; x64; Trident/6.0; .NET4.0E; .NET4.0C)",
"expect" :
{
"architecture" : "amd64"
}
},
{
"desc" : "WOW64",
"ua" : "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)",
"expect" :
{
"architecture" : "amd64"
}
},
{
"desc" : "ARMv6",
"ua" : "Mozilla/5.0 (X11; U; Linux armv61; en-US; rv:1.9.1b2pre) Gecko/20081015 Fennec/1.0a1",
"expect" :
{
"architecture" : "arm"
}
},
{
"desc" : "Mac PowerPC",
"ua" : "Mozilla/4.0 (compatible; MSIE 4.5; Mac_PowerPC)",
"expect" :
{
"architecture" : "ppc"
}
},
{
"desc" : "Mac PowerPC",
"ua" : "Mozilla/4.0 (compatible; MSIE 5.17; Mac_PowerPC Mac OS; en)",
"expect" :
{
"architecture" : "ppc"
}
},
{
"desc" : "Mac PowerPC",
"ua" : "iCab/2.9.5 (Macintosh; U; PPC; Mac OS X)",
"expect" :
{
"architecture" : "ppc"
}
},
{
"desc" : "UltraSPARC",
"ua" : "Mozilla/5.0 (X11; U; SunOS sun4u; en-US; rv:1.9b5) Gecko/2008032620 Firefox/3.0b5",
"expect" :
{
"architecture" : "sparc"
}
}]

View File

@@ -1,6 +1,7 @@
var assert = require('assert');
var UAParser = require('./../src/ua-parser');
var browsers = require('./browser-test.json');
var cpus = require('./cpu-test.json');
var devices = require('./device-test.json');
var engines = require('./engine-test.json');
var os = require('./os-test.json');
@@ -12,6 +13,12 @@ var methods = [
list : browsers,
properties : ['name', 'major', 'version']
},
{
title : 'getCPU',
label : 'cpu',
list : cpus,
properties : ['architecture']
},
{
title : 'getDevice',
label : 'device',

View File

@@ -1,7 +1,7 @@
{
"title": "UAParser.js",
"name": "ua-parser-js",
"version": "0.5.25",
"version": "0.6.0",
"description": "Lightweight JavaScript-based user-agent string parser",
"keywords": [
"user-agent",