mirror of
https://github.com/lancedikson/bowser
synced 2024-10-27 20:34:22 +00:00
Add Parser.check and related things
This commit is contained in:
parent
9cb04fcbae
commit
560ec06eeb
3
package-lock.json
generated
3
package-lock.json
generated
@ -15666,8 +15666,7 @@
|
|||||||
"semver": {
|
"semver": {
|
||||||
"version": "5.5.0",
|
"version": "5.5.0",
|
||||||
"resolved": "https://registry.npmjs.org/semver/-/semver-5.5.0.tgz",
|
"resolved": "https://registry.npmjs.org/semver/-/semver-5.5.0.tgz",
|
||||||
"integrity": "sha512-4SJ3dm0WAwWy/NVeioZh5AntkdJoWKxHxcmyP622fOkgHa4z3R0TdBJICINyaSDE6uNwVc8gZr+ZinwZAH4xIA==",
|
"integrity": "sha512-4SJ3dm0WAwWy/NVeioZh5AntkdJoWKxHxcmyP622fOkgHa4z3R0TdBJICINyaSDE6uNwVc8gZr+ZinwZAH4xIA=="
|
||||||
"dev": true
|
|
||||||
},
|
},
|
||||||
"semver-diff": {
|
"semver-diff": {
|
||||||
"version": "2.1.0",
|
"version": "2.1.0",
|
||||||
|
@ -60,5 +60,8 @@
|
|||||||
"test": "eslint ./src & nyc --reporter=html --reporter=text ava",
|
"test": "eslint ./src & nyc --reporter=html --reporter=text ava",
|
||||||
"coverage": "nyc report --reporter=text-lcov | coveralls"
|
"coverage": "nyc report --reporter=text-lcov | coveralls"
|
||||||
},
|
},
|
||||||
"license": "MIT"
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"semver": "^5.5.0"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import semver from 'semver';
|
||||||
import browserParsersList from './parser-browsers';
|
import browserParsersList from './parser-browsers';
|
||||||
import osParsersList from './parser-os';
|
import osParsersList from './parser-os';
|
||||||
import platformParsersList from './parser-platforms';
|
import platformParsersList from './parser-platforms';
|
||||||
@ -271,6 +272,47 @@ class Parser {
|
|||||||
getResult() {
|
getResult() {
|
||||||
return this.parsedResult;
|
return 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
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* const browser = new Bowser(UA);
|
||||||
|
* if (browser.check({chrome: '>118.01.1322' }))
|
||||||
|
* // or with os
|
||||||
|
* if (browser.check({windows: { chrome: '>118.01.1322' } }))
|
||||||
|
* // or with platforms
|
||||||
|
* if (browser.check({desktop: { chrome: '>118.01.1322' } }))
|
||||||
|
*/
|
||||||
|
check(checkTree) {
|
||||||
|
const thisVersion = this.getBrowser().version;
|
||||||
|
if (!semver.valid(semver.coerce(thisVersion))) {
|
||||||
|
throw new Error(`Version of current browser doesn't seem applicable: ${thisVersion}`);
|
||||||
|
}
|
||||||
|
const keysToProcess = Object.keys(checkTree);
|
||||||
|
keysToProcess.some((browserAttribute) => {
|
||||||
|
const objectOrVersion = checkTree[browserAttribute];
|
||||||
|
|
||||||
|
if (typeof objectOrVersion === 'object') {
|
||||||
|
return (this.isOs(browserAttribute) || this.isPlatform(browserAttribute))
|
||||||
|
&& this.check(objectOrVersion);
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.isBrowser(browserAttribute) && this.matches(objectOrVersion);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
isBrowser(browserName) {
|
||||||
|
return this.getBrowser().name === browserName;
|
||||||
|
}
|
||||||
|
|
||||||
|
satisfies(version) {
|
||||||
|
return semver.satisfies(this.getBrowser().version, version);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Parser;
|
export default Parser;
|
||||||
|
Loading…
Reference in New Issue
Block a user