1
0
mirror of https://github.com/lancedikson/bowser synced 2026-03-02 03:40:27 +00:00
Files
lancedikson_bowser/src/bowser.js

94 lines
2.4 KiB
JavaScript
Raw Normal View History

2016-05-06 21:29:22 +03:00
/*!
* Bowser - a browser detector
* https://github.com/bowser-js/bowser
2018-06-27 22:52:43 +03:00
* MIT License | (c) Dustin Diaz 2012-2015
* MIT License | (c) Denis Demchenko 2015-2026
2016-05-06 21:29:22 +03:00
*/
import Parser from './parser.js';
2019-12-20 08:50:05 +11:00
import {
BROWSER_MAP,
ENGINE_MAP,
OS_MAP,
PLATFORMS_MAP,
} from './constants.js';
2016-05-06 21:29:22 +03:00
2018-06-27 22:52:43 +03:00
/**
2018-07-05 22:44:43 +03:00
* Bowser class.
* Keep it simple as much as it can be.
* It's supposed to work with collections of {@link Parser} instances
2018-07-04 22:36:46 +03:00
* rather then solve one-instance problems.
* All the one-instance stuff is located in Parser class.
*
* @class
* @classdesc Bowser is a static object, that provides an API to the Parsers
* @hideconstructor
2018-06-27 22:52:43 +03:00
*/
class Bowser {
/**
* Creates a {@link Parser} instance
2018-07-04 22:36:46 +03:00
*
2018-07-05 22:44:43 +03:00
* @param {String} UA UserAgent string
* @param {Boolean|Object} [skipParsingOrHints=false] Either a boolean to skip parsing,
* or a ClientHints object (navigator.userAgentData)
* @param {Object} [clientHints] User-Agent Client Hints data (navigator.userAgentData)
2018-07-04 22:36:46 +03:00
* @returns {Parser}
* @throws {Error} when UA is not a String
2016-06-16 13:41:21 +03:00
*
* @example
* const parser = Bowser.getParser(window.navigator.userAgent);
* const result = parser.getResult();
*
* @example
* // With User-Agent Client Hints
* const parser = Bowser.getParser(
* window.navigator.userAgent,
* window.navigator.userAgentData
* );
*/
static getParser(UA, skipParsingOrHints = false, clientHints = null) {
2018-06-30 19:21:09 +03:00
if (typeof UA !== 'string') {
throw new Error('UserAgent should be a string');
}
return new Parser(UA, skipParsingOrHints, clientHints);
}
2018-07-05 22:44:43 +03:00
/**
* Creates a {@link Parser} instance and runs {@link Parser.getResult} immediately
*
* @param {String} UA UserAgent string
* @param {Object} [clientHints] User-Agent Client Hints data (navigator.userAgentData)
2018-07-05 22:44:43 +03:00
* @return {ParsedResult}
*
* @example
* const result = Bowser.parse(window.navigator.userAgent);
*
* @example
* // With User-Agent Client Hints
* const result = Bowser.parse(
* window.navigator.userAgent,
* window.navigator.userAgentData
* );
2018-07-05 22:44:43 +03:00
*/
static parse(UA, clientHints = null) {
return (new Parser(UA, clientHints)).getResult();
}
2011-04-27 15:14:35 -07:00
static get BROWSER_MAP() {
return BROWSER_MAP;
}
static get ENGINE_MAP() {
return ENGINE_MAP;
}
static get OS_MAP() {
return OS_MAP;
}
static get PLATFORMS_MAP() {
return PLATFORMS_MAP;
}
}
2018-06-27 22:52:43 +03:00
export default Bowser;