index.d.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // Type definitions for Bowser v2
  2. // Project: https://github.com/lancedikson/bowser
  3. // Definitions by: Alexander P. Cerutti <https://github.com/alexandercerutti>,
  4. export = Bowser;
  5. export as namespace Bowser;
  6. declare namespace Bowser {
  7. /**
  8. * Creates a Parser instance
  9. * @param {string} UA - User agent string
  10. * @param {boolean} skipParsing
  11. */
  12. function getParser(UA: string, skipParsing?: boolean): Parser.Parser;
  13. /**
  14. * Creates a Parser instance and runs Parser.getResult immediately
  15. * @param UA - User agent string
  16. * @returns {Parser.ParsedResult}
  17. */
  18. function parse(UA: string): Parser.ParsedResult;
  19. /**
  20. * Constants exposed via bowser getters
  21. */
  22. const BROWSER_MAP: Record<string, string>;
  23. const ENGINE_MAP: Record<string, string>;
  24. const OS_MAP: Record<string, string>;
  25. const PLATFORMS_MAP: Record<string, string>;
  26. namespace Parser {
  27. interface Parser {
  28. constructor(UA: string, skipParsing?: boolean): Parser.Parser;
  29. /**
  30. * Get parsed browser object
  31. * @return {BrowserDetails} Browser's details
  32. */
  33. getBrowser(): BrowserDetails;
  34. /**
  35. * Get browser's name
  36. * @param {Boolean} [toLowerCase] return lower-cased value
  37. * @return {String} Browser's name or an empty string
  38. */
  39. getBrowserName(toLowerCase?: boolean): string;
  40. /**
  41. * Get browser's version
  42. * @return {String} version of browser
  43. */
  44. getBrowserVersion(): string;
  45. /**
  46. * Get OS
  47. * @return {OSDetails} - OS Details
  48. *
  49. * @example
  50. * this.getOS(); // {
  51. * // name: 'macOS',
  52. * // version: '10.11.12',
  53. * // }
  54. */
  55. getOS(): OSDetails;
  56. /**
  57. * Get OS name
  58. * @param {Boolean} [toLowerCase] return lower-cased value
  59. * @return {String} name of the OS — macOS, Windows, Linux, etc.
  60. */
  61. getOSName(toLowerCase?: boolean): string;
  62. /**
  63. * Get OS version
  64. * @return {String} full version with dots ('10.11.12', '5.6', etc)
  65. */
  66. getOSVersion(): string;
  67. /**
  68. * Get parsed platform
  69. * @returns {PlatformDetails}
  70. */
  71. getPlatform(): PlatformDetails;
  72. /**
  73. * Get platform name
  74. * @param {boolean} toLowerCase
  75. */
  76. getPlatformType(toLowerCase?: boolean): string;
  77. /**
  78. * Get parsed engine
  79. * @returns {EngineDetails}
  80. */
  81. getEngine(): EngineDetails;
  82. /**
  83. * Get parsed engine's name
  84. * @returns {String} Engine's name or an empty string
  85. */
  86. getEngineName(): string;
  87. /**
  88. * Get parsed result
  89. * @return {ParsedResult}
  90. */
  91. getResult(): ParsedResult;
  92. /**
  93. * Get UserAgent string of current Parser instance
  94. * @return {String} User-Agent String of the current <Parser> object
  95. */
  96. getUA(): string;
  97. /**
  98. * Is anything? Check if the browser is called "anything",
  99. * the OS called "anything" or the platform called "anything"
  100. * @param {String} anything
  101. * @returns {Boolean}
  102. */
  103. is(anything: any): boolean;
  104. /**
  105. * Parse full information about the browser
  106. * @returns {Parser.Parser}
  107. */
  108. parse(): Parser.Parser;
  109. /**
  110. * Get parsed browser object
  111. * @returns {BrowserDetails}
  112. */
  113. parseBrowser(): BrowserDetails;
  114. /**
  115. * Get parsed engine
  116. * @returns {EngineDetails}
  117. */
  118. parseEngine(): EngineDetails;
  119. /**
  120. * Parse OS and save it to this.parsedResult.os
  121. * @returns {OSDetails}
  122. */
  123. parseOS(): OSDetails;
  124. /**
  125. * Get parsed platform
  126. * @returns {PlatformDetails}
  127. */
  128. parsePlatform(): PlatformDetails;
  129. /**
  130. * Check if parsed browser matches certain conditions
  131. *
  132. * @param {checkTree} checkTree It's one or two layered object,
  133. * which can include a platform or an OS on the first layer
  134. * and should have browsers specs on the bottom-laying layer
  135. *
  136. * @returns {Boolean|undefined} Whether the browser satisfies the set conditions or not.
  137. * Returns `undefined` when the browser is no described in the checkTree object.
  138. *
  139. * @example
  140. * const browser = new Bowser(UA);
  141. * if (browser.check({chrome: '>118.01.1322' }))
  142. * // or with os
  143. * if (browser.check({windows: { chrome: '>118.01.1322' } }))
  144. * // or with platforms
  145. * if (browser.check({desktop: { chrome: '>118.01.1322' } }))
  146. */
  147. satisfies(checkTree: checkTree): boolean | undefined;
  148. /**
  149. * Check if the browser name equals the passed string
  150. * @param browserName The string to compare with the browser name
  151. * @param [includingAlias=false] The flag showing whether alias will be included into comparison
  152. * @returns {boolean}
  153. */
  154. isBrowser(browserName: string, includingAlias?: boolean): boolean;
  155. /**
  156. * Check if any of the given values satifies `.is(anything)`
  157. * @param {string[]} anythings
  158. * @returns {boolean} true if at least one condition is satisfied, false otherwise.
  159. */
  160. some(anythings: string[]): boolean | undefined;
  161. /**
  162. * Test a UA string for a regexp
  163. * @param regex
  164. * @returns {boolean} true if the regex matches the UA, false otherwise.
  165. */
  166. test(regex: RegExp): boolean;
  167. }
  168. interface ParsedResult {
  169. browser: BrowserDetails;
  170. os: OSDetails;
  171. platform: PlatformDetails;
  172. engine: EngineDetails;
  173. }
  174. interface Details {
  175. name?: string;
  176. version?: string;
  177. }
  178. interface OSDetails extends Details {
  179. versionName?: string;
  180. }
  181. interface PlatformDetails {
  182. type?: string;
  183. vendor?: string;
  184. model?: string;
  185. }
  186. type BrowserDetails = Details;
  187. type EngineDetails = Details;
  188. interface checkTree {
  189. [key: string]: any;
  190. }
  191. }
  192. }