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",
"version": "2.0.5",
"version": "2.0.7",
"lockfileVersion": 1,
"requires": true,
"dependencies": {

View File

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

View File

@ -1,13 +1,9 @@
import { expect, tap } from '@pushrocks/tapbundle';
import * as smartversion from '../ts/index';
let localSmartVersion: smartversion.SmartVersion;
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);
});
tap.test('should correctly classify greater and less than', async () => {
// tslint:disable-next-line:no-unused-expression
expect(localSmartVersion.greaterThanString('4.0.0')).to.be.false;
// 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;
});
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();

View File

@ -1,9 +1,12 @@
import * as plugins from './smartversion.plugins';
import { SemVer } from 'semver';
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 update = {
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.versionString = this.semver.version;
}
@ -55,4 +59,24 @@ export class SmartVersion {
public lessThanString(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;
}
}