fix(core): update

This commit is contained in:
Philipp Kunz 2021-04-26 07:16:26 +00:00
parent 2899972e5b
commit 87dd7b6635
2 changed files with 42 additions and 9 deletions

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;
}
}