Add platform parser

pull/227/head
Denis Demchenko 7 years ago
parent 38320b1409
commit a65540dc15

@ -0,0 +1,188 @@
import {
getFirstMatch
} from './utils';
const TYPES = {
tablet: 'tablet',
mobile: 'mobile',
desktop: 'desktop'
};
export default [
/* Nexus Tablet */
{
test: [/nexus\s*[0-9]+/i],
describe() {
return {
type: TYPES.tablet,
vendor: 'Nexus',
};
}
},
/* iPad */
{
test: [/ipad/i],
describe() {
return {
type: TYPES.tablet,
vendor: 'Apple',
model: 'iPad'
}
}
},
/* Silk Tablet */
{
test: [/silk/i],
describe() {
return {
type: TYPES.tablet
}
}
},
/* Tablet */
{
test: [/tablet/i],
describe() {
return {
type: TYPES.tablet
};
}
},
/* iPod/iPhone */
{
test: [/ipod|iphone/i],
describe(ua) {
const model = getFirstMatch(/(ipod|iphone)/i, ua);
return {
type: TYPES.mobile,
vendor: 'Apple',
model: model
};
}
},
/* Nexus Mobile */
{
test: [/nexus\s*[0-6]\s*/i],
describe() {
return {
type: TYPES.mobile,
vendor: 'Nexus'
};
}
},
/* Mobile */
{
test: [/[^-]mobi/i],
describe() {
return {
type: TYPES.mobile
};
}
},
/* BlackBerry */
{
test(parser) {
return parser.getBrowserName(true) === 'blackberry';
},
describe() {
return {
type: TYPES.mobile,
vendor: 'BlackBerry'
};
}
},
/* Bada */
{
test(parser) {
return parser.getBrowserName(true) === 'bada';
},
describe() {
return {
type: TYPES.mobile
}
}
},
/* Windows Phone */
{
test(parser) {
return parser.getBrowserName() === 'windows phone';
},
describe() {
return {
type: TYPES.mobile,
vendor: 'Microsoft'
}
}
},
/* Android Tablet */
{
test(parser) {
const osMajorVersion = Number(String(parser.getOSVersion()).split('.')[0]);
return parser.getOSName(true) === 'android' && (osMajorVersion >= 3);
},
describe() {
return {
type: TYPES.tablet
}
}
},
/* Android Mobile */
{
test(parser) {
return parser.getOSName(true) === 'android';
},
describe() {
return {
type: TYPES.mobile
}
}
},
/* desktop */
{
test(parser) {
return parser.getOSName(true) === 'macos';
},
describe() {
return {
type: TYPES.desktop,
vendor: 'Apple'
};
}
},
/* Windows */
{
test(parser) {
return parser.getOSName(true) === 'windows';
},
describe() {
return {
type: TYPES.desktop
}
}
},
/* Linux */
{
test(parser) {
return parser.getOSName(true) === 'linux';
},
describe() {
return {
type: TYPES.desktop
}
}
}
];

@ -1,5 +1,6 @@
import browserParsersList from './parser-browsers';
import osParsersList from './parser-os';
import platformParsersList from './parser-platforms';
class Parser {
/**
@ -101,8 +102,6 @@ class Parser {
return this.getBrowser().version;
}
getPlatform(){}
/**
* Get OS
* @return {Object}
@ -153,10 +152,17 @@ class Parser {
/**
* Get OS name
* @param {Boolean} [toLowerCase] return lower-cased value
* @return {String} name of the OS macOS, Windows, Linux, etc.
*/
getOSName() {
return this.getOS().name;
getOSName(toLowerCase) {
const name = this.getOS().name;
if (toLowerCase) {
return String(name).toLowerCase();
}
return name;
}
/**
@ -167,13 +173,50 @@ class Parser {
return this.getOS().version;
}
getPlatform() {
if (this.parsedResult.platform) {
return this.parsedResult.platform;
}
return this._parsePlatform();
}
_parsePlatform() {
this.parsedResult.platform = {};
const platform = platformParsersList.find(_platform => {
if (typeof _platform.test === 'function') {
return _platform.test(this);
}
if (_platform.test instanceof Array) {
return _platform.test.some((condition) => {
return this.test(condition);
});
}
throw new Error("Browser's test function is not valid");
});
if (platform) {
this.parsedResult.platform = platform.describe(this.getUA());
}
return this.parsedResult.platform;
}
/**
* Parse full information about the browser
*/
parse(){
parse() {
this._parseBrowser();
this._parseOS();
this._parsePlatform();
return this;
}
getResult() {
return this.parsedResult;
}
}

Loading…
Cancel
Save