Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,18 @@ a semver compatible format) using a navigator useragent in a browser or

[![stable](https://img.shields.io/badge/stability-stable-green.svg)](https://github.com/dominictarr/stability#stable) [![Build Status](https://api.travis-ci.org/DamonOehlman/detect-browser.svg?branch=master)](https://travis-ci.org/DamonOehlman/detect-browser) [![bitHound Score](https://www.bithound.io/github/DamonOehlman/detect-browser/badges/score.svg)](https://www.bithound.io/github/DamonOehlman/detect-browser)

## NOTE: Version 2.x release

Release 2.0 introduces a breaking API change (hence the major release)
which requires invocation of a `detect` function rather than just inclusion of
the module. PR [#46](https://github.com/DamonOehlman/detect-browser/pull/46)
provides more context as to why this change has been made.

## Example Usage

```js
const browser = require('detect-browser');
const { detect } = require('detect-browser');
const browser = detect();

// handle the case where we don't detect the browser
if (browser) {
Expand All @@ -26,7 +34,8 @@ if (browser) {
Or you can use a switch statement:

```js
const browser = require('detect-browser');
const { detect } = require('detect-browser');
const browser = detect();

// handle the case where we don't detect the browser
switch (browser && browser.name) {
Expand Down
9 changes: 0 additions & 9 deletions browser.js

This file was deleted.

3 changes: 2 additions & 1 deletion examples/simple.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const browser = require('..');
const { detect } = require('..');
const browser = detect();

// handle the case where we don't detect the browser
if (browser) {
Expand Down
3 changes: 2 additions & 1 deletion examples/switch.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const browser = require('..');
const { detect } = require('..');
const browser = detect();

// handle the case where we don't detect the browser
switch (browser && browser.name) {
Expand Down
133 changes: 131 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
a semver compatible format) using a navigator useragent in a browser or
`process.version` in node.

## NOTE: Version 2.x release

Release 2.0 introduces a breaking API change (hence the major release)
which requires invocation of a `detect` function rather than just inclusion of
the module. PR [#46](https://github.com/DamonOehlman/detect-browser/pull/46)
provides more context as to why this change has been made.

## Example Usage

<<< examples/simple.js
Expand Down Expand Up @@ -38,5 +45,127 @@

**/

exports.name = 'node';
exports.version = process.version.slice(1);
function detect() {
var nodeVersion = getNodeVersion();
if (nodeVersion) {
return nodeVersion;
} else if (typeof navigator !== 'undefined') {
return parseUserAgent(navigator.userAgent);
}

return null;
}

function detectOS(userAgentString) {
var rules = getOperatingSystemRules();
var detected = rules.filter(function (os) {
return os.rule && os.rule.test(userAgentString);
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slightly shorter form of what you had previously, but pretty much the same.

})[0];

return detected ? detected.name : null;
}

function getNodeVersion() {
var isNode = typeof navigator === 'undefined' && typeof process !== 'undefined';
return isNode ? {
name: 'node',
version: process.version.slice(1),
os: require('os').type().toLowerCase()
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the sake of consistency, I return something useful for node.

} : null;
}

function parseUserAgent(userAgentString) {
var browsers = getBrowserRules();
if (!userAgentString) {
return null;
}

var detected = browsers.map(function(browser) {
var match = browser.rule.exec(userAgentString);
var version = match && match[1].split(/[._]/).slice(0,3);

if (version && version.length < 3) {
version = version.concat(version.length == 1 ? [0, 0] : [0]);
}

return match && {
name: browser.name,
version: version.join('.')
};
}).filter(Boolean)[0] || null;

if (detected) {
detected.os = detectOS(userAgentString);
}

return detected;
}

function getBrowserRules() {
Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A function for both getBrowserRules and getOperatingSystemRules mainly because I can see this translating well to a static method on a class at some point in the future (when we can have all the nice ES6 things).

return buildRules([
[ 'edge', /Edge\/([0-9\._]+)/ ],
[ 'yandexbrowser', /YaBrowser\/([0-9\._]+)/ ],
[ 'vivaldi', /Vivaldi\/([0-9\.]+)/ ],
[ 'kakaotalk', /KAKAOTALK\s([0-9\.]+)/ ],
[ 'chrome', /(?!Chrom.*OPR)Chrom(?:e|ium)\/([0-9\.]+)(:?\s|$)/ ],
[ 'phantomjs', /PhantomJS\/([0-9\.]+)(:?\s|$)/ ],
[ 'crios', /CriOS\/([0-9\.]+)(:?\s|$)/ ],
[ 'firefox', /Firefox\/([0-9\.]+)(?:\s|$)/ ],
[ 'fxios', /FxiOS\/([0-9\.]+)/ ],
[ 'opera', /Opera\/([0-9\.]+)(?:\s|$)/ ],
[ 'opera', /OPR\/([0-9\.]+)(:?\s|$)$/ ],
[ 'ie', /Trident\/7\.0.*rv\:([0-9\.]+).*\).*Gecko$/ ],
[ 'ie', /MSIE\s([0-9\.]+);.*Trident\/[4-7].0/ ],
[ 'ie', /MSIE\s(7\.0)/ ],
[ 'bb10', /BB10;\sTouch.*Version\/([0-9\.]+)/ ],
[ 'android', /Android\s([0-9\.]+)/ ],
[ 'ios', /Version\/([0-9\._]+).*Mobile.*Safari.*/ ],
[ 'safari', /Version\/([0-9\._]+).*Safari/ ]
]);
}

function getOperatingSystemRules() {
return buildRules([
[ 'iOS', /iP(hone|od|ad)/ ],
[ 'Android OS', /Android/ ],
[ 'BlackBerry OS', /BlackBerry|BB10/ ],
[ 'Windows Mobile', /IEMobile/ ],
[ 'Amazon OS', /Kindle/ ],
[ 'Windows 3.11', /Win16/ ],
[ 'Windows 95', /(Windows 95)|(Win95)|(Windows_95)/ ],
[ 'Windows 98', /(Windows 98)|(Win98)/ ],
[ 'Windows 2000', /(Windows NT 5.0)|(Windows 2000)/ ],
[ 'Windows XP', /(Windows NT 5.1)|(Windows XP)/ ],
[ 'Windows Server 2003', /(Windows NT 5.2)/ ],
[ 'Windows Vista', /(Windows NT 6.0)/ ],
[ 'Windows 7', /(Windows NT 6.1)/ ],
[ 'Windows 8', /(Windows NT 6.2)/ ],
[ 'Windows 8.1', /(Windows NT 6.3)/ ],
[ 'Windows 10', /(Windows NT 10.0)/ ],
[ 'Windows ME', /Windows ME/ ],
[ 'Open BSD', /OpenBSD/ ],
[ 'Sun OS', /SunOS/ ],
[ 'Linux', /(Linux)|(X11)/ ],
[ 'Mac OS', /(Mac_PowerPC)|(Macintosh)/ ],
[ 'QNX', /QNX/ ],
[ 'BeOS', /BeOS/ ],
[ 'OS/2', /OS\/2/ ],
[ 'Search Bot', /(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves\/Teoma)|(ia_archiver)/ ]
]);
}

function buildRules(ruleTuples) {
return ruleTuples.map(function(tuple) {
return {
name: tuple[0],
rule: tuple[1]
};
});
}

module.exports = {
detect: detect,
detectOS: detectOS,
getNodeVersion: getNodeVersion,
parseUserAgent: parseUserAgent
};
43 changes: 0 additions & 43 deletions lib/detectBrowser.js

This file was deleted.

112 changes: 0 additions & 112 deletions lib/detectOS.js

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"version": "1.12.0",
"description": "Unpack a browser type and version from the useragent string",
"main": "index.js",
"browser": "browser.js",
"scripts": {
"test": "node test",
"prepublish": "npm run test",
Expand All @@ -30,6 +29,7 @@
},
"homepage": "https://github.com/DamonOehlman/detect-browser",
"devDependencies": {
"gendocs": "^2.0.0",
"semver": "^5.0.3",
"tape": "^4.2.2"
}
Expand Down
7 changes: 5 additions & 2 deletions test/highLevel.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
var semver = require('semver');
var browser = require('../');
var { detect } = require('../');
var test = require('tape');

test('can run detection', function(t) {
t.ok(browser, require.cache[require.resolve('../')]);
t.ok(detect(), 'detection ran ok');
t.end();
});

test('name detected', function(t) {
const browser = detect();
t.ok(browser.name, browser.name);
t.end();
});

test('version detected', function(t) {
const browser = detect();
t.ok(browser.version, browser.version);
t.end();
});

test('version is semver compatible', function(t) {
const browser = detect();
t.ok(semver.valid(browser.version));
t.end();
});
Loading