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

Add the ability to use an array of versions in Parser.satisfies()

This commit is contained in:
Stas Tolpekin 2021-02-26 13:35:57 +03:00
parent f09411489c
commit 9f4429a564
2 changed files with 22 additions and 3 deletions

View File

@ -360,7 +360,7 @@ class Parser {
allDefinitions.forEach((key) => { allDefinitions.forEach((key) => {
const currentDefinition = checkTree[key]; const currentDefinition = checkTree[key];
if (typeof currentDefinition === 'string') { if (typeof currentDefinition === 'string' || Array.isArray(currentDefinition)) {
browsers[key] = currentDefinition; browsers[key] = currentDefinition;
browsersCounter += 1; browsersCounter += 1;
} else if (typeof currentDefinition === 'object') { } else if (typeof currentDefinition === 'object') {
@ -399,7 +399,7 @@ class Parser {
const matchingDefinition = Utils.find(browserNames, name => (this.isBrowser(name, true))); const matchingDefinition = Utils.find(browserNames, name => (this.isBrowser(name, true)));
if (matchingDefinition !== void 0) { if (matchingDefinition !== void 0) {
return this.compareVersion(browsers[matchingDefinition]); return this.compareVersions(browsers[matchingDefinition]);
} }
} }
@ -423,6 +423,19 @@ class Parser {
return browserNameLower === defaultBrowserName; return browserNameLower === defaultBrowserName;
} }
/**
* Check if browser version equals the version or equals one of versions
* @param {(string|string[])} versionsOrVersion versions strings array or version string
* @returns {boolean}
*/
compareVersions(versionsOrVersion) {
if (typeof versionsOrVersion === 'string') {
return this.compareVersion(versionsOrVersion);
}
return versionsOrVersion.some(version => this.compareVersion(version));
}
compareVersion(version) { compareVersion(version) {
let expectedResults = [0]; let expectedResults = [0];
let comparableVersion = version; let comparableVersion = version;

View File

@ -80,7 +80,7 @@ test('Skip parsing shouldn\'t parse', (t) => {
}); });
test('Parser.satisfies should make simple comparisons', (t) => { test('Parser.satisfies should make simple comparisons', (t) => {
// also covers Parser.compareVersion() method // also covers Parser.compareVersion() and Parser.compareVersions() methods
t.is(parser.satisfies({ opera: '>42' }), true); t.is(parser.satisfies({ opera: '>42' }), true);
t.is(parser.satisfies({ opera: '<44' }), true); t.is(parser.satisfies({ opera: '<44' }), true);
t.is(parser.satisfies({ opera: '=43.0.2442.1165' }), true); t.is(parser.satisfies({ opera: '=43.0.2442.1165' }), true);
@ -96,6 +96,12 @@ test('Parser.satisfies should make simple comparisons', (t) => {
t.is(parser.satisfies({ opera: '~43' }), true); t.is(parser.satisfies({ opera: '~43' }), true);
}); });
test('Parser.satisfies should make comparisons with array of versions', (t) => {
// also covers Parser.compareVersion() and Parser.compareVersions() methods
t.is(parser.satisfies({ opera: ['~42', '~43'] }), true);
t.is(parser.satisfies({ opera: ['~40', '~41'] }), false);
});
test('Parser.satisfies should make complex comparison', (t) => { test('Parser.satisfies should make complex comparison', (t) => {
t.is(parser.satisfies({ t.is(parser.satisfies({
macos: { macos: {