Add Parser.check and related things

pull/227/head
Denis Demchenko 6 years ago
parent 9cb04fcbae
commit 560ec06eeb

3
package-lock.json generated

@ -15666,8 +15666,7 @@
"semver": {
"version": "5.5.0",
"resolved": "https://registry.npmjs.org/semver/-/semver-5.5.0.tgz",
"integrity": "sha512-4SJ3dm0WAwWy/NVeioZh5AntkdJoWKxHxcmyP622fOkgHa4z3R0TdBJICINyaSDE6uNwVc8gZr+ZinwZAH4xIA==",
"dev": true
"integrity": "sha512-4SJ3dm0WAwWy/NVeioZh5AntkdJoWKxHxcmyP622fOkgHa4z3R0TdBJICINyaSDE6uNwVc8gZr+ZinwZAH4xIA=="
},
"semver-diff": {
"version": "2.1.0",

@ -60,5 +60,8 @@
"test": "eslint ./src & nyc --reporter=html --reporter=text ava",
"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 osParsersList from './parser-os';
import platformParsersList from './parser-platforms';
@ -271,6 +272,47 @@ class Parser {
getResult() {
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;

Loading…
Cancel
Save