Comparing version 1.0.3 to 2.0.0
16
index.js
@@ -1,2 +0,8 @@ | ||
/** | ||
* @function ua | ||
* Returns an object representing the user agent including data such as browser, device and platform | ||
* @param {string} _ua - the raw user agent string to be converted | ||
* @param {string} obj - (optional) object to be merged to the output result | ||
* @returns {object} object representing your user agent | ||
*/ | ||
module.exports = exports = function (_ua, obj) { | ||
@@ -93,6 +99,10 @@ if (typeof _ua === 'string') { | ||
/** | ||
* wrapped webview native app detection | ||
*/ | ||
test.call(obj, _ua, 'webview', | ||
[ true, false ], | ||
[ 'vigour-wrapper', true ], | ||
[ 'crosswalk', true ] | ||
[ 'vigour-wrapper', 'vigour' ], | ||
[ 'crosswalk', 'crosswalk' ], | ||
[ 'cordova', 'cordova' ] | ||
) | ||
@@ -99,0 +109,0 @@ |
{ | ||
"name": "vigour-ua", | ||
"version": "1.0.3", | ||
"version": "2.0.0", | ||
"description": "lightweight ua parser", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "gaston test" | ||
"test": "NODE_ENV=test node test/index.js | tap-spec", | ||
"cover": "istanbul cover --report none --print detail test/index.js", | ||
"view-cover": "istanbul report html && open ./coverage/index.html", | ||
"travis": "npm run cover -s && istanbul report lcov && ((cat coverage/lcov.info | coveralls) || exit 0)" | ||
}, | ||
@@ -35,3 +38,9 @@ "repository": { | ||
"dependencies": {}, | ||
"engines": {} | ||
"engines": {}, | ||
"devDependencies": { | ||
"coveralls": "^2.11.9", | ||
"istanbul": "^0.4.2", | ||
"tap-spec": "^4.1.1", | ||
"tape": "^4.5.1" | ||
} | ||
} |
# ua | ||
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg)](http://standardjs.com/) | ||
<!-- VDOC.badges travis; npm; coveralls --> | ||
<!-- DON'T EDIT THIS SECTION (including comments), INSTEAD RE-RUN `vdoc` TO UPDATE --> | ||
[![Build Status](https://travis-ci.org/vigour-io/ua.svg?branch=master)](https://travis-ci.org/vigour-io/ua) | ||
[![npm version](https://badge.fury.io/js/vigour-ua.svg)](https://badge.fury.io/js/vigour-ua) | ||
[![Build Status](https://travis-ci.org/vigour-io/ua.svg?branch=master)](https://travis-ci.org/vigour-io/ua) | ||
[![Coverage Status](https://coveralls.io/repos/github/vigour-io/ua/badge.svg?branch=master)](https://coveralls.io/github/vigour-io/ua?branch=master) | ||
<!-- VDOC END --> | ||
ua parser with a light footprint (~1kb) | ||
@@ -7,0 +11,0 @@ |
'use strict' | ||
describe('devices', function () { | ||
var ua = require('../../') | ||
var useragents = require('./useragents/list') | ||
it('samsung smart-tv', function () { | ||
check({ | ||
list: useragents.samsungSmartTv, | ||
platform: 'samsung', | ||
device: 'tv' | ||
}) | ||
}) | ||
var test = require('tape') | ||
var ua = require('../../') | ||
var useragents = require('./useragents/list') | ||
it('lg smart-tv', function () { | ||
check({ | ||
list: useragents.lgSmartTv, | ||
platform: 'lg', | ||
device: 'tv' | ||
}) | ||
}) | ||
test('devices - samsung smart-tv', function (t) { | ||
t.plan(useragents.samsungSmartTv.length * 2) | ||
check({ | ||
list: useragents.samsungSmartTv, | ||
platform: 'samsung', | ||
device: 'tv' | ||
}, t) | ||
}) | ||
it('chromeCast', function () { | ||
check({ | ||
list: useragents.chromeCast, | ||
platform: 'chromecast', | ||
device: 'chromecast' | ||
}) | ||
}) | ||
test('devices - lg smart-tv', function (t) { | ||
t.plan(useragents.lgSmartTv.length * 2) | ||
check({ | ||
list: useragents.lgSmartTv, | ||
platform: 'lg', | ||
device: 'tv' | ||
}, t) | ||
}) | ||
it('iPhone', function () { | ||
check({ | ||
list: useragents.iPhone, | ||
platform: 'ios', | ||
device: 'phone' | ||
}) | ||
}) | ||
test('devices - chromeCast', function (t) { | ||
t.plan(useragents.chromeCast.length * 2) | ||
check({ | ||
list: useragents.chromeCast, | ||
platform: 'chromecast', | ||
device: 'chromecast' | ||
}, t) | ||
}) | ||
it('iPad', function () { | ||
check({ | ||
list: useragents.iPad, | ||
platform: 'ios', | ||
device: 'tablet' | ||
}) | ||
}) | ||
test('devices - iPhone', function (t) { | ||
t.plan(useragents.iPhone.length * 2) | ||
check({ | ||
list: useragents.iPhone, | ||
platform: 'ios', | ||
device: 'phone' | ||
}, t) | ||
}) | ||
it('android phone', function () { | ||
check({ | ||
list: useragents.androidPhone, | ||
// browser | ||
platform: 'android', | ||
device: 'phone' | ||
}) | ||
}) | ||
test('devices - iPad', function (t) { | ||
t.plan(useragents.iPad.length * 2) | ||
check({ | ||
list: useragents.iPad, | ||
platform: 'ios', | ||
device: 'tablet' | ||
}, t) | ||
}) | ||
it('android tablet', function () { | ||
check({ | ||
list: useragents.androidTablet, | ||
platform: 'android', | ||
device: 'tablet' | ||
}) | ||
}) | ||
test('devices - android phone', function (t) { | ||
t.plan(useragents.androidPhone.length * 2) | ||
check({ | ||
list: useragents.androidPhone, | ||
platform: 'android', | ||
device: 'phone' | ||
}, t) | ||
}) | ||
it('stick tv', function () { | ||
check({ | ||
list: useragents.sticktv, | ||
platform: 'android', | ||
device: 'sticktv' | ||
}) | ||
}) | ||
test('devices - android tablet', function (t) { | ||
t.plan(useragents.androidTablet.length * 2) | ||
check({ | ||
list: useragents.androidTablet, | ||
platform: 'android', | ||
device: 'tablet' | ||
}, t) | ||
}) | ||
it('fire tv', function () { | ||
check({ | ||
list: useragents.firetv, | ||
platform: 'android', | ||
device: 'firetv' | ||
}) | ||
}) | ||
test('devices - stick tv', function (t) { | ||
t.plan(useragents.sticktv.length * 2) | ||
check({ | ||
list: useragents.sticktv, | ||
platform: 'android', | ||
device: 'sticktv' | ||
}, t) | ||
}) | ||
it('riks tv', function () { | ||
check({ | ||
list: useragents.rikstv, | ||
platform: 'rikstv', | ||
device: 'rikstv' | ||
}) | ||
}) | ||
test('devices - fire tv', function (t) { | ||
t.plan(useragents.firetv.length * 2) | ||
check({ | ||
list: useragents.firetv, | ||
platform: 'android', | ||
device: 'firetv' | ||
}, t) | ||
}) | ||
function check (params) { | ||
var result | ||
for (var i in params.list) { | ||
result = ua(params.list[i]) | ||
expect(result.device).to.equal(params.device) | ||
expect(result.platform).to.equal(params.platform) | ||
} | ||
test('devices - riks tv', function (t) { | ||
t.plan(useragents.rikstv.length * 2) | ||
check({ | ||
list: useragents.rikstv, | ||
platform: 'rikstv', | ||
device: 'rikstv' | ||
}, t) | ||
}) | ||
function check (params, t) { | ||
var result | ||
for (var i in params.list) { | ||
result = ua(params.list[i]) | ||
t.equals(result.device, params.device, 'equals device') | ||
t.equals(result.platform, params.platform, 'equals platform') | ||
} | ||
}) | ||
} |
@@ -1,75 +0,89 @@ | ||
describe('methods', function () { | ||
var ua = require('../../') | ||
var userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.3' | ||
var defaultUA = { | ||
browser: true, | ||
version: 0, | ||
'use strict' | ||
var test = require('tape') | ||
var ua = require('../../') | ||
var userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.3' | ||
var defaultUA = { | ||
browser: true, | ||
version: 0, | ||
prefix: 'webkit', | ||
platform: 'windows', | ||
device: 'desktop', | ||
webview: false | ||
} | ||
test('it returns an object', function (t) { | ||
t.plan(1) | ||
t.deepEquals(ua(userAgent), { | ||
browser: 'chrome', | ||
version: 46, | ||
prefix: 'webkit', | ||
platform: 'windows', | ||
platform: 'mac', | ||
device: 'desktop', | ||
webview: false | ||
} | ||
it('it returns an object', function () { | ||
expect(ua(userAgent)).to.deep.equal({ | ||
browser: 'chrome', | ||
version: 46, | ||
prefix: 'webkit', | ||
platform: 'mac', | ||
device: 'desktop', | ||
webview: false | ||
}) | ||
}) | ||
}) | ||
it('it can merge into an object', function () { | ||
var obj = { | ||
field: true | ||
} | ||
ua(userAgent, obj) | ||
expect(obj).to.deep.equal({ | ||
browser: 'chrome', | ||
version: 46, | ||
prefix: 'webkit', | ||
platform: 'mac', | ||
device: 'desktop', | ||
field: true, | ||
webview: false | ||
}) | ||
test('it can merge into an object', function (t) { | ||
t.plan(1) | ||
var obj = { | ||
field: true | ||
} | ||
ua(userAgent, obj) | ||
t.deepEquals(obj, { | ||
browser: 'chrome', | ||
version: 46, | ||
prefix: 'webkit', | ||
platform: 'mac', | ||
device: 'desktop', | ||
field: true, | ||
webview: false | ||
}) | ||
}) | ||
it('it can merge into itself', function () { | ||
ua(userAgent, true) | ||
expect(ua.browser).to.equal('chrome') | ||
expect(ua.version).to.equal(46) | ||
expect(ua.prefix).to.equal('webkit') | ||
expect(ua.platform).to.equal('mac') | ||
expect(ua.device).to.equal('desktop') | ||
}) | ||
test('it can merge into itself', function (t) { | ||
t.plan(5) | ||
ua(userAgent, true) | ||
t.equals(ua.browser, 'chrome') | ||
t.equals(ua.version, 46) | ||
t.equals(ua.prefix, 'webkit') | ||
t.equals(ua.platform, 'mac') | ||
t.equals(ua.device, 'desktop') | ||
}) | ||
it('should work even it _ua === undefined', function () { | ||
expect(ua()).to.deep.equal(defaultUA) | ||
}) | ||
test('should work even it _ua === undefined', function (t) { | ||
t.plan(1) | ||
t.deepEquals(ua(), defaultUA) | ||
}) | ||
it('should work even it _ua === {}', function () { | ||
expect(ua({})).to.deep.equal(defaultUA) | ||
}) | ||
test('should work even it _ua === {}', function (t) { | ||
t.plan(1) | ||
t.deepEquals(ua({}), defaultUA) | ||
}) | ||
it('should work even it _ua is a number', function () { | ||
expect(ua(42)).to.deep.equal(defaultUA) | ||
}) | ||
test('should work even it _ua is a number', function (t) { | ||
t.plan(1) | ||
t.deepEquals(ua(42), defaultUA) | ||
}) | ||
it('should work even it _ua is a function', function () { | ||
expect(ua(expect)).to.deep.equal(defaultUA) | ||
}) | ||
test('should work even it _ua is a function', function (t) { | ||
t.plan(1) | ||
var testFunction = function () { | ||
return 1 + 1 | ||
} | ||
t.deepEquals(ua(testFunction), defaultUA) | ||
}) | ||
it('should work even it _ua is an array', function () { | ||
expect(ua([])).to.deep.equal(defaultUA) | ||
}) | ||
test('should work even it _ua is an array', function (t) { | ||
t.plan(1) | ||
t.deepEquals(ua([]), defaultUA) | ||
}) | ||
it('should work even it _ua === true', function () { | ||
expect(ua(true)).to.deep.equal(defaultUA) | ||
}) | ||
test('should work even it _ua === true', function (t) { | ||
t.plan(1) | ||
t.deepEquals(ua(true), defaultUA) | ||
}) | ||
it('should work even it _ua === false', function () { | ||
expect(ua(false)).to.deep.equal(defaultUA) | ||
}) | ||
test('should work even it _ua === false', function (t) { | ||
t.plan(1) | ||
t.deepEquals(ua(false), defaultUA) | ||
}) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
19625
12
370
59
4