Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
b295f3d7e3 | |||
c8438069d3 | |||
af212b068d | |||
ed87cdb101 | |||
cb589f5863 | |||
fc41cb1403 | |||
fdef084c6b | |||
87dd7b6635 |
23335
package-lock.json
generated
23335
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
15
package.json
15
package.json
@ -1,10 +1,11 @@
|
||||
{
|
||||
"name": "@pushrocks/smartversion",
|
||||
"version": "2.0.6",
|
||||
"version": "3.0.2",
|
||||
"private": false,
|
||||
"description": "handle semver with ease",
|
||||
"main": "dist_ts/index.js",
|
||||
"typings": "dist_ts/index.d.ts",
|
||||
"type": "module",
|
||||
"author": "Lossless GmbH",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
@ -12,16 +13,16 @@
|
||||
"build": "(tsbuild --web)"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@gitzone/tsbuild": "^2.1.25",
|
||||
"@gitzone/tsrun": "^1.2.12",
|
||||
"@gitzone/tstest": "^1.0.52",
|
||||
"@pushrocks/tapbundle": "^3.2.14",
|
||||
"@types/node": "^14.14.41",
|
||||
"@gitzone/tsbuild": "^2.1.61",
|
||||
"@gitzone/tsrun": "^1.2.32",
|
||||
"@gitzone/tstest": "^1.0.70",
|
||||
"@pushrocks/tapbundle": "^5.0.3",
|
||||
"@types/node": "^17.0.27",
|
||||
"tslint": "^6.1.3",
|
||||
"tslint-config-prettier": "^1.18.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"@types/semver": "^7.3.4",
|
||||
"@types/semver": "^7.3.9",
|
||||
"semver": "^7.3.5"
|
||||
},
|
||||
"files": [
|
||||
|
41
test/test.ts
41
test/test.ts
@ -1,21 +1,36 @@
|
||||
import { expect, tap } from '@pushrocks/tapbundle';
|
||||
import * as smartversion from '../ts/index';
|
||||
import * as smartversion from '../ts/index.js';
|
||||
|
||||
let localSmartVersion: smartversion.SmartVersion;
|
||||
tap.test('should create a valid SmartVersion', async () => {
|
||||
localSmartVersion = new smartversion.SmartVersion('3.2.1');
|
||||
expect(localSmartVersion).to.be.instanceof(smartversion.SmartVersion);
|
||||
const localSmartVersion = new smartversion.SmartVersion('3.2.1');
|
||||
expect(localSmartVersion).toBeInstanceOf(smartversion.SmartVersion);
|
||||
// tslint:disable-next-line:no-unused-expression
|
||||
expect(localSmartVersion.greaterThanString('4.0.0')).toBeFalse();
|
||||
// tslint:disable-next-line:no-unused-expression
|
||||
expect(localSmartVersion.greaterThanString('3.0.0')).toBeTrue();
|
||||
// tslint:disable-next-line:no-unused-expression
|
||||
expect(localSmartVersion.lessThanString('v4.0.0')).toBeTrue();
|
||||
// tslint:disable-next-line:no-unused-expression
|
||||
expect(localSmartVersion.lessThanString('v3.0.0')).toBeFalse();
|
||||
});
|
||||
|
||||
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
|
||||
expect(localSmartVersion.greaterThanString('3.0.0')).to.be.true;
|
||||
// tslint:disable-next-line:no-unused-expression
|
||||
expect(localSmartVersion.lessThanString('v4.0.0')).to.be.true;
|
||||
// tslint:disable-next-line:no-unused-expression
|
||||
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).toBeInstanceOf(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).toEqual('4.7.5');
|
||||
});
|
||||
|
||||
tap.test('should create a patch version', async () => {
|
||||
const versInstance = smartversion.SmartVersion.fromFuzzyString('1.2.3');
|
||||
const newVersion = versInstance.getNewPatchVersion();
|
||||
console.log(newVersion.versionString);
|
||||
})
|
||||
|
||||
tap.start();
|
||||
|
11
ts/00_commitinfo_data.ts
Normal file
11
ts/00_commitinfo_data.ts
Normal file
@ -0,0 +1,11 @@
|
||||
/**
|
||||
* autocreated commitinfo by @pushrocks/commitinfo
|
||||
*/
|
||||
export const name: string = '@pushrocks/smartversion';
|
||||
export const version: string = '3.0.2';
|
||||
export const description: string = 'handle semver with ease'
|
||||
export const commitinfo = {
|
||||
name: '@pushrocks/smartversion',
|
||||
version: '3.0.2',
|
||||
description: 'handle semver with ease'
|
||||
}
|
80
ts/index.ts
80
ts/index.ts
@ -1,25 +1,19 @@
|
||||
import * as plugins from './smartversion.plugins';
|
||||
|
||||
import { SemVer } from 'semver';
|
||||
import * as plugins from './smartversion.plugins.js';
|
||||
|
||||
export class SmartVersion {
|
||||
public semver: SemVer;
|
||||
public versionString: string;
|
||||
public update = {
|
||||
patch: () => {
|
||||
this.semver.patch = this.semver.patch + 1;
|
||||
},
|
||||
minor: () => {
|
||||
this.semver.minor = this.semver.minor + 1;
|
||||
},
|
||||
major: () => {
|
||||
this.semver.major = this.semver.major + 1;
|
||||
},
|
||||
public static fromFuzzyString(fuzzyString: string): SmartVersion {
|
||||
return new SmartVersion(plugins.semver.minVersion(fuzzyString).version, fuzzyString);
|
||||
}
|
||||
|
||||
public originalVersionString: string;
|
||||
public semver: plugins.semver.SemVer;
|
||||
public get versionString(): string {
|
||||
return this.semver.version;
|
||||
};
|
||||
|
||||
constructor(semVerStringArg: string) {
|
||||
constructor(semVerStringArg: string, originalStringArg?: string) {
|
||||
this.originalVersionString = originalStringArg;
|
||||
this.semver = new plugins.semver.SemVer(semVerStringArg);
|
||||
this.versionString = this.semver.version;
|
||||
}
|
||||
|
||||
public get major() {
|
||||
@ -41,7 +35,7 @@ export class SmartVersion {
|
||||
/**
|
||||
* compares the version of this against a string
|
||||
*/
|
||||
public greaterThanString(versionStringArg) {
|
||||
public greaterThanString(versionStringArg: string) {
|
||||
return plugins.semver.gt(this.versionString, versionStringArg);
|
||||
}
|
||||
|
||||
@ -52,7 +46,55 @@ export class SmartVersion {
|
||||
/**
|
||||
* compares the version of this against a string
|
||||
*/
|
||||
public lessThanString(versionStringArg) {
|
||||
public lessThanString(versionStringArg: string) {
|
||||
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;
|
||||
}
|
||||
|
||||
public getNewPatchVersion() {
|
||||
const newInstance = new SmartVersion(`${this.semver.major}.${this.semver.minor}.${this.semver.patch + 1}`);
|
||||
return newInstance;
|
||||
}
|
||||
|
||||
public getNewMinorVersion() {
|
||||
const newInstance = new SmartVersion(`${this.semver.major}.${this.semver.minor + 1}.${0}`);
|
||||
return newInstance;
|
||||
}
|
||||
|
||||
public getNewMajorVersion() {
|
||||
const newInstance = new SmartVersion(`${this.semver.major + 1}.${0}.${0}`);
|
||||
return newInstance;
|
||||
}
|
||||
|
||||
public getNewVersion(typeArg: 'patch' | 'minor' | 'major') {
|
||||
switch (typeArg) {
|
||||
case 'patch':
|
||||
return this.getNewPatchVersion();
|
||||
case 'minor':
|
||||
return this.getNewMinorVersion();
|
||||
case 'major':
|
||||
return this.getNewMajorVersion();
|
||||
default:
|
||||
throw new Error('unknown new version type.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +1,3 @@
|
||||
import * as semver from 'semver';
|
||||
import semver from 'semver';
|
||||
|
||||
export { semver };
|
||||
|
Reference in New Issue
Block a user