@helpers4/version 2.0.0-alpha.3 → 2.0.0-alpha.6

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
@@ -12,10 +12,11 @@ A set of helpers for working with URLs.
12
12
  [https://helpers4.js.org/version](https://helpers4.js.org/version)
13
13
 
14
14
  <!-- AUTOMATIC-METHODS -->
15
- - stripV
16
15
  - compare
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/api.json ADDED
@@ -0,0 +1,104 @@
1
+ {
2
+ "category": "version",
3
+ "members": [
4
+ {
5
+ "name": "compare",
6
+ "kind": "function",
7
+ "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",
8
+ "signature": "compare(version1: string, version2: string): number",
9
+ "params": [
10
+ {
11
+ "name": "version1",
12
+ "type": "string",
13
+ "description": "First version string"
14
+ },
15
+ {
16
+ "name": "version2",
17
+ "type": "string",
18
+ "description": "Second version string"
19
+ }
20
+ ],
21
+ "returns": "number",
22
+ "examples": [
23
+ "```ts\ncompare('1.0.0', '2.0.0') // -1\ncompare('1.0.0-alpha', '1.0.0') // -1 (prerelease < release)\ncompare('1.0.0-alpha', '1.0.0-beta') // -1\ncompare('1.0.0-alpha.1', '1.0.0-alpha.2') // -1\ncompare('1.0.0+build1', '1.0.0+build2') // 0 (build metadata ignored)\n```"
24
+ ]
25
+ },
26
+ {
27
+ "name": "increment",
28
+ "kind": "function",
29
+ "description": "Increments a semantic version",
30
+ "signature": "increment(version: string, type: \"major\" | \"minor\" | \"patch\"): string",
31
+ "params": [
32
+ {
33
+ "name": "version",
34
+ "type": "string",
35
+ "description": "The version to increment"
36
+ },
37
+ {
38
+ "name": "type",
39
+ "type": "\"major\" | \"minor\" | \"patch\"",
40
+ "description": "The increment type ('major', 'minor', 'patch')"
41
+ }
42
+ ],
43
+ "returns": "string"
44
+ },
45
+ {
46
+ "name": "parse",
47
+ "kind": "function",
48
+ "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)",
49
+ "signature": "parse(version: string): ParsedVersion",
50
+ "params": [
51
+ {
52
+ "name": "version",
53
+ "type": "string",
54
+ "description": "Version string to parse"
55
+ }
56
+ ],
57
+ "returns": "ParsedVersion",
58
+ "examples": [
59
+ "```ts\nparse('1.2.3') // { major: 1, minor: 2, patch: 3, prerelease: [], build: [] }\nparse('v1.0.0-alpha.1') // { major: 1, minor: 0, patch: 0, prerelease: ['alpha', '1'], build: [] }\nparse('2.0.0+build.123') // { major: 2, minor: 0, patch: 0, prerelease: [], build: ['build', '123'] }\nparse('1.0.0-beta+exp.sha.5114f85') // { major: 1, minor: 0, patch: 0, prerelease: ['beta'], build: ['exp', 'sha', '5114f85'] }\n```"
60
+ ]
61
+ },
62
+ {
63
+ "name": "ParsedVersion",
64
+ "kind": "interface",
65
+ "description": "Represents a parsed semantic version according to SemVer 2.0.0 specification"
66
+ },
67
+ {
68
+ "name": "satisfiesRange",
69
+ "kind": "function",
70
+ "description": "Checks if a version satisfies a range (simple implementation)",
71
+ "signature": "satisfiesRange(version: string, range: string): boolean",
72
+ "params": [
73
+ {
74
+ "name": "version",
75
+ "type": "string",
76
+ "description": "Version to check"
77
+ },
78
+ {
79
+ "name": "range",
80
+ "type": "string",
81
+ "description": "Range pattern (e.g., \">=1.0.0\", \"~1.2.0\", \"^1.0.0\")"
82
+ }
83
+ ],
84
+ "returns": "boolean"
85
+ },
86
+ {
87
+ "name": "stripV",
88
+ "kind": "function",
89
+ "description": "Strip the leading \"v\" from a version string if it exists.",
90
+ "signature": "stripV(version: string): string",
91
+ "params": [
92
+ {
93
+ "name": "version",
94
+ "type": "string",
95
+ "description": "The version string to process"
96
+ }
97
+ ],
98
+ "returns": "string",
99
+ "examples": [
100
+ "```typescript\nstripV(\"v1.2.3\") // \"1.2.3\"\nstripV(\"1.2.3\") // \"1.2.3\"\nstripV(\"v20.1.0\") // \"20.1.0\"\nstripV(null) // null\nstripV(undefined) // undefined\nstripV(\"\") // \"\"\nstripV(\"1.0.0-beta\") // \"1.0.0-beta\"\n```"
101
+ ]
102
+ }
103
+ ]
104
+ }
package/examples.json ADDED
@@ -0,0 +1,90 @@
1
+ {
2
+ "category": "version",
3
+ "helpers": [
4
+ {
5
+ "helper": "compare",
6
+ "examples": [
7
+ {
8
+ "title": "Compare two semver versions",
9
+ "description": "Returns -1, 0, or 1 based on SemVer ordering.",
10
+ "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"
11
+ },
12
+ {
13
+ "title": "Prerelease is lower than release",
14
+ "description": "A prerelease version is always less than the release.",
15
+ "code": "compare('1.0.0-alpha', '1.0.0')\n// => -1"
16
+ }
17
+ ]
18
+ },
19
+ {
20
+ "helper": "increment",
21
+ "examples": [
22
+ {
23
+ "title": "Increment the patch version",
24
+ "description": "Bumps the patch number while keeping major and minor.",
25
+ "code": "increment('1.2.3', 'patch')\n// => '1.2.4'"
26
+ },
27
+ {
28
+ "title": "Increment the minor version",
29
+ "description": "Bumps the minor number and resets patch to 0.",
30
+ "code": "increment('1.2.3', 'minor')\n// => '1.3.0'"
31
+ },
32
+ {
33
+ "title": "Preserve the v prefix",
34
+ "description": "The v prefix is preserved if present in the input.",
35
+ "code": "increment('v1.0.0', 'major')\n// => 'v2.0.0'"
36
+ }
37
+ ]
38
+ },
39
+ {
40
+ "helper": "parse",
41
+ "examples": [
42
+ {
43
+ "title": "Parse a semver string",
44
+ "description": "Breaks a semantic version string into its components.",
45
+ "code": "parse('1.2.3')\n// => { major: 1, minor: 2, patch: 3, prerelease: [], build: [] }"
46
+ },
47
+ {
48
+ "title": "Parse a prerelease version",
49
+ "description": "Handles prerelease identifiers and optional v prefix.",
50
+ "code": "parse('v2.0.0-alpha.1')\n// => { major: 2, minor: 0, patch: 0, prerelease: ['alpha', '1'], build: [] }"
51
+ }
52
+ ]
53
+ },
54
+ {
55
+ "helper": "satisfiesRange",
56
+ "examples": [
57
+ {
58
+ "title": "Check caret range",
59
+ "description": "Caret (^) allows patch and minor updates within the same major.",
60
+ "code": "satisfiesRange('1.2.3', '^1.0.0')\n// => true"
61
+ },
62
+ {
63
+ "title": "Check greater-than-or-equal range",
64
+ "description": "The >= operator checks if the version is at least the specified value.",
65
+ "code": "satisfiesRange('2.0.0', '>=1.5.0')\n// => true"
66
+ },
67
+ {
68
+ "title": "Out of range",
69
+ "description": "Returns false when the version does not satisfy the range.",
70
+ "code": "satisfiesRange('0.9.0', '>=1.0.0')\n// => false"
71
+ }
72
+ ]
73
+ },
74
+ {
75
+ "helper": "stripV",
76
+ "examples": [
77
+ {
78
+ "title": "Remove v prefix from a version string",
79
+ "description": "Strips the leading \"v\" from a git tag-style version string.",
80
+ "code": "stripV('v1.2.3')\n// => '1.2.3'"
81
+ },
82
+ {
83
+ "title": "No-op when there is no v prefix",
84
+ "description": "Returns the string unchanged when it does not start with \"v\".",
85
+ "code": "stripV('1.2.3')\n// => '1.2.3'"
86
+ }
87
+ ]
88
+ }
89
+ ]
90
+ }
package/lib/index.d.ts CHANGED
@@ -1,4 +1,119 @@
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;
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
+ */
25
+ declare function compare(version1: string, version2: string): number;
26
+
27
+ /**
28
+ * This file is part of helpers4.
29
+ * Copyright (C) 2025 baxyz
30
+ * SPDX-License-Identifier: LGPL-3.0-or-later
31
+ */
32
+ /**
33
+ * Increments a semantic version
34
+ * @param version - The version to increment
35
+ * @param type - The increment type ('major', 'minor', 'patch')
36
+ * @returns Incremented version string
37
+ */
38
+ declare function increment(version: string, type: 'major' | 'minor' | 'patch'): string;
39
+
40
+ /**
41
+ * This file is part of helpers4.
42
+ * Copyright (C) 2025 baxyz
43
+ * SPDX-License-Identifier: LGPL-3.0-or-later
44
+ */
45
+ /**
46
+ * Represents a parsed semantic version according to SemVer 2.0.0 specification
47
+ */
48
+ interface ParsedVersion {
49
+ /** Major version number */
50
+ major: number;
51
+ /** Minor version number */
52
+ minor: number;
53
+ /** Patch version number */
54
+ patch: number;
55
+ /** Pre-release identifiers (e.g., ['alpha', '1'] for -alpha.1) */
56
+ prerelease: string[];
57
+ /** Build metadata identifiers (e.g., ['build', '123'] for +build.123) */
58
+ build: string[];
59
+ }
60
+ /**
61
+ * Parses a semantic version string into its components according to SemVer 2.0.0 specification
62
+ *
63
+ * Supports:
64
+ * - Core version: MAJOR.MINOR.PATCH
65
+ * - Pre-release: -alpha, -beta.1, -rc.1, -0.3.7, -x.7.z.92
66
+ * - Build metadata: +build, +sha.abc123, +20130313144700
67
+ * - Optional 'v' prefix (commonly used in git tags)
68
+ *
69
+ * @param version - Version string to parse
70
+ * @returns Parsed version object with major, minor, patch, prerelease, and build
71
+ * @example
72
+ * parse('1.2.3') // { major: 1, minor: 2, patch: 3, prerelease: [], build: [] }
73
+ * parse('v1.0.0-alpha.1') // { major: 1, minor: 0, patch: 0, prerelease: ['alpha', '1'], build: [] }
74
+ * parse('2.0.0+build.123') // { major: 2, minor: 0, patch: 0, prerelease: [], build: ['build', '123'] }
75
+ * parse('1.0.0-beta+exp.sha.5114f85') // { major: 1, minor: 0, patch: 0, prerelease: ['beta'], build: ['exp', 'sha', '5114f85'] }
76
+ */
77
+ declare function parse(version: string): ParsedVersion;
78
+
79
+ /**
80
+ * This file is part of helpers4.
81
+ * Copyright (C) 2025 baxyz
82
+ * SPDX-License-Identifier: LGPL-3.0-or-later
83
+ */
84
+ /**
85
+ * Checks if a version satisfies a range (simple implementation)
86
+ * @param version - Version to check
87
+ * @param range - Range pattern (e.g., ">=1.0.0", "~1.2.0", "^1.0.0")
88
+ * @returns True if version satisfies the range
89
+ */
90
+ declare function satisfiesRange(version: string, range: string): boolean;
91
+
92
+ /**
93
+ * This file is part of helpers4.
94
+ * Copyright (C) 2025 baxyz
95
+ * SPDX-License-Identifier: LGPL-3.0-or-later
96
+ */
97
+ /**
98
+ * Strip the leading "v" from a version string if it exists.
99
+ *
100
+ * @param version - The version string to process
101
+ * @returns The version string without leading "v", or the original value if it's not a string or doesn't start with "v"
102
+ *
103
+ * @example
104
+ * ```typescript
105
+ * stripV("v1.2.3") // "1.2.3"
106
+ * stripV("1.2.3") // "1.2.3"
107
+ * stripV("v20.1.0") // "20.1.0"
108
+ * stripV(null) // null
109
+ * stripV(undefined) // undefined
110
+ * stripV("") // ""
111
+ * stripV("1.0.0-beta") // "1.0.0-beta"
112
+ * ```
113
+ */
114
+ declare function stripV(version: string): string;
115
+ declare function stripV(version: null): null;
116
+ declare function stripV(version: undefined): undefined;
117
+
118
+ export { compare, increment, parse, satisfiesRange, stripV };
119
+ export type { ParsedVersion };
package/lib/index.js CHANGED
@@ -1,108 +1,195 @@
1
- // helpers/version/stripV.ts
2
- function stripV(version) {
3
- return typeof version === "string" && version.startsWith("v") ? version.slice(1) : version;
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
+ */
19
+ function parse(version) {
20
+ const [versionWithPrerelease, buildString] = version.replace(/^v/, "").split("+");
21
+ const build = buildString ? buildString.split(".") : [];
22
+ const [coreVersion, prereleaseString] = versionWithPrerelease.split("-");
23
+ const prerelease = prereleaseString ? prereleaseString.split(".") : [];
24
+ const parts = coreVersion.split(".").map(Number);
25
+ return {
26
+ major: parts[0] || 0,
27
+ minor: parts[1] || 0,
28
+ patch: parts[2] || 0,
29
+ prerelease,
30
+ build
31
+ };
32
+ }
33
+ //#endregion
34
+ //#region helpers/version/compare.ts
35
+ /**
36
+ * This file is part of helpers4.
37
+ * Copyright (C) 2025 baxyz
38
+ * SPDX-License-Identifier: LGPL-3.0-or-later
39
+ */
40
+ /**
41
+ * Compares two prerelease identifier arrays according to SemVer spec
42
+ * Rules:
43
+ * - Numeric identifiers are compared as integers
44
+ * - Alphanumeric identifiers are compared lexically (ASCII)
45
+ * - Numeric identifiers have lower precedence than alphanumeric
46
+ * - A larger set of prerelease fields has higher precedence if all preceding are equal
47
+ * @param pre1 - First prerelease array
48
+ * @param pre2 - Second prerelease array
49
+ * @returns -1, 0, or 1
50
+ */
51
+ function comparePrerelease(pre1, pre2) {
52
+ if (pre1.length === 0 && pre2.length === 0) return 0;
53
+ if (pre1.length === 0) return 1;
54
+ if (pre2.length === 0) return -1;
55
+ const maxLength = Math.max(pre1.length, pre2.length);
56
+ for (let i = 0; i < maxLength; i++) {
57
+ if (i >= pre1.length) return -1;
58
+ if (i >= pre2.length) return 1;
59
+ const id1 = pre1[i];
60
+ const id2 = pre2[i];
61
+ const isNum1 = /^\d+$/.test(id1);
62
+ const isNum2 = /^\d+$/.test(id2);
63
+ if (isNum1 && isNum2) {
64
+ const num1 = parseInt(id1, 10);
65
+ const num2 = parseInt(id2, 10);
66
+ if (num1 < num2) return -1;
67
+ if (num1 > num2) return 1;
68
+ } else if (isNum1) return -1;
69
+ else if (isNum2) return 1;
70
+ else {
71
+ if (id1 < id2) return -1;
72
+ if (id1 > id2) return 1;
73
+ }
74
+ }
75
+ return 0;
4
76
  }
5
- // helpers/version/compare.ts
77
+ /**
78
+ * Compares two semantic version strings according to SemVer 2.0.0 specification
79
+ *
80
+ * Supports:
81
+ * - Core version: MAJOR.MINOR.PATCH
82
+ * - Pre-release: -alpha, -beta.1, -rc.1, etc.
83
+ * - Build metadata: +build, +sha.abc123 (ignored in comparison per spec)
84
+ * - Optional 'v' prefix
85
+ *
86
+ * @param version1 - First version string
87
+ * @param version2 - Second version string
88
+ * @returns -1 if version1 < version2, 0 if equal, 1 if version1 > version2
89
+ * @example
90
+ * compare('1.0.0', '2.0.0') // -1
91
+ * compare('1.0.0-alpha', '1.0.0') // -1 (prerelease < release)
92
+ * compare('1.0.0-alpha', '1.0.0-beta') // -1
93
+ * compare('1.0.0-alpha.1', '1.0.0-alpha.2') // -1
94
+ * compare('1.0.0+build1', '1.0.0+build2') // 0 (build metadata ignored)
95
+ */
6
96
  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;
97
+ const v1 = parse(version1);
98
+ const v2 = parse(version2);
99
+ if (v1.major !== v2.major) return v1.major < v2.major ? -1 : 1;
100
+ if (v1.minor !== v2.minor) return v1.minor < v2.minor ? -1 : 1;
101
+ if (v1.patch !== v2.patch) return v1.patch < v2.patch ? -1 : 1;
102
+ return comparePrerelease(v1.prerelease, v2.prerelease);
103
+ }
104
+ //#endregion
105
+ //#region helpers/version/increment.ts
106
+ /**
107
+ * This file is part of helpers4.
108
+ * Copyright (C) 2025 baxyz
109
+ * SPDX-License-Identifier: LGPL-3.0-or-later
110
+ */
111
+ /**
112
+ * Increments a semantic version
113
+ * @param version - The version to increment
114
+ * @param type - The increment type ('major', 'minor', 'patch')
115
+ * @returns Incremented version string
116
+ */
117
+ function increment(version, type) {
118
+ const normalize = (v) => v.replace(/^v/, "");
119
+ const hasV = version.startsWith("v");
120
+ const parts = normalize(version).split(".").map(Number);
121
+ while (parts.length < 3) parts.push(0);
122
+ let [major, minor, patch] = parts;
123
+ switch (type) {
124
+ case "major":
125
+ major++;
126
+ minor = 0;
127
+ patch = 0;
128
+ break;
129
+ case "minor":
130
+ minor++;
131
+ patch = 0;
132
+ break;
133
+ case "patch":
134
+ patch++;
135
+ break;
136
+ default: throw new Error(`Invalid increment type: ${type}`);
137
+ }
138
+ const result = `${major}.${minor}.${patch}`;
139
+ return hasV ? `v${result}` : result;
22
140
  }
23
- // helpers/version/satisfiesRange.ts
141
+ //#endregion
142
+ //#region helpers/version/satisfiesRange.ts
143
+ /**
144
+ * This file is part of helpers4.
145
+ * Copyright (C) 2025 baxyz
146
+ * SPDX-License-Identifier: LGPL-3.0-or-later
147
+ */
148
+ /**
149
+ * Checks if a version satisfies a range (simple implementation)
150
+ * @param version - Version to check
151
+ * @param range - Range pattern (e.g., ">=1.0.0", "~1.2.0", "^1.0.0")
152
+ * @returns True if version satisfies the range
153
+ */
24
154
  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;
155
+ const normalize = (v) => v.replace(/^v/, "");
156
+ const normalizedVersion = normalize(version);
157
+ if (!range.match(/[~^<>=]/)) return normalizedVersion === normalize(range);
158
+ if (range.startsWith(">=")) return compareVersionsSimple(normalizedVersion, normalize(range.slice(2))) >= 0;
159
+ if (range.startsWith(">")) return compareVersionsSimple(normalizedVersion, normalize(range.slice(1))) > 0;
160
+ if (range.startsWith("<=")) return compareVersionsSimple(normalizedVersion, normalize(range.slice(2))) <= 0;
161
+ if (range.startsWith("<")) return compareVersionsSimple(normalizedVersion, normalize(range.slice(1))) < 0;
162
+ if (range.startsWith("^")) {
163
+ const targetVersion = normalize(range.slice(1));
164
+ const [targetMajor] = targetVersion.split(".").map(Number);
165
+ const [versionMajor] = normalizedVersion.split(".").map(Number);
166
+ return versionMajor === targetMajor && compareVersionsSimple(normalizedVersion, targetVersion) >= 0;
167
+ }
168
+ if (range.startsWith("~")) {
169
+ const targetVersion = normalize(range.slice(1));
170
+ const [targetMajor, targetMinor] = targetVersion.split(".").map(Number);
171
+ const [versionMajor, versionMinor] = normalizedVersion.split(".").map(Number);
172
+ return versionMajor === targetMajor && versionMinor === targetMinor && compareVersionsSimple(normalizedVersion, targetVersion) >= 0;
173
+ } else return false;
59
174
  }
60
175
  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;
176
+ const parts1 = version1.split(".").map(Number);
177
+ const parts2 = version2.split(".").map(Number);
178
+ const maxLength = Math.max(parts1.length, parts2.length);
179
+ for (let i = 0; i < maxLength; i++) {
180
+ const part1 = parts1[i] || 0;
181
+ const part2 = parts2[i] || 0;
182
+ if (part1 < part2) return -1;
183
+ if (part1 > part2) return 1;
184
+ }
185
+ return 0;
73
186
  }
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;
187
+ //#endregion
188
+ //#region helpers/version/stripV.ts
189
+ function stripV(version) {
190
+ return typeof version === "string" && version.startsWith("v") ? version.slice(1) : version;
102
191
  }
103
- export {
104
- stripV,
105
- satisfiesRange,
106
- increment,
107
- compare
108
- };
192
+ //#endregion
193
+ export { compare, increment, parse, satisfiesRange, stripV };
194
+
195
+ //# sourceMappingURL=index.js.map