1
0
mirror of https://github.com/lancedikson/bowser synced 2024-10-27 20:34:22 +00:00

Added a definitive-working definition for v2

This commit is contained in:
Alexander Cerutti 2019-01-15 09:42:23 +01:00 committed by GitHub
parent e17180a6a8
commit 93e1ff52aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

173
index.d.ts vendored
View File

@ -1,57 +1,39 @@
// Type definitions for Bowser v2 // Type definitions for Bowser v2
// Project: https://github.com/lancedikson/bowser // Project: https://github.com/lancedikson/bowser
// Definitions by: Alexander P. Cerutti <https://github.com/alexandercerutti> // Definitions by: Alexander P. Cerutti <https://github.com/alexandercerutti>,
declare module "bowser" { export = Bowser;
/** export as namespace Bowser;
* Bowser class.
* Keep it simple as much as it can be. declare namespace Bowser {
* 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 {
constructor();
/** /**
* Creates a {@link module:parser:Parser} instance * Creates a Parser instance
* * @param {string} UA - User agent string
* @param {String} UA UserAgent string * @param {boolean} skipParsing
* @param {Boolean} [skipParsing=false] same as skipParsing for {@link Parser}
* @returns {Parser}
* @throws {Error} when UA is not a String
*
* @example
* const parser = Bowser.getParser(window.navigator.userAgent);
* const result = parser.getResult();
*/ */
static getParser(UA: string, skipParsing?: boolean): Parser
function getParser(UA: string, skipParsing?: boolean): Parser.Parser;
/** /**
* Creates a {@link Parser} instance and runs {@link Parser.getResult} immediately * Creates a Parser instance and runs Parser.getResult immediately
* * @param UA - User agent string
* @param UA * @returns {Parser.ParsedResult}
* @return {ParsedResult}
*
* @example
* const result = Bowser.parse(window.navigator.userAgent);
*/ */
static parse(UA: string): Parser.ParsedResult
function parse(UA: string): Parser.ParsedResult;
} }
/** declare namespace Parser {
* The main class that arranges the whole parsing process.
*/
class Parser { class Parser {
constructor(UA: string, skipParsing?: boolean); constructor(UA: string, skipParsing?: boolean);
/** /**
* Get parsed browser object * Get parsed browser object
* @return {Parser.BrowserDetails} Browser's details * @return {BrowserDetails} Browser's details
*/ */
getBrowser(): Parser.BrowserDetails; getBrowser(): BrowserDetails;
/** /**
* Get browser's name * Get browser's name
@ -69,7 +51,7 @@ declare module "bowser" {
/** /**
* Get OS * Get OS
* @return {Parser.OSDetails} - OS Details * @return {OSDetails} - OS Details
* *
* @example * @example
* this.getOS(); // { * this.getOS(); // {
@ -78,7 +60,7 @@ declare module "bowser" {
* // } * // }
*/ */
getOS(): Parser.OSDetails; getOS(): OSDetails;
/** /**
* Get OS name * Get OS name
@ -97,10 +79,10 @@ declare module "bowser" {
/** /**
* Get parsed platform * Get parsed platform
* @returns {Parser.PlatformDetails} * @returns {PlatformDetails}
*/ */
getPlatform(): Parser.PlatformDetails; getPlatform(): PlatformDetails;
/** /**
* Get platform name * Get platform name
@ -111,17 +93,17 @@ declare module "bowser" {
/** /**
* Get parsed engine * Get parsed engine
* @returns {Parser.EngineDetails} * @returns {EngineDetails}
*/ */
getEngine(): Parser.EngineDetails; getEngine(): EngineDetails;
/** /**
* Get parsed result * Get parsed result
* @return {Parser.ParsedResult} * @return {ParsedResult}
*/ */
getResult(): Parser.ParsedResult; getResult(): ParsedResult;
/** /**
* Get UserAgent string of current Parser instance * Get UserAgent string of current Parser instance
@ -147,36 +129,36 @@ declare module "bowser" {
/** /**
* Get parsed browser object * Get parsed browser object
* @returns {Parser.BrowserDetails} * @returns {BrowserDetails}
*/ */
parseBrowser(): Parser.BrowserDetails; parseBrowser(): BrowserDetails;
/** /**
* Get parsed engine * Get parsed engine
* @returns {Parser.EngineDetails} * @returns {EngineDetails}
*/ */
parseEngine(): Parser.EngineDetails; parseEngine(): EngineDetails;
/** /**
* Parse OS and save it to this.parsedResult.os * Parse OS and save it to this.parsedResult.os
* @returns {Parser.OSDetails} * @returns {OSDetails}
*/ */
parseOS(): Parser.OSDetails; parseOS(): OSDetails;
/** /**
* Get parsed platform * Get parsed platform
* @returns {Parser.PlatformDetails} * @returns {PlatformDetails}
*/ */
parsePlatform(): Parser.PlatformDetails; parsePlatform(): PlatformDetails;
/** /**
* Check if parsed browser matches certain conditions * Check if parsed browser matches certain conditions
* *
* @param {Parser.checkTree} checkTree It's one or two layered object, * @param {checkTree} checkTree It's one or two layered object,
* which can include a platform or an OS on the first layer * which can include a platform or an OS on the first layer
* and should have browsers specs on the bottom-laying layer * and should have browsers specs on the bottom-laying layer
* *
@ -192,7 +174,7 @@ declare module "bowser" {
* if (browser.check({desktop: { chrome: '>118.01.1322' } })) * if (browser.check({desktop: { chrome: '>118.01.1322' } }))
*/ */
satisfies(checkTree: Parser.checkTree): boolean | undefined; satisfies(checkTree: checkTree): boolean | undefined;
/** /**
* Check if any of the given values satifies `.is(anything)` * Check if any of the given values satifies `.is(anything)`
@ -208,20 +190,19 @@ declare module "bowser" {
* @returns {boolean} true if the regex matches the UA, false otherwise. * @returns {boolean} true if the regex matches the UA, false otherwise.
*/ */
test(regex: RegExp): boolean test(regex: RegExp): boolean;
} }
namespace Parser {
interface ParsedResult { interface ParsedResult {
browser: Details; browser: BrowserDetails;
os: OSDetails; os: OSDetails;
platform: PlatformDetails; platform: PlatformDetails;
engine: Details; engine: EngineDetails;
} }
interface Details { interface Details {
name?: string; name?: string;
version?: Array<{index: number, input: string} | boolean | string | any>; version?: string;
} }
interface OSDetails extends Details { interface OSDetails extends Details {
@ -241,77 +222,3 @@ declare module "bowser" {
[key: string]: any; [key: string]: any;
} }
} }
class Utils {
/**
* Get first matched item for a string
* @param {RegExp} regexp
* @param {String} ua
* @return {Array|{index: number, input: string}|*|boolean|string}
*/
static getFirstMatch(regexp: RegExp, ua: string): Array<{index: number, input: string} | boolean | string | any>;
/**
* Get second matched item for a string
* @param regexp
* @param {String} ua
* @return {Array|{index: number, input: string}|*|boolean|string}
*/
static getSecondMatch(regexp: RegExp, ua: string): Array<{index: number, input: string} | boolean | string | any>;
/**
* Match a regexp and return a constant or undefined
* @param {RegExp} regexp
* @param {String} ua
* @param {*} _const Any const that will be returned if regexp matches the string
* @return {*}
*/
static matchAndReturnConst(regexp: RegExp, ua: string, _const: any): any | undefined;
/**
* Retrieves Windows commercial name from NT Core version name
* @param {string} version
* @returns {string | undefined}
*/
static getWindowsVersionName(version: string): string | undefined;
/**
* Get version precisions count
*
* @example
* getVersionPrecision("1.10.3") // 3
*
* @param {string} version
* @return {number}
*/
static getVersionPrecision(version: string): number
/**
* Calculate browser version weight
*
* @example
* compareVersions('1.10.2.1', '1.8.2.1.90') // 1
* compareVersions('1.010.2.1', '1.09.2.1.90'); // 1
* compareVersions('1.10.2.1', '1.10.2.1'); // 0
* compareVersions('1.10.2.1', '1.0800.2'); // -1
* compareVersions('1.10.2.1', '1.10', true); // 0
*
* @param {String} versionA versions versions to compare
* @param {String} versionB versions versions to compare
* @param {boolean} [isLoose] enable loose comparison
* @return {Number} comparison result: -1 when versionA is lower,
* 1 when versionA is bigger, 0 when both equal
*/
static compareVersions(versionA: string, versionB: string, isLoose?: boolean): number;
/**
* Array::map polyfill
*
* @param {Array} arr
* @param {Function} iterator
* @return {Array}
*/
static map(arr: Array<any>, iterator: Function): Array<any>
}
export = Bowser;
}