@helpers4/version 2.0.0-alpha.1 → 2.0.0-alpha.10

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 CHANGED
@@ -9,13 +9,14 @@ A set of helpers for working with URLs.
9
9
 
10
10
  ## Documentation
11
11
 
12
- [https://helpers4.js.org/version](https://helpers4.js.org/version)
12
+ [https://helpers4.dev/typescript/categories/version/](https://helpers4.dev/typescript/categories/version/)
13
13
 
14
14
  <!-- AUTOMATIC-METHODS -->
15
15
  - compare
16
- - stripV
17
- - satisfiesRange
18
16
  - increment
17
+ - parse
18
+ - satisfiesRange
19
+ - stripV
19
20
  <!-- /AUTOMATIC-METHODS -->
20
21
 
21
22
  ## See
@@ -24,6 +25,7 @@ A set of helpers for working with URLs.
24
25
  - [array](../array)
25
26
  - [date](../date)
26
27
  - [function](../function)
28
+ - [math](../math)
27
29
  - [number](../number)
28
30
  - [object](../object)
29
31
  - [observable](../observable)
@@ -41,6 +43,7 @@ A set of helpers for working with URLs.
41
43
  | array | [@helpers4/array](https://www.npmjs.com/package/@helpers4/array) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/array) | Array manipulation utilities |
42
44
  | date | [@helpers4/date](https://www.npmjs.com/package/@helpers4/date) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/date) | date utilities |
43
45
  | function | [@helpers4/function](https://www.npmjs.com/package/@helpers4/function) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/function) | Function utilities and type guards |
46
+ | math | [@helpers4/math](https://www.npmjs.com/package/@helpers4/math) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/math) | math utilities |
44
47
  | number | [@helpers4/number](https://www.npmjs.com/package/@helpers4/number) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/number) | number utilities |
45
48
  | object | [@helpers4/object](https://www.npmjs.com/package/@helpers4/object) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/object) | Object manipulation utilities |
46
49
  | observable | [@helpers4/observable](https://www.npmjs.com/package/@helpers4/observable) | [Source](https://github.com/helpers4/helpers4/tree/main/helpers/observable) | Observable utilities and combinators |
package/lib/index.d.ts CHANGED
@@ -1,4 +1,125 @@
1
- export * from './compare';
2
- export * from './stripV';
3
- export * from './satisfiesRange';
4
- export * from './increment';
1
+ /**
2
+ * This file is part of helpers4.
3
+ * Copyright (C) 2025 baxyz
4
+ * SPDX-License-Identifier: LGPL-3.0-or-later
5
+ */
6
+ /**
7
+ * Compares two semantic version strings according to SemVer 2.0.0 specification
8
+ *
9
+ * Supports:
10
+ * - Core version: MAJOR.MINOR.PATCH
11
+ * - Pre-release: -alpha, -beta.1, -rc.1, etc.
12
+ * - Build metadata: +build, +sha.abc123 (ignored in comparison per spec)
13
+ * - Optional 'v' prefix
14
+ *
15
+ * @param version1 - First version string
16
+ * @param version2 - Second version string
17
+ * @returns -1 if version1 < version2, 0 if equal, 1 if version1 > version2
18
+ * @example
19
+ * compare('1.0.0', '2.0.0') // -1
20
+ * compare('1.0.0-alpha', '1.0.0') // -1 (prerelease < release)
21
+ * compare('1.0.0-alpha', '1.0.0-beta') // -1
22
+ * compare('1.0.0-alpha.1', '1.0.0-alpha.2') // -1
23
+ * compare('1.0.0+build1', '1.0.0+build2') // 0 (build metadata ignored)
24
+ * @since 1.9.0
25
+ */
26
+ declare function compare(version1: string, version2: string): number;
27
+
28
+ /**
29
+ * This file is part of helpers4.
30
+ * Copyright (C) 2025 baxyz
31
+ * SPDX-License-Identifier: LGPL-3.0-or-later
32
+ */
33
+ /**
34
+ * Increments a semantic version
35
+ * @param version - The version to increment
36
+ * @param type - The increment type ('major', 'minor', 'patch')
37
+ * @returns Incremented version string
38
+ * @since 1.9.0
39
+ */
40
+ declare function increment(version: string, type: 'major' | 'minor' | 'patch'): string;
41
+
42
+ /**
43
+ * This file is part of helpers4.
44
+ * Copyright (C) 2025 baxyz
45
+ * SPDX-License-Identifier: LGPL-3.0-or-later
46
+ */
47
+ /**
48
+ * Represents a parsed semantic version according to SemVer 2.0.0 specification
49
+ * @since 2.0.0
50
+ */
51
+ interface ParsedVersion {
52
+ /** Major version number */
53
+ major: number;
54
+ /** Minor version number */
55
+ minor: number;
56
+ /** Patch version number */
57
+ patch: number;
58
+ /** Pre-release identifiers (e.g., ['alpha', '1'] for -alpha.1) */
59
+ prerelease: string[];
60
+ /** Build metadata identifiers (e.g., ['build', '123'] for +build.123) */
61
+ build: string[];
62
+ }
63
+ /**
64
+ * Parses a semantic version string into its components according to SemVer 2.0.0 specification
65
+ *
66
+ * Supports:
67
+ * - Core version: MAJOR.MINOR.PATCH
68
+ * - Pre-release: -alpha, -beta.1, -rc.1, -0.3.7, -x.7.z.92
69
+ * - Build metadata: +build, +sha.abc123, +20130313144700
70
+ * - Optional 'v' prefix (commonly used in git tags)
71
+ *
72
+ * @param version - Version string to parse
73
+ * @returns Parsed version object with major, minor, patch, prerelease, and build
74
+ * @example
75
+ * parse('1.2.3') // { major: 1, minor: 2, patch: 3, prerelease: [], build: [] }
76
+ * parse('v1.0.0-alpha.1') // { major: 1, minor: 0, patch: 0, prerelease: ['alpha', '1'], build: [] }
77
+ * parse('2.0.0+build.123') // { major: 2, minor: 0, patch: 0, prerelease: [], build: ['build', '123'] }
78
+ * parse('1.0.0-beta+exp.sha.5114f85') // { major: 1, minor: 0, patch: 0, prerelease: ['beta'], build: ['exp', 'sha', '5114f85'] }
79
+ * @since 2.0.0
80
+ */
81
+ declare function parse(version: string): ParsedVersion;
82
+
83
+ /**
84
+ * This file is part of helpers4.
85
+ * Copyright (C) 2025 baxyz
86
+ * SPDX-License-Identifier: LGPL-3.0-or-later
87
+ */
88
+ /**
89
+ * Checks if a version satisfies a range (simple implementation)
90
+ * @param version - Version to check
91
+ * @param range - Range pattern (e.g., ">=1.0.0", "~1.2.0", "^1.0.0")
92
+ * @returns True if version satisfies the range
93
+ * @since 1.9.0
94
+ */
95
+ declare function satisfiesRange(version: string, range: string): boolean;
96
+
97
+ /**
98
+ * This file is part of helpers4.
99
+ * Copyright (C) 2025 baxyz
100
+ * SPDX-License-Identifier: LGPL-3.0-or-later
101
+ */
102
+ /**
103
+ * Strip the leading "v" from a version string if it exists.
104
+ *
105
+ * @param version - The version string to process
106
+ * @returns The version string without leading "v", or the original value if it's not a string or doesn't start with "v"
107
+ *
108
+ * @example
109
+ * ```typescript
110
+ * stripV("v1.2.3") // "1.2.3"
111
+ * stripV("1.2.3") // "1.2.3"
112
+ * stripV("v20.1.0") // "20.1.0"
113
+ * stripV(null) // null
114
+ * stripV(undefined) // undefined
115
+ * stripV("") // ""
116
+ * stripV("1.0.0-beta") // "1.0.0-beta"
117
+ * ```
118
+ * @since 1.9.0
119
+ */
120
+ declare function stripV(version: string): string;
121
+ declare function stripV(version: null): null;
122
+ declare function stripV(version: undefined): undefined;
123
+
124
+ export { compare, increment, parse, satisfiesRange, stripV };
125
+ export type { ParsedVersion };
package/lib/index.js CHANGED
@@ -1,5 +1,199 @@
1
- // helpers/version/index.ts
2
- export * from "./compare";
3
- export * from "./stripV";
4
- export * from "./satisfiesRange";
5
- export * from "./increment";
1
+ //#region helpers/version/parse.ts
2
+ /**
3
+ * Parses a semantic version string into its components according to SemVer 2.0.0 specification
4
+ *
5
+ * Supports:
6
+ * - Core version: MAJOR.MINOR.PATCH
7
+ * - Pre-release: -alpha, -beta.1, -rc.1, -0.3.7, -x.7.z.92
8
+ * - Build metadata: +build, +sha.abc123, +20130313144700
9
+ * - Optional 'v' prefix (commonly used in git tags)
10
+ *
11
+ * @param version - Version string to parse
12
+ * @returns Parsed version object with major, minor, patch, prerelease, and build
13
+ * @example
14
+ * parse('1.2.3') // { major: 1, minor: 2, patch: 3, prerelease: [], build: [] }
15
+ * parse('v1.0.0-alpha.1') // { major: 1, minor: 0, patch: 0, prerelease: ['alpha', '1'], build: [] }
16
+ * parse('2.0.0+build.123') // { major: 2, minor: 0, patch: 0, prerelease: [], build: ['build', '123'] }
17
+ * parse('1.0.0-beta+exp.sha.5114f85') // { major: 1, minor: 0, patch: 0, prerelease: ['beta'], build: ['exp', 'sha', '5114f85'] }
18
+ * @since 2.0.0
19
+ */
20
+ function parse(version) {
21
+ const [versionWithPrerelease, buildString] = version.replace(/^v/, "").split("+");
22
+ const build = buildString ? buildString.split(".") : [];
23
+ const [coreVersion, prereleaseString] = versionWithPrerelease.split("-");
24
+ const prerelease = prereleaseString ? prereleaseString.split(".") : [];
25
+ const parts = coreVersion.split(".").map(Number);
26
+ return {
27
+ major: parts[0] || 0,
28
+ minor: parts[1] || 0,
29
+ patch: parts[2] || 0,
30
+ prerelease,
31
+ build
32
+ };
33
+ }
34
+ //#endregion
35
+ //#region helpers/version/compare.ts
36
+ /**
37
+ * This file is part of helpers4.
38
+ * Copyright (C) 2025 baxyz
39
+ * SPDX-License-Identifier: LGPL-3.0-or-later
40
+ */
41
+ /**
42
+ * Compares two prerelease identifier arrays according to SemVer spec
43
+ * Rules:
44
+ * - Numeric identifiers are compared as integers
45
+ * - Alphanumeric identifiers are compared lexically (ASCII)
46
+ * - Numeric identifiers have lower precedence than alphanumeric
47
+ * - A larger set of prerelease fields has higher precedence if all preceding are equal
48
+ * @param pre1 - First prerelease array
49
+ * @param pre2 - Second prerelease array
50
+ * @returns -1, 0, or 1
51
+ */
52
+ function comparePrerelease(pre1, pre2) {
53
+ if (pre1.length === 0 && pre2.length === 0) return 0;
54
+ if (pre1.length === 0) return 1;
55
+ if (pre2.length === 0) return -1;
56
+ const maxLength = Math.max(pre1.length, pre2.length);
57
+ for (let i = 0; i < maxLength; i++) {
58
+ if (i >= pre1.length) return -1;
59
+ if (i >= pre2.length) return 1;
60
+ const id1 = pre1[i];
61
+ const id2 = pre2[i];
62
+ const isNum1 = /^\d+$/.test(id1);
63
+ const isNum2 = /^\d+$/.test(id2);
64
+ if (isNum1 && isNum2) {
65
+ const num1 = parseInt(id1, 10);
66
+ const num2 = parseInt(id2, 10);
67
+ if (num1 < num2) return -1;
68
+ if (num1 > num2) return 1;
69
+ } else if (isNum1) return -1;
70
+ else if (isNum2) return 1;
71
+ else {
72
+ if (id1 < id2) return -1;
73
+ if (id1 > id2) return 1;
74
+ }
75
+ }
76
+ return 0;
77
+ }
78
+ /**
79
+ * Compares two semantic version strings according to SemVer 2.0.0 specification
80
+ *
81
+ * Supports:
82
+ * - Core version: MAJOR.MINOR.PATCH
83
+ * - Pre-release: -alpha, -beta.1, -rc.1, etc.
84
+ * - Build metadata: +build, +sha.abc123 (ignored in comparison per spec)
85
+ * - Optional 'v' prefix
86
+ *
87
+ * @param version1 - First version string
88
+ * @param version2 - Second version string
89
+ * @returns -1 if version1 < version2, 0 if equal, 1 if version1 > version2
90
+ * @example
91
+ * compare('1.0.0', '2.0.0') // -1
92
+ * compare('1.0.0-alpha', '1.0.0') // -1 (prerelease < release)
93
+ * compare('1.0.0-alpha', '1.0.0-beta') // -1
94
+ * compare('1.0.0-alpha.1', '1.0.0-alpha.2') // -1
95
+ * compare('1.0.0+build1', '1.0.0+build2') // 0 (build metadata ignored)
96
+ * @since 1.9.0
97
+ */
98
+ function compare(version1, version2) {
99
+ const v1 = parse(version1);
100
+ const v2 = parse(version2);
101
+ if (v1.major !== v2.major) return v1.major < v2.major ? -1 : 1;
102
+ if (v1.minor !== v2.minor) return v1.minor < v2.minor ? -1 : 1;
103
+ if (v1.patch !== v2.patch) return v1.patch < v2.patch ? -1 : 1;
104
+ return comparePrerelease(v1.prerelease, v2.prerelease);
105
+ }
106
+ //#endregion
107
+ //#region helpers/version/increment.ts
108
+ /**
109
+ * This file is part of helpers4.
110
+ * Copyright (C) 2025 baxyz
111
+ * SPDX-License-Identifier: LGPL-3.0-or-later
112
+ */
113
+ /**
114
+ * Increments a semantic version
115
+ * @param version - The version to increment
116
+ * @param type - The increment type ('major', 'minor', 'patch')
117
+ * @returns Incremented version string
118
+ * @since 1.9.0
119
+ */
120
+ function increment(version, type) {
121
+ const normalize = (v) => v.replace(/^v/, "");
122
+ const hasV = version.startsWith("v");
123
+ const parts = normalize(version).split(".").map(Number);
124
+ while (parts.length < 3) parts.push(0);
125
+ let [major, minor, patch] = parts;
126
+ switch (type) {
127
+ case "major":
128
+ major++;
129
+ minor = 0;
130
+ patch = 0;
131
+ break;
132
+ case "minor":
133
+ minor++;
134
+ patch = 0;
135
+ break;
136
+ case "patch":
137
+ patch++;
138
+ break;
139
+ default: throw new Error(`Invalid increment type: ${type}`);
140
+ }
141
+ const result = `${major}.${minor}.${patch}`;
142
+ return hasV ? `v${result}` : result;
143
+ }
144
+ //#endregion
145
+ //#region helpers/version/satisfiesRange.ts
146
+ /**
147
+ * This file is part of helpers4.
148
+ * Copyright (C) 2025 baxyz
149
+ * SPDX-License-Identifier: LGPL-3.0-or-later
150
+ */
151
+ /**
152
+ * Checks if a version satisfies a range (simple implementation)
153
+ * @param version - Version to check
154
+ * @param range - Range pattern (e.g., ">=1.0.0", "~1.2.0", "^1.0.0")
155
+ * @returns True if version satisfies the range
156
+ * @since 1.9.0
157
+ */
158
+ function satisfiesRange(version, range) {
159
+ const normalize = (v) => v.replace(/^v/, "");
160
+ const normalizedVersion = normalize(version);
161
+ if (!range.match(/[~^<>=]/)) return normalizedVersion === normalize(range);
162
+ if (range.startsWith(">=")) return compareVersionsSimple(normalizedVersion, normalize(range.slice(2))) >= 0;
163
+ if (range.startsWith(">")) return compareVersionsSimple(normalizedVersion, normalize(range.slice(1))) > 0;
164
+ if (range.startsWith("<=")) return compareVersionsSimple(normalizedVersion, normalize(range.slice(2))) <= 0;
165
+ if (range.startsWith("<")) return compareVersionsSimple(normalizedVersion, normalize(range.slice(1))) < 0;
166
+ if (range.startsWith("^")) {
167
+ const targetVersion = normalize(range.slice(1));
168
+ const [targetMajor] = targetVersion.split(".").map(Number);
169
+ const [versionMajor] = normalizedVersion.split(".").map(Number);
170
+ return versionMajor === targetMajor && compareVersionsSimple(normalizedVersion, targetVersion) >= 0;
171
+ }
172
+ if (range.startsWith("~")) {
173
+ const targetVersion = normalize(range.slice(1));
174
+ const [targetMajor, targetMinor] = targetVersion.split(".").map(Number);
175
+ const [versionMajor, versionMinor] = normalizedVersion.split(".").map(Number);
176
+ return versionMajor === targetMajor && versionMinor === targetMinor && compareVersionsSimple(normalizedVersion, targetVersion) >= 0;
177
+ } else return false;
178
+ }
179
+ function compareVersionsSimple(version1, version2) {
180
+ const parts1 = version1.split(".").map(Number);
181
+ const parts2 = version2.split(".").map(Number);
182
+ const maxLength = Math.max(parts1.length, parts2.length);
183
+ for (let i = 0; i < maxLength; i++) {
184
+ const part1 = parts1[i] || 0;
185
+ const part2 = parts2[i] || 0;
186
+ if (part1 < part2) return -1;
187
+ if (part1 > part2) return 1;
188
+ }
189
+ return 0;
190
+ }
191
+ //#endregion
192
+ //#region helpers/version/stripV.ts
193
+ function stripV(version) {
194
+ return typeof version === "string" && version.startsWith("v") ? version.slice(1) : version;
195
+ }
196
+ //#endregion
197
+ export { compare, increment, parse, satisfiesRange, stripV };
198
+
199
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":[],"sources":["../../../helpers/version/parse.ts","../../../helpers/version/compare.ts","../../../helpers/version/increment.ts","../../../helpers/version/satisfiesRange.ts","../../../helpers/version/stripV.ts"],"sourcesContent":["/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/**\n * Represents a parsed semantic version according to SemVer 2.0.0 specification\n * @since 2.0.0\n */\nexport interface ParsedVersion {\n /** Major version number */\n major: number;\n /** Minor version number */\n minor: number;\n /** Patch version number */\n patch: number;\n /** Pre-release identifiers (e.g., ['alpha', '1'] for -alpha.1) */\n prerelease: string[];\n /** Build metadata identifiers (e.g., ['build', '123'] for +build.123) */\n build: string[];\n}\n\n/**\n * Parses a semantic version string into its components according to SemVer 2.0.0 specification\n *\n * Supports:\n * - Core version: MAJOR.MINOR.PATCH\n * - Pre-release: -alpha, -beta.1, -rc.1, -0.3.7, -x.7.z.92\n * - Build metadata: +build, +sha.abc123, +20130313144700\n * - Optional 'v' prefix (commonly used in git tags)\n *\n * @param version - Version string to parse\n * @returns Parsed version object with major, minor, patch, prerelease, and build\n * @example\n * parse('1.2.3') // { major: 1, minor: 2, patch: 3, prerelease: [], build: [] }\n * parse('v1.0.0-alpha.1') // { major: 1, minor: 0, patch: 0, prerelease: ['alpha', '1'], build: [] }\n * parse('2.0.0+build.123') // { major: 2, minor: 0, patch: 0, prerelease: [], build: ['build', '123'] }\n * parse('1.0.0-beta+exp.sha.5114f85') // { major: 1, minor: 0, patch: 0, prerelease: ['beta'], build: ['exp', 'sha', '5114f85'] }\n * @since 2.0.0\n */\nexport function parse(version: string): ParsedVersion {\n // Remove optional 'v' prefix\n const normalized = version.replace(/^v/, '');\n\n // Split build metadata first (everything after +)\n const [versionWithPrerelease, buildString] = normalized.split('+');\n const build = buildString ? buildString.split('.') : [];\n\n // Split prerelease (everything after -)\n const [coreVersion, prereleaseString] = versionWithPrerelease.split('-');\n const prerelease = prereleaseString ? prereleaseString.split('.') : [];\n\n // Parse core version\n const parts = coreVersion.split('.').map(Number);\n\n return {\n major: parts[0] || 0,\n minor: parts[1] || 0,\n patch: parts[2] || 0,\n prerelease,\n build,\n };\n}\n","/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\nimport { parse } from './parse';\n\n/**\n * Compares two prerelease identifier arrays according to SemVer spec\n * Rules:\n * - Numeric identifiers are compared as integers\n * - Alphanumeric identifiers are compared lexically (ASCII)\n * - Numeric identifiers have lower precedence than alphanumeric\n * - A larger set of prerelease fields has higher precedence if all preceding are equal\n * @param pre1 - First prerelease array\n * @param pre2 - Second prerelease array\n * @returns -1, 0, or 1\n */\nfunction comparePrerelease(pre1: string[], pre2: string[]): number {\n // No prerelease has higher precedence than prerelease\n // e.g., 1.0.0 > 1.0.0-alpha\n if (pre1.length === 0 && pre2.length === 0) return 0;\n if (pre1.length === 0) return 1; // No prerelease > prerelease\n if (pre2.length === 0) return -1; // prerelease < no prerelease\n\n const maxLength = Math.max(pre1.length, pre2.length);\n\n for (let i = 0; i < maxLength; i++) {\n // A larger set has higher precedence if all preceding are equal\n if (i >= pre1.length) return -1;\n if (i >= pre2.length) return 1;\n\n const id1 = pre1[i];\n const id2 = pre2[i];\n\n const isNum1 = /^\\d+$/.test(id1);\n const isNum2 = /^\\d+$/.test(id2);\n\n // Both numeric: compare as integers\n if (isNum1 && isNum2) {\n const num1 = parseInt(id1, 10);\n const num2 = parseInt(id2, 10);\n if (num1 < num2) return -1;\n if (num1 > num2) return 1;\n // num1 === num2, continue to next identifier\n }\n // Numeric has lower precedence than alphanumeric\n else if (isNum1) {\n return -1;\n } else if (isNum2) {\n return 1;\n }\n // Both alphanumeric: compare lexically (ASCII sort)\n else {\n if (id1 < id2) return -1;\n if (id1 > id2) return 1;\n // id1 === id2, continue to next identifier\n }\n }\n\n return 0;\n}\n\n/**\n * Compares two semantic version strings according to SemVer 2.0.0 specification\n *\n * Supports:\n * - Core version: MAJOR.MINOR.PATCH\n * - Pre-release: -alpha, -beta.1, -rc.1, etc.\n * - Build metadata: +build, +sha.abc123 (ignored in comparison per spec)\n * - Optional 'v' prefix\n *\n * @param version1 - First version string\n * @param version2 - Second version string\n * @returns -1 if version1 < version2, 0 if equal, 1 if version1 > version2\n * @example\n * compare('1.0.0', '2.0.0') // -1\n * compare('1.0.0-alpha', '1.0.0') // -1 (prerelease < release)\n * compare('1.0.0-alpha', '1.0.0-beta') // -1\n * compare('1.0.0-alpha.1', '1.0.0-alpha.2') // -1\n * compare('1.0.0+build1', '1.0.0+build2') // 0 (build metadata ignored)\n * @since 1.9.0\n */\nexport function compare(version1: string, version2: string): number {\n const v1 = parse(version1);\n const v2 = parse(version2);\n\n // Compare major, minor, patch\n if (v1.major !== v2.major) return v1.major < v2.major ? -1 : 1;\n if (v1.minor !== v2.minor) return v1.minor < v2.minor ? -1 : 1;\n if (v1.patch !== v2.patch) return v1.patch < v2.patch ? -1 : 1;\n\n // Compare prerelease (build metadata is ignored per SemVer spec)\n return comparePrerelease(v1.prerelease, v2.prerelease);\n}\n","/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/**\n * Increments a semantic version\n * @param version - The version to increment\n * @param type - The increment type ('major', 'minor', 'patch')\n * @returns Incremented version string\n * @since 1.9.0\n */\nexport function increment(\n version: string,\n type: 'major' | 'minor' | 'patch'\n): string {\n const normalize = (v: string) => v.replace(/^v/, '');\n const hasV = version.startsWith('v');\n const normalizedVersion = normalize(version);\n\n const parts = normalizedVersion.split('.').map(Number);\n\n // Ensure we have at least major.minor.patch\n while (parts.length < 3) {\n parts.push(0);\n }\n\n let [major, minor, patch] = parts;\n\n switch (type) {\n case 'major':\n major++;\n minor = 0;\n patch = 0;\n break;\n case 'minor':\n minor++;\n patch = 0;\n break;\n case 'patch':\n patch++;\n break;\n default:\n throw new Error(`Invalid increment type: ${type}`);\n }\n\n const result = `${major}.${minor}.${patch}`;\n return hasV ? `v${result}` : result;\n}\n","/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/**\n * Checks if a version satisfies a range (simple implementation)\n * @param version - Version to check\n * @param range - Range pattern (e.g., \">=1.0.0\", \"~1.2.0\", \"^1.0.0\")\n * @returns True if version satisfies the range\n * @since 1.9.0\n */\nexport function satisfiesRange(version: string, range: string): boolean {\n const normalize = (v: string) => v.replace(/^v/, '');\n const normalizedVersion = normalize(version);\n\n // Handle exact match\n if (!range.match(/[~^<>=]/)) {\n return normalizedVersion === normalize(range);\n }\n\n // Handle >= operator\n if (range.startsWith('>=')) {\n const targetVersion = normalize(range.slice(2));\n return compareVersionsSimple(normalizedVersion, targetVersion) >= 0;\n }\n\n // Handle > operator\n if (range.startsWith('>')) {\n const targetVersion = normalize(range.slice(1));\n return compareVersionsSimple(normalizedVersion, targetVersion) > 0;\n }\n\n // Handle <= operator\n if (range.startsWith('<=')) {\n const targetVersion = normalize(range.slice(2));\n return compareVersionsSimple(normalizedVersion, targetVersion) <= 0;\n }\n\n // Handle < operator\n if (range.startsWith('<')) {\n const targetVersion = normalize(range.slice(1));\n return compareVersionsSimple(normalizedVersion, targetVersion) < 0;\n }\n\n // Handle caret range (^1.2.3 allows patch and minor updates)\n if (range.startsWith('^')) {\n const targetVersion = normalize(range.slice(1));\n const [targetMajor] = targetVersion.split('.').map(Number);\n const [versionMajor] = normalizedVersion.split('.').map(Number);\n\n return versionMajor === targetMajor &&\n compareVersionsSimple(normalizedVersion, targetVersion) >= 0;\n }\n\n // Handle tilde range (~1.2.3 allows patch updates)\n if (range.startsWith('~')) {\n const targetVersion = normalize(range.slice(1));\n const [targetMajor, targetMinor] = targetVersion.split('.').map(Number);\n const [versionMajor, versionMinor] = normalizedVersion.split('.').map(Number);\n\n return versionMajor === targetMajor &&\n versionMinor === targetMinor &&\n compareVersionsSimple(normalizedVersion, targetVersion) >= 0;\n } else {\n // Unsupported range format\n return false;\n }\n}\n\nfunction compareVersionsSimple(version1: string, version2: string): number {\n const parts1 = version1.split('.').map(Number);\n const parts2 = version2.split('.').map(Number);\n\n const maxLength = Math.max(parts1.length, parts2.length);\n\n for (let i = 0; i < maxLength; i++) {\n const part1 = parts1[i] || 0;\n const part2 = parts2[i] || 0;\n\n if (part1 < part2) return -1;\n if (part1 > part2) return 1;\n }\n\n return 0;\n}\n","/**\n * This file is part of helpers4.\n * Copyright (C) 2025 baxyz\n * SPDX-License-Identifier: LGPL-3.0-or-later\n */\n\n/**\n * Strip the leading \"v\" from a version string if it exists.\n * \n * @param version - The version string to process\n * @returns The version string without leading \"v\", or the original value if it's not a string or doesn't start with \"v\"\n * \n * @example\n * ```typescript\n * stripV(\"v1.2.3\") // \"1.2.3\"\n * stripV(\"1.2.3\") // \"1.2.3\"\n * stripV(\"v20.1.0\") // \"20.1.0\"\n * stripV(null) // null\n * stripV(undefined) // undefined\n * stripV(\"\") // \"\"\n * stripV(\"1.0.0-beta\") // \"1.0.0-beta\"\n * ```\n * @since 1.9.0\n */\nexport function stripV(version: string): string;\nexport function stripV(version: null): null;\nexport function stripV(version: undefined): undefined;\nexport function stripV(version: string | null | undefined): string | null | undefined {\n return typeof version === \"string\" && version.startsWith(\"v\") ? version.slice(1) : version;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAyCA,SAAgB,MAAM,SAAgC;CAKpD,MAAM,CAAC,uBAAuB,eAHX,QAAQ,QAAQ,MAAM,GAAG,CAGY,MAAM,IAAI;CAClE,MAAM,QAAQ,cAAc,YAAY,MAAM,IAAI,GAAG,EAAE;CAGvD,MAAM,CAAC,aAAa,oBAAoB,sBAAsB,MAAM,IAAI;CACxE,MAAM,aAAa,mBAAmB,iBAAiB,MAAM,IAAI,GAAG,EAAE;CAGtE,MAAM,QAAQ,YAAY,MAAM,IAAI,CAAC,IAAI,OAAO;AAEhD,QAAO;EACL,OAAO,MAAM,MAAM;EACnB,OAAO,MAAM,MAAM;EACnB,OAAO,MAAM,MAAM;EACnB;EACA;EACD;;;;;;;;;;;;;;;;;;;;AC3CH,SAAS,kBAAkB,MAAgB,MAAwB;AAGjE,KAAI,KAAK,WAAW,KAAK,KAAK,WAAW,EAAG,QAAO;AACnD,KAAI,KAAK,WAAW,EAAG,QAAO;AAC9B,KAAI,KAAK,WAAW,EAAG,QAAO;CAE9B,MAAM,YAAY,KAAK,IAAI,KAAK,QAAQ,KAAK,OAAO;AAEpD,MAAK,IAAI,IAAI,GAAG,IAAI,WAAW,KAAK;AAElC,MAAI,KAAK,KAAK,OAAQ,QAAO;AAC7B,MAAI,KAAK,KAAK,OAAQ,QAAO;EAE7B,MAAM,MAAM,KAAK;EACjB,MAAM,MAAM,KAAK;EAEjB,MAAM,SAAS,QAAQ,KAAK,IAAI;EAChC,MAAM,SAAS,QAAQ,KAAK,IAAI;AAGhC,MAAI,UAAU,QAAQ;GACpB,MAAM,OAAO,SAAS,KAAK,GAAG;GAC9B,MAAM,OAAO,SAAS,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Y,KAAK,IAAI,OAAO,QAAQ,OAAO,OAAO;AAExD,MAAK,IAAI,IAAI,GAAG,IAAI,WAAW,KAAK;EAClC,MAAM,QAAQ,OAAO,MAAM;EAC3B,MAAM,QAAQ,OAAO,MAAM;AAE3B,MAAI,QAAQ,MAAO,QAAO;AAC1B,MAAI,QAAQ,MAAO,QAAO;;AAG5B,QAAO;;;;AC1DT,SAAgB,OAAO,SAA+D;AACpF,QAAO,OAAO,YAAY,YAAY,QAAQ,WAAW,IAAI,GAAG,QAAQ,MAAM,EAAE,GAAG"}
package/meta/api.json ADDED
@@ -0,0 +1,250 @@
1
+ {
2
+ "category": "version",
3
+ "version": "2.0.0-alpha.10",
4
+ "functions": [
5
+ {
6
+ "name": "compare",
7
+ "kind": "function",
8
+ "description": "Compares two semantic version strings according to SemVer 2.0.0 specification\n\nSupports:\n- Core version: MAJOR.MINOR.PATCH\n- Pre-release: -alpha, -beta.1, -rc.1, etc.\n- Build metadata: +build, +sha.abc123 (ignored in comparison per spec)\n- Optional 'v' prefix",
9
+ "since": "1.9.0",
10
+ "signatures": [
11
+ {
12
+ "signature": "compare(version1: string, version2: string): number",
13
+ "description": "Compares two semantic version strings according to SemVer 2.0.0 specification\n\nSupports:\n- Core version: MAJOR.MINOR.PATCH\n- Pre-release: -alpha, -beta.1, -rc.1, etc.\n- Build metadata: +build, +sha.abc123 (ignored in comparison per spec)\n- Optional 'v' prefix",
14
+ "params": [
15
+ {
16
+ "name": "version1",
17
+ "type": "string",
18
+ "description": "First version string"
19
+ },
20
+ {
21
+ "name": "version2",
22
+ "type": "string",
23
+ "description": "Second version string"
24
+ }
25
+ ],
26
+ "returns": {
27
+ "type": "number",
28
+ "description": "-1 if version1 < version2, 0 if equal, 1 if version1 > version2"
29
+ }
30
+ }
31
+ ],
32
+ "examples": [
33
+ {
34
+ "title": "Compare two semver versions",
35
+ "description": "Returns -1, 0, or 1 based on SemVer ordering.",
36
+ "code": "compare('1.0.0', '2.0.0') // => -1\ncompare('1.0.0', '1.0.0') // => 0\ncompare('2.0.0', '1.0.0') // => 1"
37
+ },
38
+ {
39
+ "title": "Prerelease is lower than release",
40
+ "description": "A prerelease version is always less than the release.",
41
+ "code": "compare('1.0.0-alpha', '1.0.0')\n// => -1"
42
+ }
43
+ ],
44
+ "sourceFile": "compare.ts"
45
+ },
46
+ {
47
+ "name": "increment",
48
+ "kind": "function",
49
+ "description": "Increments a semantic version",
50
+ "since": "1.9.0",
51
+ "signatures": [
52
+ {
53
+ "signature": "increment(version: string, type: \"major\" | \"minor\" | \"patch\"): string",
54
+ "description": "Increments a semantic version",
55
+ "params": [
56
+ {
57
+ "name": "version",
58
+ "type": "string",
59
+ "description": "The version to increment"
60
+ },
61
+ {
62
+ "name": "type",
63
+ "type": "\"major\" | \"minor\" | \"patch\"",
64
+ "description": "The increment type ('major', 'minor', 'patch')"
65
+ }
66
+ ],
67
+ "returns": {
68
+ "type": "string",
69
+ "description": "Incremented version string"
70
+ }
71
+ }
72
+ ],
73
+ "examples": [
74
+ {
75
+ "title": "Increment the patch version",
76
+ "description": "Bumps the patch number while keeping major and minor.",
77
+ "code": "increment('1.2.3', 'patch')\n// => '1.2.4'"
78
+ },
79
+ {
80
+ "title": "Increment the minor version",
81
+ "description": "Bumps the minor number and resets patch to 0.",
82
+ "code": "increment('1.2.3', 'minor')\n// => '1.3.0'"
83
+ },
84
+ {
85
+ "title": "Preserve the v prefix",
86
+ "description": "The v prefix is preserved if present in the input.",
87
+ "code": "increment('v1.0.0', 'major')\n// => 'v2.0.0'"
88
+ }
89
+ ],
90
+ "sourceFile": "increment.ts"
91
+ },
92
+ {
93
+ "name": "parse",
94
+ "kind": "function",
95
+ "description": "Parses a semantic version string into its components according to SemVer 2.0.0 specification\n\nSupports:\n- Core version: MAJOR.MINOR.PATCH\n- Pre-release: -alpha, -beta.1, -rc.1, -0.3.7, -x.7.z.92\n- Build metadata: +build, +sha.abc123, +20130313144700\n- Optional 'v' prefix (commonly used in git tags)",
96
+ "since": "2.0.0",
97
+ "signatures": [
98
+ {
99
+ "signature": "parse(version: string): ParsedVersion",
100
+ "description": "Parses a semantic version string into its components according to SemVer 2.0.0 specification\n\nSupports:\n- Core version: MAJOR.MINOR.PATCH\n- Pre-release: -alpha, -beta.1, -rc.1, -0.3.7, -x.7.z.92\n- Build metadata: +build, +sha.abc123, +20130313144700\n- Optional 'v' prefix (commonly used in git tags)",
101
+ "params": [
102
+ {
103
+ "name": "version",
104
+ "type": "string",
105
+ "description": "Version string to parse"
106
+ }
107
+ ],
108
+ "returns": {
109
+ "type": "ParsedVersion",
110
+ "description": "Parsed version object with major, minor, patch, prerelease, and build"
111
+ }
112
+ }
113
+ ],
114
+ "examples": [
115
+ {
116
+ "title": "Parse a semver string",
117
+ "description": "Breaks a semantic version string into its components.",
118
+ "code": "parse('1.2.3')\n// => { major: 1, minor: 2, patch: 3, prerelease: [], build: [] }"
119
+ },
120
+ {
121
+ "title": "Parse a prerelease version",
122
+ "description": "Handles prerelease identifiers and optional v prefix.",
123
+ "code": "parse('v2.0.0-alpha.1')\n// => { major: 2, minor: 0, patch: 0, prerelease: ['alpha', '1'], build: [] }"
124
+ }
125
+ ],
126
+ "sourceFile": "parse.ts"
127
+ },
128
+ {
129
+ "name": "ParsedVersion",
130
+ "kind": "interface",
131
+ "description": "Represents a parsed semantic version according to SemVer 2.0.0 specification",
132
+ "since": "2.0.0",
133
+ "signatures": [],
134
+ "examples": [],
135
+ "sourceFile": "parse.ts"
136
+ },
137
+ {
138
+ "name": "satisfiesRange",
139
+ "kind": "function",
140
+ "description": "Checks if a version satisfies a range (simple implementation)",
141
+ "since": "1.9.0",
142
+ "signatures": [
143
+ {
144
+ "signature": "satisfiesRange(version: string, range: string): boolean",
145
+ "description": "Checks if a version satisfies a range (simple implementation)",
146
+ "params": [
147
+ {
148
+ "name": "version",
149
+ "type": "string",
150
+ "description": "Version to check"
151
+ },
152
+ {
153
+ "name": "range",
154
+ "type": "string",
155
+ "description": "Range pattern (e.g., \">=1.0.0\", \"~1.2.0\", \"^1.0.0\")"
156
+ }
157
+ ],
158
+ "returns": {
159
+ "type": "boolean",
160
+ "description": "True if version satisfies the range"
161
+ }
162
+ }
163
+ ],
164
+ "examples": [
165
+ {
166
+ "title": "Check caret range",
167
+ "description": "Caret (^) allows patch and minor updates within the same major.",
168
+ "code": "satisfiesRange('1.2.3', '^1.0.0')\n// => true"
169
+ },
170
+ {
171
+ "title": "Check greater-than-or-equal range",
172
+ "description": "The >= operator checks if the version is at least the specified value.",
173
+ "code": "satisfiesRange('2.0.0', '>=1.5.0')\n// => true"
174
+ },
175
+ {
176
+ "title": "Out of range",
177
+ "description": "Returns false when the version does not satisfy the range.",
178
+ "code": "satisfiesRange('0.9.0', '>=1.0.0')\n// => false"
179
+ }
180
+ ],
181
+ "sourceFile": "satisfiesRange.ts"
182
+ },
183
+ {
184
+ "name": "stripV",
185
+ "kind": "function",
186
+ "description": "Strip the leading \"v\" from a version string if it exists.",
187
+ "since": "1.9.0",
188
+ "signatures": [
189
+ {
190
+ "signature": "stripV(version: string): string",
191
+ "description": "Strip the leading \"v\" from a version string if it exists.",
192
+ "params": [
193
+ {
194
+ "name": "version",
195
+ "type": "string",
196
+ "description": "The version string to process"
197
+ }
198
+ ],
199
+ "returns": {
200
+ "type": "string",
201
+ "description": "The version string without leading \"v\", or the original value if it's not a string or doesn't start with \"v\""
202
+ }
203
+ },
204
+ {
205
+ "signature": "stripV(version: null): null",
206
+ "description": "Strip the leading \"v\" from a version string if it exists.",
207
+ "params": [
208
+ {
209
+ "name": "version",
210
+ "type": "null",
211
+ "description": "The version string to process"
212
+ }
213
+ ],
214
+ "returns": {
215
+ "type": "null",
216
+ "description": "The version string without leading \"v\", or the original value if it's not a string or doesn't start with \"v\""
217
+ }
218
+ },
219
+ {
220
+ "signature": "stripV(version: undefined): undefined",
221
+ "description": "Strip the leading \"v\" from a version string if it exists.",
222
+ "params": [
223
+ {
224
+ "name": "version",
225
+ "type": "undefined",
226
+ "description": "The version string to process"
227
+ }
228
+ ],
229
+ "returns": {
230
+ "type": "undefined",
231
+ "description": "The version string without leading \"v\", or the original value if it's not a string or doesn't start with \"v\""
232
+ }
233
+ }
234
+ ],
235
+ "examples": [
236
+ {
237
+ "title": "Remove v prefix from a version string",
238
+ "description": "Strips the leading \"v\" from a git tag-style version string.",
239
+ "code": "stripV('v1.2.3')\n// => '1.2.3'"
240
+ },
241
+ {
242
+ "title": "No-op when there is no v prefix",
243
+ "description": "Returns the string unchanged when it does not start with \"v\".",
244
+ "code": "stripV('1.2.3')\n// => '1.2.3'"
245
+ }
246
+ ],
247
+ "sourceFile": "stripV.ts"
248
+ }
249
+ ]
250
+ }