Skip to content
This repository has been archived by the owner on Dec 13, 2019. It is now read-only.

Commit

Permalink
Feature detection mechanism #20
Browse files Browse the repository at this point in the history
- Removed dependency on semver package
- Implemented a simple algorithm of comparing versions
  • Loading branch information
vyacheslav-pushkin committed Dec 12, 2019
1 parent 7354d6d commit 60d260d
Show file tree
Hide file tree
Showing 11 changed files with 186 additions and 1,760 deletions.
1,736 changes: 45 additions & 1,691 deletions dist-browser/cuba.js

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion dist-node/cuba.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,3 @@ export declare class CubaApp {
export declare function getBasicAuthHeaders(client: string, secret: string, locale?: string): {
[header: string]: string;
};
export declare function matchesVersion(versionToTest: string, versionToMatch: string): boolean;
17 changes: 1 addition & 16 deletions dist-node/cuba.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ function __export(m) {
Object.defineProperty(exports, "__esModule", { value: true });
var storage_1 = require("./storage");
var util_1 = require("./util");
var semver = require("semver");
__export(require("./model"));
__export(require("./storage"));
var apps = [];
Expand Down Expand Up @@ -423,7 +422,7 @@ var CubaApp = /** @class */ (function () {
case 1:
_a.sent();
_a.label = 2;
case 2: return [2 /*return*/, matchesVersion(this.apiVersion, minVersion)];
case 2: return [2 /*return*/, util_1.matchesVersion(this.apiVersion, minVersion)];
}
});
});
Expand Down Expand Up @@ -464,17 +463,3 @@ function getBasicAuthHeaders(client, secret, locale) {
};
}
exports.getBasicAuthHeaders = getBasicAuthHeaders;
function matchesVersion(versionToTest, versionToMatch) {
var semverToTest = semver.coerce(versionToTest);
if (!semverToTest) {
// versionToTest cannot be converted to semver
return false;
}
var semverToMatch = semver.coerce(versionToMatch);
if (!semverToMatch) {
// versionToMatch cannot be converted to semver
throw new Error("Cannot determine required REST API version: value " + versionToMatch + " cannot be converted to semver");
}
return semver.gte(semverToTest, semverToMatch);
}
exports.matchesVersion = matchesVersion;
13 changes: 13 additions & 0 deletions dist-node/util.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,15 @@
export declare function base64encode(str: string): string;
/**
* Compares version strings. Intended to be used only for comparing versions consisting of major, minor and
* (optional) patch version (dot-separated) and optional `-SNAPSHOT` suffix. Does not conform to
* Semantic Versioning Specification and will produce incorrect results in some cases not covered above
* (e.g. it doesn't take into account any text including pre-release identifiers, so 7.2.0-beta will be considered
* equal to 7.2.0).
*
* @param testVersion
* @param minimumVersion
*
* @returns true if testVersion is greater or equal than minimumVersion
*/
export declare function matchesVersion(testVersion: string, minimumVersion: string): boolean;
export declare function encodeGetParams(data: any): string;
42 changes: 42 additions & 0 deletions dist-node/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,48 @@ function base64encode(str) {
/* tslint:enable:no-string-literal */
}
exports.base64encode = base64encode;
/**
* Compares version strings. Intended to be used only for comparing versions consisting of major, minor and
* (optional) patch version (dot-separated) and optional `-SNAPSHOT` suffix. Does not conform to
* Semantic Versioning Specification and will produce incorrect results in some cases not covered above
* (e.g. it doesn't take into account any text including pre-release identifiers, so 7.2.0-beta will be considered
* equal to 7.2.0).
*
* @param testVersion
* @param minimumVersion
*
* @returns true if testVersion is greater or equal than minimumVersion
*/
function matchesVersion(testVersion, minimumVersion) {
if (!testVersion) {
return false;
}
if (!minimumVersion) {
throw new Error('Cannot determine required REST API version: the minimum version is not valid');
}
var testVersionComponents = testVersion.split('.');
var requiredVersionComponents = minimumVersion.split('.');
for (var i = 0; i < requiredVersionComponents.length; i++) {
var match = parseInt(requiredVersionComponents[i], 10);
var test = parseInt(testVersionComponents[i], 10);
if (isNaN(match) || match < 0) {
throw new Error('Cannot determine required REST API version: the minimum version is not valid');
}
if ((test === undefined || test === null || isNaN(test)) && match > 0) {
// Required version has more components than test version, and current required version component is > 0
return false;
}
if (test > match) {
return true;
}
else if (test < match) {
return false;
}
}
// Versions are equal
return true;
}
exports.matchesVersion = matchesVersion;
function encodeGetParams(data) {
return Object
.keys(data)
Expand Down
8 changes: 1 addition & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"mocha": "~5.0.5",
"node-fetch": "^2.6.0",
"nyc": "~14.1.1",
"semver": "^6.3.0",
"tslint": "~5.17.0",
"typescript": "3.5.2"
},
Expand Down
20 changes: 1 addition & 19 deletions src/cuba.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import {
} from "./model";
import {DefaultStorage} from "./storage";
import {EntityFilter} from "./filter";
import {base64encode, encodeGetParams} from "./util";
import * as semver from "semver";
import {base64encode, encodeGetParams, matchesVersion} from "./util";

export * from './model';
export * from './storage';
Expand Down Expand Up @@ -524,20 +523,3 @@ export function getBasicAuthHeaders(client: string, secret: string, locale = 'en
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
};
}

export function matchesVersion(versionToTest: string, versionToMatch: string): boolean {
const semverToTest = semver.coerce(versionToTest);
if (!semverToTest) {
// versionToTest cannot be converted to semver
return false;
}

const semverToMatch = semver.coerce(versionToMatch);
if (!semverToMatch) {
// versionToMatch cannot be converted to semver
throw new Error(
`Cannot determine required REST API version: value ${versionToMatch} cannot be converted to semver`);
}

return semver.gte(semverToTest, semverToMatch);
}
48 changes: 48 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,54 @@ export function base64encode(str: string): string {
/* tslint:enable:no-string-literal */
}

/**
* Compares version strings. Intended to be used only for comparing versions consisting of major, minor and
* (optional) patch version (dot-separated) and optional `-SNAPSHOT` suffix. Does not conform to
* Semantic Versioning Specification and will produce incorrect results in some cases not covered above
* (e.g. it doesn't take into account any text including pre-release identifiers, so 7.2.0-beta will be considered
* equal to 7.2.0).
*
* @param testVersion
* @param minimumVersion
*
* @returns true if testVersion is greater or equal than minimumVersion
*/
export function matchesVersion(testVersion: string, minimumVersion: string): boolean {
if (!testVersion) {
return false;
}

if (!minimumVersion) {
throw new Error('Cannot determine required REST API version: the minimum version is not valid');
}

const testVersionComponents = testVersion.split('.');
const requiredVersionComponents = minimumVersion.split('.');

for (let i = 0; i < requiredVersionComponents.length; i++) {
const match = parseInt(requiredVersionComponents[i], 10);
const test = parseInt(testVersionComponents[i], 10);

if (isNaN(match) || match < 0) {
throw new Error('Cannot determine required REST API version: the minimum version is not valid');
}

if ((test === undefined || test === null || isNaN(test)) && match > 0) {
// Required version has more components than test version, and current required version component is > 0
return false;
}

if (test > match) {
return true;
} else if (test < match) {
return false;
}
}

// Versions are equal
return true;
}

export function encodeGetParams(data): string {
return Object
.keys(data)
Expand Down
25 changes: 0 additions & 25 deletions test/cuba.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,31 +76,6 @@ describe('cuba', function () {
});
});

describe('.matchesVersion()', function () {
it('should compare versions correctly', async function () {
const matchesVersion = cuba.matchesVersion;
assert( matchesVersion('7.2.0', '7.2.0'));
assert( matchesVersion('7.2.1', '7.2.0'));
assert( matchesVersion('7.3.0', '7.2.0'));
assert( matchesVersion('8.2.0', '7.2.0'));
assert( matchesVersion('7.2-SNAPSHOT', '7.2.0'));

assert(!matchesVersion('7.2.0', '7.2.1'));
assert(!matchesVersion('7.2.0', '7.3.0'));
assert(!matchesVersion('7.2.0', '8.2.0'));
assert(!matchesVersion('7.2-SNAPSHOT', '7.2.1'));
assert(!matchesVersion('7.2-SNAPSHOT', '7.3.0'));
assert(!matchesVersion('0', '7.2.0'));
assert(!matchesVersion(undefined, '7.2.0'));
assert(!matchesVersion(null, '7.2.0'));
assert(!matchesVersion('A string that cannot be converted to a semver', '7.2.0'));

assert.throws(() => matchesVersion('7.2.0', undefined));
assert.throws(() => matchesVersion('7.2.0', null));
assert.throws(() => matchesVersion('7.2.0', 'A string that cannot be converted to a semver'));
})
});

describe('CubaApp', function () {

let app;
Expand Down
35 changes: 35 additions & 0 deletions test/utlil.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,39 @@ describe('util', function() {
});
});

describe('.matchesVersion()', function () {
it('should compare versions correctly', async function () {
const matchesVersion = util.matchesVersion;
assert(matchesVersion('7.2.0', '7.2.0'));
assert(matchesVersion('7.2.1', '7.2.0'));
assert(matchesVersion('7.3.0', '7.2.0'));
assert(matchesVersion('7.3.0', '7.2.9'));
assert(matchesVersion('7.2.10', '7.2.2'));
assert(matchesVersion('7.2.0', '7.2'));
assert(matchesVersion('7.10.0', '7.9.0'));
assert(matchesVersion('7.02.0', '7.2.0'));
assert(matchesVersion('8.2.0', '7.2.0'));
assert(matchesVersion('7.2-SNAPSHOT', '7.2.0'));
assert(matchesVersion('7.2.0-SNAPSHOT', '7.2.0'));
assert(matchesVersion('7.2beta', '7.2.0'));
assert(matchesVersion('7.2.beta', '7.2.0'));

assert(!matchesVersion('7.2.0', '7.2.1'));
assert(!matchesVersion('7.2.0', '7.3.0'));
assert(!matchesVersion('7.2.0', '8.2.0'));
assert(!matchesVersion('7.2-SNAPSHOT', '7.2.1'));
assert(!matchesVersion('7.2-SNAPSHOT', '7.3.0'));
assert(!matchesVersion('7.2.0-SNAPSHOT', '7.2.1'));
assert(!matchesVersion('0', '7.2.0'));
assert(!matchesVersion(undefined, '7.2.0'));
assert(!matchesVersion(null, '7.2.0'));
assert(!matchesVersion('A string that cannot be converted to a semver', '7.2.0'));

assert.throws(() => matchesVersion('7.2.0', undefined));
assert.throws(() => matchesVersion('7.2.0', null));
assert.throws(() => matchesVersion('7.2.0', 'A string that cannot be converted to a semver'));
})
});


});

0 comments on commit 60d260d

Please sign in to comment.