Skip to content

Commit

Permalink
refactor(versioning/nuget): simplify by using class (#13059)
Browse files Browse the repository at this point in the history
  • Loading branch information
viceice committed Dec 11, 2021
1 parent 62de7b0 commit c96637b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 64 deletions.
11 changes: 8 additions & 3 deletions lib/versioning/loose/generic.ts
Expand Up @@ -104,15 +104,20 @@ export const comparer = (
};
};

// helper functions to ease create other versioning schemas with little code
// especially if those schemas do not support ranges
/**
* helper functions to ease create other versioning schemas with little code
* especially if those schemas do not support ranges
* @deprecated Use `GenericVersioningApi` instead
* @param param0 object with parse and optional compare function
* @returns
*/
export const create = ({
parse,
compare,
}: {
parse: VersionParser;
compare: VersionComparator;
}): any => {
}): VersioningApi => {
let schema: VersioningApi = {} as any;
if (parse) {
schema = { ...schema, ...parser(parse) };
Expand Down
78 changes: 17 additions & 61 deletions lib/versioning/nuget/index.ts
@@ -1,7 +1,6 @@
import semver from 'semver';
import { regEx } from '../../util/regex';
import * as generic from '../loose/generic';
import type { GenericVersion } from '../loose/generic';
import { GenericVersion, GenericVersioningApi } from '../loose/generic';
import type { VersioningApi } from '../types';

export const id = 'nuget';
Expand All @@ -13,71 +12,28 @@ export const supportsRanges = false;

const pattern = regEx(/^(\d+(?:\.\d+)*)(-[^+]+)?(\+.*)?$/);

function parse(version: string): GenericVersion {
const matches = pattern.exec(version);
if (!matches) {
return null;
}
const [, prefix, prerelease] = matches;
const release = prefix.split('.').map(Number);
return { release, prerelease: prerelease || '' };
}

function compareSemVer(version1: string, version2: string): number | null {
const parsed1 = semver.parse(version1);
const parsed2 = semver.parse(version2);

if (!(parsed1 && parsed2)) {
return null;
class NugetVersioningApi extends GenericVersioningApi {
protected _parse(version: string): GenericVersion {
const matches = pattern.exec(version);
if (!matches) {
return null;
}
const [, prefix, prerelease] = matches;
const release = prefix.split('.').map(Number);
return { release, prerelease: prerelease || '' };
}

return parsed1.compare(parsed2);
}

function compareLegacy(version1: string, version2: string): number {
const parsed1 = parse(version1);
const parsed2 = parse(version2);
if (!(parsed1 && parsed2)) {
return 1;
}
protected override _compare(version: string, other: string): number {
const parsed1 = semver.parse(version);
const parsed2 = semver.parse(other);

const length = Math.max(parsed1.release.length, parsed2.release.length);
for (let i = 0; i < length; i += 1) {
// 2.1 and 2.1.0 are equivalent
const part1 = parsed1.release[i] || 0;
const part2 = parsed2.release[i] || 0;
if (part1 !== part2) {
return part1 - part2;
if (!(parsed1 && parsed2)) {
return super._compare(version, other);
}
return parsed1.compare(parsed2);
}
// numeric version equals
const suffixComparison = parsed1.prerelease.localeCompare(parsed2.prerelease);
if (suffixComparison !== 0) {
// Empty suffix should compare greater than non-empty suffix
if (parsed1.prerelease === '') {
return 1;
}
if (parsed2.prerelease === '') {
return -1;
}
}
return suffixComparison;
}

function compare(version1: string, version2: string): number {
const res = compareSemVer(version1, version2);
if (res !== null) {
return res;
}

return compareLegacy(version1, version2);
}

export const api: VersioningApi = {
...generic.create({
parse,
compare,
}),
};
export const api: VersioningApi = new NugetVersioningApi();

export default api;

0 comments on commit c96637b

Please sign in to comment.