4 Commits

Author SHA1 Message Date
fdef084c6b 2.0.7 2021-04-26 07:16:27 +00:00
87dd7b6635 fix(core): update 2021-04-26 07:16:26 +00:00
2899972e5b 2.0.6 2021-04-26 03:44:42 +00:00
2a9d678a68 fix(core): update 2021-04-26 03:44:42 +00:00
4 changed files with 46 additions and 13 deletions

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{ {
"name": "@pushrocks/smartversion", "name": "@pushrocks/smartversion",
"version": "2.0.5", "version": "2.0.7",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {

View File

@ -1,6 +1,6 @@
{ {
"name": "@pushrocks/smartversion", "name": "@pushrocks/smartversion",
"version": "2.0.5", "version": "2.0.7",
"private": false, "private": false,
"description": "handle semver with ease", "description": "handle semver with ease",
"main": "dist_ts/index.js", "main": "dist_ts/index.js",
@ -8,8 +8,8 @@
"author": "Lossless GmbH", "author": "Lossless GmbH",
"license": "MIT", "license": "MIT",
"scripts": { "scripts": {
"test": "(tstest test/)", "test": "(tstest test/ --web)",
"build": "(tsbuild)" "build": "(tsbuild --web)"
}, },
"devDependencies": { "devDependencies": {
"@gitzone/tsbuild": "^2.1.25", "@gitzone/tsbuild": "^2.1.25",

View File

@ -1,13 +1,9 @@
import { expect, tap } from '@pushrocks/tapbundle'; import { expect, tap } from '@pushrocks/tapbundle';
import * as smartversion from '../ts/index'; import * as smartversion from '../ts/index';
let localSmartVersion: smartversion.SmartVersion;
tap.test('should create a valid SmartVersion', async () => { tap.test('should create a valid SmartVersion', async () => {
localSmartVersion = new smartversion.SmartVersion('3.2.1'); const localSmartVersion = new smartversion.SmartVersion('3.2.1');
expect(localSmartVersion).to.be.instanceof(smartversion.SmartVersion); expect(localSmartVersion).to.be.instanceof(smartversion.SmartVersion);
});
tap.test('should correctly classify greater and less than', async () => {
// tslint:disable-next-line:no-unused-expression // tslint:disable-next-line:no-unused-expression
expect(localSmartVersion.greaterThanString('4.0.0')).to.be.false; expect(localSmartVersion.greaterThanString('4.0.0')).to.be.false;
// tslint:disable-next-line:no-unused-expression // tslint:disable-next-line:no-unused-expression
@ -18,4 +14,17 @@ tap.test('should correctly classify greater and less than', async () => {
expect(localSmartVersion.lessThanString('v3.0.0')).to.be.false; expect(localSmartVersion.lessThanString('v3.0.0')).to.be.false;
}); });
tap.test('should create a valid SmartVersion', async () => {
const localSmartVersion = smartversion.SmartVersion.fromFuzzyString('4');
expect(localSmartVersion).to.be.instanceof(smartversion.SmartVersion);
console.log(localSmartVersion.versionString);
});
tap.test('should create a valid SmartVersion', async () => {
const localSmartVersion = smartversion.SmartVersion.fromFuzzyString('4.x');
const bestMatch = localSmartVersion.getBestMatch(['4.0.1', '4.7.5', '4.3.0']);
console.log(bestMatch);
expect(bestMatch).to.equal('4.7.5');
});
tap.start(); tap.start();

View File

@ -1,9 +1,12 @@
import * as plugins from './smartversion.plugins'; import * as plugins from './smartversion.plugins';
import { SemVer } from 'semver';
export class SmartVersion { export class SmartVersion {
public semver: SemVer; public static fromFuzzyString(fuzzyString): SmartVersion {
return new SmartVersion(plugins.semver.minVersion(fuzzyString).version, fuzzyString);
}
public originalVersionString: string;
public semver: plugins.semver.SemVer;
public versionString: string; public versionString: string;
public update = { public update = {
patch: () => { patch: () => {
@ -17,7 +20,8 @@ export class SmartVersion {
}, },
}; };
constructor(semVerStringArg: string) { constructor(semVerStringArg: string, originalStringArg?: string) {
this.originalVersionString = originalStringArg;
this.semver = new plugins.semver.SemVer(semVerStringArg); this.semver = new plugins.semver.SemVer(semVerStringArg);
this.versionString = this.semver.version; this.versionString = this.semver.version;
} }
@ -55,4 +59,24 @@ export class SmartVersion {
public lessThanString(versionStringArg) { public lessThanString(versionStringArg) {
return plugins.semver.lt(this.versionString, versionStringArg); return plugins.semver.lt(this.versionString, versionStringArg);
} }
/**
* tries to get the best match from a range of available versions
*/
public getBestMatch(availableVersions: string[]): string {
let bestMatchingVersion: string;
for (const versionArg of availableVersions) {
if (!plugins.semver.satisfies(versionArg, this.originalVersionString)) {
continue;
}
if(!bestMatchingVersion) {
bestMatchingVersion = versionArg;
} else {
if (plugins.semver.lt(bestMatchingVersion, versionArg)) {
bestMatchingVersion = versionArg;
}
}
}
return bestMatchingVersion;
}
} }