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

Add a bit of docs

This commit is contained in:
Denis Demchenko
2018-07-05 22:44:43 +03:00
parent 15b431562b
commit 1f572ed8f4
19 changed files with 5804 additions and 19 deletions

View File

@@ -7,31 +7,38 @@
import Parser from './parser';
/**
* Bowser class
* Keep it simple as much as it can be
* It's supposed to work with collections of Parser instances
* Bowser class.
* Keep it simple as much as it can be.
* It's supposed to work with collections of {@link Parser} instances
* rather then solve one-instance problems.
* All the one-instance stuff is located in Parser class.
*/
class Bowser {
/**
* Creates a Parser instance instead of Bowser's one
* Creates a {@link module:parser:Parser} instance
*
* @param {String} UA UserAgent string
* @param {Boolean} [skipParsing=false] same as skipParsing for Parser
* @param {String} UA UserAgent string
* @param {Boolean} [skipParsing=false] same as skipParsing for {@link Parser}
* @returns {Parser}
* @throws {Error} when UA is not a String
*
* @example
* const bowser = new Bowser(window.navigator.userAgent);
* bowser.getResult()
*/
constructor(UA, skipParsing=false) {
static getParser(UA, skipParsing=false) {
if (typeof UA !== 'string') {
throw new Error('UserAgent should be a string');
}
return new Parser(UA, skipParsing);
}
/**
* Creates a {@link Parser} instance and runs {@link Parser.getResult} immediately
*
* @param UA
* @return {ParsedResult}
*/
static parse(UA) {
return (new Bowser(UA)).getResult();
}

View File

@@ -4,14 +4,17 @@ import platformParsersList from './parser-platforms';
import enginesParsersList from './parser-engines';
import { compareVersions } from './utils';
/**
* The main class that arranges the whole parsing process.
*/
class Parser {
/**
* Create instance of Parser
*
* @param {String} UA User-Agent string
* @param {Boolean} [skipParsing=false] parser can skip parsing in purpose of performance
* @param {String} UA User-Agent string
* @param {Boolean} [skipParsing=false] parser can skip parsing in purpose of performance
* improvements if you need to make a more particular parsing
* like `.parseBrowser()` or `.parsePlatform()`
* like {@link Parser#parseBrowser} or {@link Parser#parsePlatform}
*
* @throw {Error} in case of empty UA String
*
@@ -23,6 +26,24 @@ class Parser {
}
this._ua = UA;
/**
* @typedef ParsedResult
* @property {Object} browser
* @property {String} [browser.name]
* @property {String} [browser.version]
* @property {Object} os
* @property {String} [os.name]
* @property {String} [os.version]
* @property {String} [os.versionName]
* @property {Object} platform
* @property {String} [platform.type]
* @property {String} [platform.vendor]
* @property {String} [platform.model]
* @property {Object} engine
* @property {String} [engine.name]
* @property {String} [engine.version]
*/
this.parsedResult = {};
if (skipParsing !== true) {
@@ -32,7 +53,7 @@ class Parser {
/**
* Get UserAgent string of current Parser instance
* @return {String}
* @return {String} User-Agent String of the current <Parser> object
*
* @public
*/
@@ -284,6 +305,10 @@ class Parser {
return this;
}
/**
* Get parsed result
* @return {ParsedResult}
*/
getResult() {
/* TODO: Make this function pure, return a new object instead of the reference */
return this.parsedResult;
@@ -292,10 +317,12 @@ class Parser {
/**
* Check if parsed browser matches certain conditions
*
* @param {Object} checkTree It's one or two layered object,
* @param {Object} checkTree It's one or two layered object,
* which can include a platform or an OS on the first layer
* and should have browsers specs on the bottom-laying layer
*
* @returns {Boolean} whether the browser satisfies the set conditions or not
*
* @example
* const browser = new Bowser(UA);
* if (browser.check({chrome: '>118.01.1322' }))