parser.js
12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
import browserParsersList from './parser-browsers.js';
import osParsersList from './parser-os.js';
import platformParsersList from './parser-platforms.js';
import enginesParsersList from './parser-engines.js';
import Utils from './utils.js';
/**
* 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
* improvements if you need to make a more particular parsing
* like {@link Parser#parseBrowser} or {@link Parser#parsePlatform}
*
* @throw {Error} in case of empty UA String
*
* @constructor
*/
constructor(UA, skipParsing = false) {
if (UA === void (0) || UA === null || UA === '') {
throw new Error("UserAgent parameter can't be empty");
}
this._ua = UA;
/**
* @typedef ParsedResult
* @property {Object} browser
* @property {String|undefined} [browser.name]
* Browser name, like `"Chrome"` or `"Internet Explorer"`
* @property {String|undefined} [browser.version] Browser version as a String `"12.01.45334.10"`
* @property {Object} os
* @property {String|undefined} [os.name] OS name, like `"Windows"` or `"macOS"`
* @property {String|undefined} [os.version] OS version, like `"NT 5.1"` or `"10.11.1"`
* @property {String|undefined} [os.versionName] OS name, like `"XP"` or `"High Sierra"`
* @property {Object} platform
* @property {String|undefined} [platform.type]
* platform type, can be either `"desktop"`, `"tablet"` or `"mobile"`
* @property {String|undefined} [platform.vendor] Vendor of the device,
* like `"Apple"` or `"Samsung"`
* @property {String|undefined} [platform.model] Device model,
* like `"iPhone"` or `"Kindle Fire HD 7"`
* @property {Object} engine
* @property {String|undefined} [engine.name]
* Can be any of this: `WebKit`, `Blink`, `Gecko`, `Trident`, `Presto`, `EdgeHTML`
* @property {String|undefined} [engine.version] String version of the engine
*/
this.parsedResult = {};
if (skipParsing !== true) {
this.parse();
}
}
/**
* Get UserAgent string of current Parser instance
* @return {String} User-Agent String of the current <Parser> object
*
* @public
*/
getUA() {
return this._ua;
}
/**
* Test a UA string for a regexp
* @param {RegExp} regex
* @return {Boolean}
*/
test(regex) {
return regex.test(this._ua);
}
/**
* Get parsed browser object
* @return {Object}
*/
parseBrowser() {
this.parsedResult.browser = {};
const browserDescriptor = Utils.find(browserParsersList, (_browser) => {
if (typeof _browser.test === 'function') {
return _browser.test(this);
}
if (_browser.test instanceof Array) {
return _browser.test.some(condition => this.test(condition));
}
throw new Error("Browser's test function is not valid");
});
if (browserDescriptor) {
this.parsedResult.browser = browserDescriptor.describe(this.getUA());
}
return this.parsedResult.browser;
}
/**
* Get parsed browser object
* @return {Object}
*
* @public
*/
getBrowser() {
if (this.parsedResult.browser) {
return this.parsedResult.browser;
}
return this.parseBrowser();
}
/**
* Get browser's name
* @return {String} Browser's name or an empty string
*
* @public
*/
getBrowserName(toLowerCase) {
if (toLowerCase) {
return String(this.getBrowser().name).toLowerCase() || '';
}
return this.getBrowser().name || '';
}
/**
* Get browser's version
* @return {String} version of browser
*
* @public
*/
getBrowserVersion() {
return this.getBrowser().version;
}
/**
* Get OS
* @return {Object}
*
* @example
* this.getOS();
* {
* name: 'macOS',
* version: '10.11.12'
* }
*/
getOS() {
if (this.parsedResult.os) {
return this.parsedResult.os;
}
return this.parseOS();
}
/**
* Parse OS and save it to this.parsedResult.os
* @return {*|{}}
*/
parseOS() {
this.parsedResult.os = {};
const os = Utils.find(osParsersList, (_os) => {
if (typeof _os.test === 'function') {
return _os.test(this);
}
if (_os.test instanceof Array) {
return _os.test.some(condition => this.test(condition));
}
throw new Error("Browser's test function is not valid");
});
if (os) {
this.parsedResult.os = os.describe(this.getUA());
}
return this.parsedResult.os;
}
/**
* Get OS name
* @param {Boolean} [toLowerCase] return lower-cased value
* @return {String} name of the OS — macOS, Windows, Linux, etc.
*/
getOSName(toLowerCase) {
const { name } = this.getOS();
if (toLowerCase) {
return String(name).toLowerCase() || '';
}
return name || '';
}
/**
* Get OS version
* @return {String} full version with dots ('10.11.12', '5.6', etc)
*/
getOSVersion() {
return this.getOS().version;
}
/**
* Get parsed platform
* @return {{}}
*/
getPlatform() {
if (this.parsedResult.platform) {
return this.parsedResult.platform;
}
return this.parsePlatform();
}
/**
* Get platform name
* @param {Boolean} [toLowerCase=false]
* @return {*}
*/
getPlatformType(toLowerCase = false) {
const { type } = this.getPlatform();
if (toLowerCase) {
return String(type).toLowerCase() || '';
}
return type || '';
}
/**
* Get parsed platform
* @return {{}}
*/
parsePlatform() {
this.parsedResult.platform = {};
const platform = Utils.find(platformParsersList, (_platform) => {
if (typeof _platform.test === 'function') {
return _platform.test(this);
}
if (_platform.test instanceof Array) {
return _platform.test.some(condition => 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;
}
/**
* Get parsed engine
* @return {{}}
*/
getEngine() {
if (this.parsedResult.engine) {
return this.parsedResult.engine;
}
return this.parseEngine();
}
/**
* Get engines's name
* @return {String} Engines's name or an empty string
*
* @public
*/
getEngineName(toLowerCase) {
if (toLowerCase) {
return String(this.getEngine().name).toLowerCase() || '';
}
return this.getEngine().name || '';
}
/**
* Get parsed platform
* @return {{}}
*/
parseEngine() {
this.parsedResult.engine = {};
const engine = Utils.find(enginesParsersList, (_engine) => {
if (typeof _engine.test === 'function') {
return _engine.test(this);
}
if (_engine.test instanceof Array) {
return _engine.test.some(condition => this.test(condition));
}
throw new Error("Browser's test function is not valid");
});
if (engine) {
this.parsedResult.engine = engine.describe(this.getUA());
}
return this.parsedResult.engine;
}
/**
* Parse full information about the browser
*/
parse() {
this.parseBrowser();
this.parseOS();
this.parsePlatform();
this.parseEngine();
return this;
}
/**
* Get parsed result
* @return {ParsedResult}
*/
getResult() {
return Utils.assign({}, this.parsedResult);
}
/**
* Check if parsed browser matches certain conditions
*
* @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|undefined} Whether the browser satisfies the set conditions or not.
* Returns `undefined` when the browser is no described in the checkTree object.
*
* @example
* const browser = Bowser.getParser(window.navigator.userAgent);
* if (browser.satisfies({chrome: '>118.01.1322' }))
* // or with os
* if (browser.satisfies({windows: { chrome: '>118.01.1322' } }))
* // or with platforms
* if (browser.satisfies({desktop: { chrome: '>118.01.1322' } }))
*/
satisfies(checkTree) {
const platformsAndOSes = {};
let platformsAndOSCounter = 0;
const browsers = {};
let browsersCounter = 0;
const allDefinitions = Object.keys(checkTree);
allDefinitions.forEach((key) => {
const currentDefinition = checkTree[key];
if (typeof currentDefinition === 'string') {
browsers[key] = currentDefinition;
browsersCounter += 1;
} else if (typeof currentDefinition === 'object') {
platformsAndOSes[key] = currentDefinition;
platformsAndOSCounter += 1;
}
});
if (platformsAndOSCounter > 0) {
const platformsAndOSNames = Object.keys(platformsAndOSes);
const OSMatchingDefinition = Utils.find(platformsAndOSNames, name => (this.isOS(name)));
if (OSMatchingDefinition) {
const osResult = this.satisfies(platformsAndOSes[OSMatchingDefinition]);
if (osResult !== void 0) {
return osResult;
}
}
const platformMatchingDefinition = Utils.find(
platformsAndOSNames,
name => (this.isPlatform(name)),
);
if (platformMatchingDefinition) {
const platformResult = this.satisfies(platformsAndOSes[platformMatchingDefinition]);
if (platformResult !== void 0) {
return platformResult;
}
}
}
if (browsersCounter > 0) {
const browserNames = Object.keys(browsers);
const matchingDefinition = Utils.find(browserNames, name => (this.isBrowser(name, true)));
if (matchingDefinition !== void 0) {
return this.compareVersion(browsers[matchingDefinition]);
}
}
return undefined;
}
/**
* Check if the browser name equals the passed string
* @param browserName The string to compare with the browser name
* @param [includingAlias=false] The flag showing whether alias will be included into comparison
* @returns {boolean}
*/
isBrowser(browserName, includingAlias = false) {
const defaultBrowserName = this.getBrowserName().toLowerCase();
let browserNameLower = browserName.toLowerCase();
const alias = Utils.getBrowserTypeByAlias(browserNameLower);
if (includingAlias && alias) {
browserNameLower = alias.toLowerCase();
}
return browserNameLower === defaultBrowserName;
}
compareVersion(version) {
let expectedResults = [0];
let comparableVersion = version;
let isLoose = false;
const currentBrowserVersion = this.getBrowserVersion();
if (typeof currentBrowserVersion !== 'string') {
return void 0;
}
if (version[0] === '>' || version[0] === '<') {
comparableVersion = version.substr(1);
if (version[1] === '=') {
isLoose = true;
comparableVersion = version.substr(2);
} else {
expectedResults = [];
}
if (version[0] === '>') {
expectedResults.push(1);
} else {
expectedResults.push(-1);
}
} else if (version[0] === '=') {
comparableVersion = version.substr(1);
} else if (version[0] === '~') {
isLoose = true;
comparableVersion = version.substr(1);
}
return expectedResults.indexOf(
Utils.compareVersions(currentBrowserVersion, comparableVersion, isLoose),
) > -1;
}
isOS(osName) {
return this.getOSName(true) === String(osName).toLowerCase();
}
isPlatform(platformType) {
return this.getPlatformType(true) === String(platformType).toLowerCase();
}
isEngine(engineName) {
return this.getEngineName(true) === String(engineName).toLowerCase();
}
/**
* Is anything? Check if the browser is called "anything",
* the OS called "anything" or the platform called "anything"
* @param {String} anything
* @returns {Boolean}
*/
is(anything) {
return this.isBrowser(anything) || this.isOS(anything) || this.isPlatform(anything);
}
/**
* Check if any of the given values satisfies this.is(anything)
* @param {String[]} anythings
* @returns {Boolean}
*/
some(anythings = []) {
return anythings.some(anything => this.is(anything));
}
}
export default Parser;