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

Add semverCheck and related methods

This commit is contained in:
Denis Demchenko 2018-07-02 22:24:02 +03:00
parent 560ec06eeb
commit 6030eb9e5d
2 changed files with 48 additions and 13 deletions

View File

@ -91,15 +91,15 @@ class Parser {
/** /**
* Get browser's name * Get browser's name
* @return {String} Browser's name * @return {String} Browser's name or an empty string
* *
* @public * @public
*/ */
getBrowserName(toLowerCase) { getBrowserName(toLowerCase) {
if (toLowerCase) { if (toLowerCase) {
return String(this.getBrowser().name).toLowerCase(); return String(this.getBrowser().name).toLowerCase() || '';
} }
return this.getBrowser().name; return this.getBrowser().name || '';
} }
@ -167,10 +167,10 @@ class Parser {
const { name } = this.getOS(); const { name } = this.getOS();
if (toLowerCase) { if (toLowerCase) {
return String(name).toLowerCase(); return String(name).toLowerCase() || '';
} }
return name; return name || '';
} }
/** /**
@ -193,6 +193,21 @@ class Parser {
return this.parsePlatform(); return this.parsePlatform();
} }
/**
* Get platform name
* @param {Boolean} toLowerCase
* @return {*}
*/
getPlatformName(toLowerCase) {
const { name } = this.getPlatform();
if (toLowerCase) {
return String(name).toLowerCase() || '';
}
return name || '';
}
/** /**
* Get parsed platform * Get parsed platform
* @return {{}} * @return {{}}
@ -288,30 +303,38 @@ class Parser {
* // or with platforms * // or with platforms
* if (browser.check({desktop: { chrome: '>118.01.1322' } })) * if (browser.check({desktop: { chrome: '>118.01.1322' } }))
*/ */
check(checkTree) { semverCheck(checkTree) {
const thisVersion = this.getBrowser().version; const thisVersion = this.getBrowser().version;
if (!semver.valid(semver.coerce(thisVersion))) { if (!semver.valid(semver.coerce(thisVersion))) {
throw new Error(`Version of current browser doesn't seem applicable: ${thisVersion}`); throw new Error(`Version of current browser doesn't seem applicable: ${thisVersion}`);
} }
const keysToProcess = Object.keys(checkTree); const keysToProcess = Object.keys(checkTree);
keysToProcess.some((browserAttribute) => { return keysToProcess.some((browserAttribute) => {
const objectOrVersion = checkTree[browserAttribute]; const objectOrVersion = checkTree[browserAttribute];
if (typeof objectOrVersion === 'object') { if (typeof objectOrVersion === 'object') {
return (this.isOs(browserAttribute) || this.isPlatform(browserAttribute)) return (this.isOs(browserAttribute) || this.isPlatform(browserAttribute))
&& this.check(objectOrVersion); && this.semverCheck(objectOrVersion);
} }
return this.isBrowser(browserAttribute) && this.matches(objectOrVersion); return this.isBrowser(browserAttribute) && this.satisfies(objectOrVersion);
}); });
} }
isBrowser(browserName) { isBrowser(browserName) {
return this.getBrowser().name === browserName; return this.getBrowserName(true) === String(browserName).toLowerCase();
} }
satisfies(version) { satisfies(version) {
return semver.satisfies(this.getBrowser().version, version); return semver.satisfies(semver.coerce(this.getBrowser().version), version);
}
isOs(osName) {
return this.getOSName(true) === String(osName).toLowerCase();
}
isPlatform(platformName) {
return this.getPlatformName(true) === String(platformName).toLowerCase();
} }
} }

View File

@ -17,7 +17,7 @@ test('Parser.test', (t) => {
t.truthy(parser.test(/Chrome/i)); t.truthy(parser.test(/Chrome/i));
}); });
test('Parser._parseBrowser is being called when the Parser.getBrowser() is called', (t) => { test('Parser.parseBrowser is being called when the Parser.getBrowser() is called', (t) => {
const spy = sinon.spy(parser, 'parseBrowser'); const spy = sinon.spy(parser, 'parseBrowser');
const b = parser.getBrowser(); const b = parser.getBrowser();
t.truthy(spy.called); t.truthy(spy.called);
@ -34,7 +34,7 @@ test('Parser.getBrowserVersion returns a correct result', (t) => {
t.is(parser.getBrowserVersion(), '43.0.2442.1165'); t.is(parser.getBrowserVersion(), '43.0.2442.1165');
}); });
test('Parser._parseOS is being called when getOS() called', (t) => { test('Parser.parseOS is being called when getOS() called', (t) => {
const spy = sinon.spy(parser, 'parseOS'); const spy = sinon.spy(parser, 'parseOS');
parser.getOS(); parser.getOS();
t.truthy(spy.called); t.truthy(spy.called);
@ -56,3 +56,15 @@ test('Parser.getOSVersion returns a correct result', (t) => {
test('Skip parsing shouldn\'t parse', (t) => { test('Skip parsing shouldn\'t parse', (t) => {
t.deepEqual((new Parser(UA, true)).getResult(), {}); t.deepEqual((new Parser(UA, true)).getResult(), {});
}); });
test('Parser.check should make simple check', (t) => {
t.is(parser.semverCheck({ opera: '>42' }), true);
});
test('Parser.check should make simple check', (t) => {
t.is(parser.semverCheck({
macos: {
opera: '>42',
},
}), true);
});