@helpers4/version 2.0.0-alpha.1 → 2.0.0-alpha.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/lib/index.d.ts +4 -4
- package/lib/index.js +108 -5
- package/package.json +2 -2
package/README.md
CHANGED
package/lib/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
3
|
-
export
|
|
4
|
-
export
|
|
1
|
+
export declare function stripV(version: string): string; export function stripV(version: null): null; export function stripV(version: undefined): undefined; export function stripV(version: string | null | undefined): string | null | undefined;
|
|
2
|
+
export declare function compare(version1: string, version2: string): number;
|
|
3
|
+
export declare function satisfiesRange(version: string, range: string): boolean;
|
|
4
|
+
export declare function increment(version: string, type: 'major' | 'minor' | 'patch'): string;
|
package/lib/index.js
CHANGED
|
@@ -1,5 +1,108 @@
|
|
|
1
|
-
// helpers/version/
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
// helpers/version/stripV.ts
|
|
2
|
+
function stripV(version) {
|
|
3
|
+
return typeof version === "string" && version.startsWith("v") ? version.slice(1) : version;
|
|
4
|
+
}
|
|
5
|
+
// helpers/version/compare.ts
|
|
6
|
+
function compare(version1, version2) {
|
|
7
|
+
const normalize = (v) => v.replace(/^v/, "");
|
|
8
|
+
const v1 = normalize(version1);
|
|
9
|
+
const v2 = normalize(version2);
|
|
10
|
+
const parts1 = v1.split(".").map(Number);
|
|
11
|
+
const parts2 = v2.split(".").map(Number);
|
|
12
|
+
const maxLength = Math.max(parts1.length, parts2.length);
|
|
13
|
+
for (let i = 0;i < maxLength; i++) {
|
|
14
|
+
const part1 = parts1[i] || 0;
|
|
15
|
+
const part2 = parts2[i] || 0;
|
|
16
|
+
if (part1 < part2)
|
|
17
|
+
return -1;
|
|
18
|
+
if (part1 > part2)
|
|
19
|
+
return 1;
|
|
20
|
+
}
|
|
21
|
+
return 0;
|
|
22
|
+
}
|
|
23
|
+
// helpers/version/satisfiesRange.ts
|
|
24
|
+
function satisfiesRange(version, range) {
|
|
25
|
+
const normalize = (v) => v.replace(/^v/, "");
|
|
26
|
+
const normalizedVersion = normalize(version);
|
|
27
|
+
if (!range.match(/[~^<>=]/)) {
|
|
28
|
+
return normalizedVersion === normalize(range);
|
|
29
|
+
}
|
|
30
|
+
if (range.startsWith(">=")) {
|
|
31
|
+
const targetVersion = normalize(range.slice(2));
|
|
32
|
+
return compareVersionsSimple(normalizedVersion, targetVersion) >= 0;
|
|
33
|
+
}
|
|
34
|
+
if (range.startsWith(">")) {
|
|
35
|
+
const targetVersion = normalize(range.slice(1));
|
|
36
|
+
return compareVersionsSimple(normalizedVersion, targetVersion) > 0;
|
|
37
|
+
}
|
|
38
|
+
if (range.startsWith("<=")) {
|
|
39
|
+
const targetVersion = normalize(range.slice(2));
|
|
40
|
+
return compareVersionsSimple(normalizedVersion, targetVersion) <= 0;
|
|
41
|
+
}
|
|
42
|
+
if (range.startsWith("<")) {
|
|
43
|
+
const targetVersion = normalize(range.slice(1));
|
|
44
|
+
return compareVersionsSimple(normalizedVersion, targetVersion) < 0;
|
|
45
|
+
}
|
|
46
|
+
if (range.startsWith("^")) {
|
|
47
|
+
const targetVersion = normalize(range.slice(1));
|
|
48
|
+
const [targetMajor] = targetVersion.split(".").map(Number);
|
|
49
|
+
const [versionMajor] = normalizedVersion.split(".").map(Number);
|
|
50
|
+
return versionMajor === targetMajor && compareVersionsSimple(normalizedVersion, targetVersion) >= 0;
|
|
51
|
+
}
|
|
52
|
+
if (range.startsWith("~")) {
|
|
53
|
+
const targetVersion = normalize(range.slice(1));
|
|
54
|
+
const [targetMajor, targetMinor] = targetVersion.split(".").map(Number);
|
|
55
|
+
const [versionMajor, versionMinor] = normalizedVersion.split(".").map(Number);
|
|
56
|
+
return versionMajor === targetMajor && versionMinor === targetMinor && compareVersionsSimple(normalizedVersion, targetVersion) >= 0;
|
|
57
|
+
}
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
function compareVersionsSimple(version1, version2) {
|
|
61
|
+
const parts1 = version1.split(".").map(Number);
|
|
62
|
+
const parts2 = version2.split(".").map(Number);
|
|
63
|
+
const maxLength = Math.max(parts1.length, parts2.length);
|
|
64
|
+
for (let i = 0;i < maxLength; i++) {
|
|
65
|
+
const part1 = parts1[i] || 0;
|
|
66
|
+
const part2 = parts2[i] || 0;
|
|
67
|
+
if (part1 < part2)
|
|
68
|
+
return -1;
|
|
69
|
+
if (part1 > part2)
|
|
70
|
+
return 1;
|
|
71
|
+
}
|
|
72
|
+
return 0;
|
|
73
|
+
}
|
|
74
|
+
// helpers/version/increment.ts
|
|
75
|
+
function increment(version, type) {
|
|
76
|
+
const normalize = (v) => v.replace(/^v/, "");
|
|
77
|
+
const hasV = version.startsWith("v");
|
|
78
|
+
const normalizedVersion = normalize(version);
|
|
79
|
+
const parts = normalizedVersion.split(".").map(Number);
|
|
80
|
+
while (parts.length < 3) {
|
|
81
|
+
parts.push(0);
|
|
82
|
+
}
|
|
83
|
+
let [major, minor, patch] = parts;
|
|
84
|
+
switch (type) {
|
|
85
|
+
case "major":
|
|
86
|
+
major++;
|
|
87
|
+
minor = 0;
|
|
88
|
+
patch = 0;
|
|
89
|
+
break;
|
|
90
|
+
case "minor":
|
|
91
|
+
minor++;
|
|
92
|
+
patch = 0;
|
|
93
|
+
break;
|
|
94
|
+
case "patch":
|
|
95
|
+
patch++;
|
|
96
|
+
break;
|
|
97
|
+
default:
|
|
98
|
+
throw new Error(`Invalid increment type: ${type}`);
|
|
99
|
+
}
|
|
100
|
+
const result = `${major}.${minor}.${patch}`;
|
|
101
|
+
return hasV ? `v${result}` : result;
|
|
102
|
+
}
|
|
103
|
+
export {
|
|
104
|
+
stripV,
|
|
105
|
+
satisfiesRange,
|
|
106
|
+
increment,
|
|
107
|
+
compare
|
|
108
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@helpers4/version",
|
|
3
|
-
"version": "2.0.0-alpha.
|
|
3
|
+
"version": "2.0.0-alpha.3",
|
|
4
4
|
"description": "A set of helpers in TS/JS, compatible with tree-shaking, for version.",
|
|
5
5
|
"author": "baxyz <baxy@etik.com>",
|
|
6
6
|
"license": "AGPL-3.0",
|
|
@@ -20,8 +20,8 @@
|
|
|
20
20
|
"keywords": [
|
|
21
21
|
"helpers",
|
|
22
22
|
"version",
|
|
23
|
-
"compare",
|
|
24
23
|
"stripV",
|
|
24
|
+
"compare",
|
|
25
25
|
"satisfiesRange",
|
|
26
26
|
"increment"
|
|
27
27
|
],
|