@helpers4/version 2.0.0-alpha.2 → 2.0.0-alpha.21
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/LICENSE.md +150 -650
- package/README.md +13 -6
- package/lib/index.d.ts +192 -4
- package/lib/index.js +174 -100
- package/lib/index.js.map +1 -0
- package/llms.txt +485 -0
- package/meta/api.json +470 -0
- package/meta/category.json +6 -0
- package/meta/examples.json +125 -0
- package/meta/licenses.json +4 -0
- package/package.json +22 -7
package/llms.txt
ADDED
|
@@ -0,0 +1,485 @@
|
|
|
1
|
+
# @helpers4/version
|
|
2
|
+
|
|
3
|
+
> Tree-shakable TypeScript utility functions for the `version` domain.
|
|
4
|
+
> Package: `@helpers4/version` — Version: 2.0.0-alpha.21
|
|
5
|
+
> License: LGPL-3.0-or-later
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install @helpers4/version
|
|
11
|
+
# or
|
|
12
|
+
pnpm add @helpers4/version
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { compare, increment, isPrerelease, ... } from '@helpers4/version';
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Functions
|
|
22
|
+
|
|
23
|
+
| Function | Description |
|
|
24
|
+
|---|---|
|
|
25
|
+
| `compare` | Compares two semantic version strings according to SemVer 2.0.0 specification Supports: - Core vers |
|
|
26
|
+
| `increment` | Increments a semantic version |
|
|
27
|
+
| `isPrerelease` | Returns `true` when the version string has a prerelease suffix (i.e. contains a `-` after the core ` |
|
|
28
|
+
| `parse` | Parses a semantic version string into its components according to SemVer 2.0.0 specification Suppor |
|
|
29
|
+
| `satisfiesRange` | Checks if a version satisfies a range (simple implementation) |
|
|
30
|
+
| `stringify` | Reconstruct a semantic version string from a ParsedVersion object. This is the inverse of parse: `s |
|
|
31
|
+
| `stripV` | Strip the leading "v" from a version string if it exists. |
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
## API Reference
|
|
36
|
+
|
|
37
|
+
### `compare`
|
|
38
|
+
|
|
39
|
+
Compares two semantic version strings according to SemVer 2.0.0 specification
|
|
40
|
+
|
|
41
|
+
Supports:
|
|
42
|
+
- Core version: MAJOR.MINOR.PATCH
|
|
43
|
+
- Pre-release: -alpha, -beta.1, -rc.1, etc.
|
|
44
|
+
- Build metadata: +build, +sha.abc123 (ignored in comparison per spec)
|
|
45
|
+
- Optional 'v' prefix
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
import { compare } from '@helpers4/version';
|
|
49
|
+
|
|
50
|
+
compare(version1: string, version2: string): number
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
**Parameters:**
|
|
54
|
+
|
|
55
|
+
- `version1: string` — First version string
|
|
56
|
+
- `version2: string` — Second version string
|
|
57
|
+
|
|
58
|
+
**Returns:** `number` — -1 if version1 < version2, 0 if equal, 1 if version1 > version2
|
|
59
|
+
|
|
60
|
+
**Examples:**
|
|
61
|
+
|
|
62
|
+
*Compare two semver versions*
|
|
63
|
+
|
|
64
|
+
Returns -1, 0, or 1 based on SemVer ordering.
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
compare('1.0.0', '2.0.0') // => -1
|
|
68
|
+
compare('1.0.0', '1.0.0') // => 0
|
|
69
|
+
compare('2.0.0', '1.0.0') // => 1
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
*Prerelease is lower than release*
|
|
73
|
+
|
|
74
|
+
A prerelease version is always less than the release.
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
compare('1.0.0-alpha', '1.0.0')
|
|
78
|
+
// => -1
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
---
|
|
82
|
+
|
|
83
|
+
### `increment`
|
|
84
|
+
|
|
85
|
+
Increments a semantic version
|
|
86
|
+
|
|
87
|
+
```typescript
|
|
88
|
+
import { increment } from '@helpers4/version';
|
|
89
|
+
|
|
90
|
+
increment(version: string, type: "major" | "minor" | "patch"): string
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
**Parameters:**
|
|
94
|
+
|
|
95
|
+
- `version: string` — The version to increment
|
|
96
|
+
- `type: "major" | "minor" | "patch"` — The increment type ('major', 'minor', 'patch')
|
|
97
|
+
|
|
98
|
+
**Returns:** `string` — Incremented version string
|
|
99
|
+
|
|
100
|
+
```typescript
|
|
101
|
+
import { increment } from '@helpers4/version';
|
|
102
|
+
|
|
103
|
+
increment(version: undefined, type: "major" | "minor" | "patch"): undefined
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
**Parameters:**
|
|
107
|
+
|
|
108
|
+
- `version: undefined` — The version to increment
|
|
109
|
+
- `type: "major" | "minor" | "patch"` — The increment type ('major', 'minor', 'patch')
|
|
110
|
+
|
|
111
|
+
**Returns:** `undefined` — Incremented version string
|
|
112
|
+
|
|
113
|
+
```typescript
|
|
114
|
+
import { increment } from '@helpers4/version';
|
|
115
|
+
|
|
116
|
+
increment(version: null, type: "major" | "minor" | "patch"): null
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
**Parameters:**
|
|
120
|
+
|
|
121
|
+
- `version: null` — The version to increment
|
|
122
|
+
- `type: "major" | "minor" | "patch"` — The increment type ('major', 'minor', 'patch')
|
|
123
|
+
|
|
124
|
+
**Returns:** `null` — Incremented version string
|
|
125
|
+
|
|
126
|
+
**Examples:**
|
|
127
|
+
|
|
128
|
+
*Increment the patch version*
|
|
129
|
+
|
|
130
|
+
Bumps the patch number while keeping major and minor.
|
|
131
|
+
|
|
132
|
+
```typescript
|
|
133
|
+
increment('1.2.3', 'patch')
|
|
134
|
+
// => '1.2.4'
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
*Increment the minor version*
|
|
138
|
+
|
|
139
|
+
Bumps the minor number and resets patch to 0.
|
|
140
|
+
|
|
141
|
+
```typescript
|
|
142
|
+
increment('1.2.3', 'minor')
|
|
143
|
+
// => '1.3.0'
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
*Preserve the v prefix*
|
|
147
|
+
|
|
148
|
+
The v prefix is preserved if present in the input.
|
|
149
|
+
|
|
150
|
+
```typescript
|
|
151
|
+
increment('v1.0.0', 'major')
|
|
152
|
+
// => 'v2.0.0'
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
---
|
|
156
|
+
|
|
157
|
+
### `isPrerelease`
|
|
158
|
+
|
|
159
|
+
Returns `true` when the version string has a prerelease suffix
|
|
160
|
+
(i.e. contains a `-` after the core `MAJOR.MINOR.PATCH`).
|
|
161
|
+
|
|
162
|
+
```typescript
|
|
163
|
+
import { isPrerelease } from '@helpers4/version';
|
|
164
|
+
|
|
165
|
+
isPrerelease(version: string): boolean
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
**Parameters:**
|
|
169
|
+
|
|
170
|
+
- `version: string` — A semantic version string (e.g. `'2.0.0-alpha.1'`, `'1.0.0'`).
|
|
171
|
+
|
|
172
|
+
**Returns:** `boolean` — `true` if the version is a prerelease, `false` otherwise.
|
|
173
|
+
|
|
174
|
+
```typescript
|
|
175
|
+
import { isPrerelease } from '@helpers4/version';
|
|
176
|
+
|
|
177
|
+
isPrerelease(version: ParsedVersion): boolean
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
**Parameters:**
|
|
181
|
+
|
|
182
|
+
- `version: ParsedVersion` — A ParsedVersion object (as returned by parse).
|
|
183
|
+
|
|
184
|
+
**Returns:** `boolean` — `true` if `version.prerelease` is non-empty, `false` otherwise.
|
|
185
|
+
|
|
186
|
+
```typescript
|
|
187
|
+
import { isPrerelease } from '@helpers4/version';
|
|
188
|
+
|
|
189
|
+
isPrerelease(version: undefined): undefined
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
**Parameters:**
|
|
193
|
+
|
|
194
|
+
- `version: undefined` — A semantic version string (e.g. `'2.0.0-alpha.1'`, `'1.0.0'`).
|
|
195
|
+
|
|
196
|
+
**Returns:** `undefined` — `true` if the version is a prerelease, `false` otherwise.
|
|
197
|
+
|
|
198
|
+
```typescript
|
|
199
|
+
import { isPrerelease } from '@helpers4/version';
|
|
200
|
+
|
|
201
|
+
isPrerelease(version: null): null
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
**Parameters:**
|
|
205
|
+
|
|
206
|
+
- `version: null` — A semantic version string (e.g. `'2.0.0-alpha.1'`, `'1.0.0'`).
|
|
207
|
+
|
|
208
|
+
**Returns:** `null` — `true` if the version is a prerelease, `false` otherwise.
|
|
209
|
+
|
|
210
|
+
**Examples:**
|
|
211
|
+
|
|
212
|
+
*Detect a prerelease version*
|
|
213
|
+
|
|
214
|
+
Returns true for any version string that contains a prerelease suffix.
|
|
215
|
+
|
|
216
|
+
```typescript
|
|
217
|
+
isPrerelease('2.0.0-alpha.1') // true
|
|
218
|
+
isPrerelease('1.0.0-rc.0') // true
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
*Stable versions return false*
|
|
222
|
+
|
|
223
|
+
Returns false when the version has no prerelease suffix.
|
|
224
|
+
|
|
225
|
+
```typescript
|
|
226
|
+
isPrerelease('1.0.0') // false
|
|
227
|
+
isPrerelease('2.1.3') // false
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
*Accept a ParsedVersion object*
|
|
231
|
+
|
|
232
|
+
Works with the result of parse() — checks the prerelease array instead of string matching.
|
|
233
|
+
|
|
234
|
+
```typescript
|
|
235
|
+
isPrerelease(parse('2.0.0-alpha.1')) // true
|
|
236
|
+
isPrerelease(parse('1.0.0')) // false
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
---
|
|
240
|
+
|
|
241
|
+
### `parse`
|
|
242
|
+
|
|
243
|
+
Parses a semantic version string into its components according to SemVer 2.0.0 specification
|
|
244
|
+
|
|
245
|
+
Supports:
|
|
246
|
+
- Core version: MAJOR.MINOR.PATCH
|
|
247
|
+
- Pre-release: -alpha, -beta.1, -rc.1, -0.3.7, -x.7.z.92
|
|
248
|
+
- Build metadata: +build, +sha.abc123, +20130313144700
|
|
249
|
+
- Optional 'v' prefix (commonly used in git tags)
|
|
250
|
+
|
|
251
|
+
```typescript
|
|
252
|
+
import { parse } from '@helpers4/version';
|
|
253
|
+
|
|
254
|
+
parse(version: string): ParsedVersion
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
**Parameters:**
|
|
258
|
+
|
|
259
|
+
- `version: string` — Version string to parse
|
|
260
|
+
|
|
261
|
+
**Returns:** `ParsedVersion` — Parsed version object with major, minor, patch, prerelease, and build
|
|
262
|
+
|
|
263
|
+
```typescript
|
|
264
|
+
import { parse } from '@helpers4/version';
|
|
265
|
+
|
|
266
|
+
parse(version: undefined): undefined
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
**Parameters:**
|
|
270
|
+
|
|
271
|
+
- `version: undefined` — Version string to parse
|
|
272
|
+
|
|
273
|
+
**Returns:** `undefined` — Parsed version object with major, minor, patch, prerelease, and build
|
|
274
|
+
|
|
275
|
+
```typescript
|
|
276
|
+
import { parse } from '@helpers4/version';
|
|
277
|
+
|
|
278
|
+
parse(version: null): null
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
**Parameters:**
|
|
282
|
+
|
|
283
|
+
- `version: null` — Version string to parse
|
|
284
|
+
|
|
285
|
+
**Returns:** `null` — Parsed version object with major, minor, patch, prerelease, and build
|
|
286
|
+
|
|
287
|
+
**Examples:**
|
|
288
|
+
|
|
289
|
+
*Parse a semver string*
|
|
290
|
+
|
|
291
|
+
Breaks a semantic version string into its components.
|
|
292
|
+
|
|
293
|
+
```typescript
|
|
294
|
+
parse('1.2.3')
|
|
295
|
+
// => { major: 1, minor: 2, patch: 3, prerelease: [], build: [] }
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
*Parse a prerelease version*
|
|
299
|
+
|
|
300
|
+
Handles prerelease identifiers and optional v prefix.
|
|
301
|
+
|
|
302
|
+
```typescript
|
|
303
|
+
parse('v2.0.0-alpha.1')
|
|
304
|
+
// => { major: 2, minor: 0, patch: 0, prerelease: ['alpha', '1'], build: [] }
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
---
|
|
308
|
+
|
|
309
|
+
### `satisfiesRange`
|
|
310
|
+
|
|
311
|
+
Checks if a version satisfies a range (simple implementation)
|
|
312
|
+
|
|
313
|
+
```typescript
|
|
314
|
+
import { satisfiesRange } from '@helpers4/version';
|
|
315
|
+
|
|
316
|
+
satisfiesRange(version: string, range: string): boolean
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
**Parameters:**
|
|
320
|
+
|
|
321
|
+
- `version: string` — Version to check
|
|
322
|
+
- `range: string` — Range pattern (e.g., ">=1.0.0", "~1.2.0", "^1.0.0")
|
|
323
|
+
|
|
324
|
+
**Returns:** `boolean` — True if version satisfies the range
|
|
325
|
+
|
|
326
|
+
**Examples:**
|
|
327
|
+
|
|
328
|
+
*Check caret range*
|
|
329
|
+
|
|
330
|
+
Caret (^) allows patch and minor updates within the same major.
|
|
331
|
+
|
|
332
|
+
```typescript
|
|
333
|
+
satisfiesRange('1.2.3', '^1.0.0')
|
|
334
|
+
// => true
|
|
335
|
+
```
|
|
336
|
+
|
|
337
|
+
*Check greater-than-or-equal range*
|
|
338
|
+
|
|
339
|
+
The >= operator checks if the version is at least the specified value.
|
|
340
|
+
|
|
341
|
+
```typescript
|
|
342
|
+
satisfiesRange('2.0.0', '>=1.5.0')
|
|
343
|
+
// => true
|
|
344
|
+
```
|
|
345
|
+
|
|
346
|
+
*Out of range*
|
|
347
|
+
|
|
348
|
+
Returns false when the version does not satisfy the range.
|
|
349
|
+
|
|
350
|
+
```typescript
|
|
351
|
+
satisfiesRange('0.9.0', '>=1.0.0')
|
|
352
|
+
// => false
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
---
|
|
356
|
+
|
|
357
|
+
### `stringify`
|
|
358
|
+
|
|
359
|
+
Reconstruct a semantic version string from a ParsedVersion object.
|
|
360
|
+
|
|
361
|
+
This is the inverse of parse:
|
|
362
|
+
`stringify(parse(v)) === stripV(v)` for any valid SemVer string `v`.
|
|
363
|
+
|
|
364
|
+
```typescript
|
|
365
|
+
import { stringify } from '@helpers4/version';
|
|
366
|
+
|
|
367
|
+
stringify(parsed: ParsedVersion): string
|
|
368
|
+
```
|
|
369
|
+
|
|
370
|
+
**Parameters:**
|
|
371
|
+
|
|
372
|
+
- `parsed: ParsedVersion` — A parsed semantic version object.
|
|
373
|
+
|
|
374
|
+
**Returns:** `string` — The reconstructed version string (without leading `v`).
|
|
375
|
+
|
|
376
|
+
```typescript
|
|
377
|
+
import { stringify } from '@helpers4/version';
|
|
378
|
+
|
|
379
|
+
stringify(parsed: undefined): undefined
|
|
380
|
+
```
|
|
381
|
+
|
|
382
|
+
**Parameters:**
|
|
383
|
+
|
|
384
|
+
- `parsed: undefined` — A parsed semantic version object.
|
|
385
|
+
|
|
386
|
+
**Returns:** `undefined` — The reconstructed version string (without leading `v`).
|
|
387
|
+
|
|
388
|
+
```typescript
|
|
389
|
+
import { stringify } from '@helpers4/version';
|
|
390
|
+
|
|
391
|
+
stringify(parsed: null): null
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
**Parameters:**
|
|
395
|
+
|
|
396
|
+
- `parsed: null` — A parsed semantic version object.
|
|
397
|
+
|
|
398
|
+
**Returns:** `null` — The reconstructed version string (without leading `v`).
|
|
399
|
+
|
|
400
|
+
**Examples:**
|
|
401
|
+
|
|
402
|
+
*Reconstruct a stable version*
|
|
403
|
+
|
|
404
|
+
Converts a ParsedVersion object back to a version string.
|
|
405
|
+
|
|
406
|
+
```typescript
|
|
407
|
+
stringify({ major: 1, minor: 2, patch: 3, prerelease: [], build: [] })
|
|
408
|
+
// => '1.2.3'
|
|
409
|
+
```
|
|
410
|
+
|
|
411
|
+
*Round-trip with parse*
|
|
412
|
+
|
|
413
|
+
stringify(parse(v)) returns the original version string (without leading v).
|
|
414
|
+
|
|
415
|
+
```typescript
|
|
416
|
+
stringify(parse('2.0.0-alpha.1'))
|
|
417
|
+
// => '2.0.0-alpha.1'
|
|
418
|
+
|
|
419
|
+
stringify(parse('1.0.0-beta+exp.sha.5114f85'))
|
|
420
|
+
// => '1.0.0-beta+exp.sha.5114f85'
|
|
421
|
+
```
|
|
422
|
+
|
|
423
|
+
---
|
|
424
|
+
|
|
425
|
+
### `stripV`
|
|
426
|
+
|
|
427
|
+
Strip the leading "v" from a version string if it exists.
|
|
428
|
+
|
|
429
|
+
```typescript
|
|
430
|
+
import { stripV } from '@helpers4/version';
|
|
431
|
+
|
|
432
|
+
stripV(version: string): string
|
|
433
|
+
```
|
|
434
|
+
|
|
435
|
+
**Parameters:**
|
|
436
|
+
|
|
437
|
+
- `version: string` — The version string to process
|
|
438
|
+
|
|
439
|
+
**Returns:** `string` — The version string without leading "v", or the original value if it's not a string or doesn't start with "v"
|
|
440
|
+
|
|
441
|
+
```typescript
|
|
442
|
+
import { stripV } from '@helpers4/version';
|
|
443
|
+
|
|
444
|
+
stripV(version: null): null
|
|
445
|
+
```
|
|
446
|
+
|
|
447
|
+
**Parameters:**
|
|
448
|
+
|
|
449
|
+
- `version: null` — The version string to process
|
|
450
|
+
|
|
451
|
+
**Returns:** `null` — The version string without leading "v", or the original value if it's not a string or doesn't start with "v"
|
|
452
|
+
|
|
453
|
+
```typescript
|
|
454
|
+
import { stripV } from '@helpers4/version';
|
|
455
|
+
|
|
456
|
+
stripV(version: undefined): undefined
|
|
457
|
+
```
|
|
458
|
+
|
|
459
|
+
**Parameters:**
|
|
460
|
+
|
|
461
|
+
- `version: undefined` — The version string to process
|
|
462
|
+
|
|
463
|
+
**Returns:** `undefined` — The version string without leading "v", or the original value if it's not a string or doesn't start with "v"
|
|
464
|
+
|
|
465
|
+
**Examples:**
|
|
466
|
+
|
|
467
|
+
*Remove v prefix from a version string*
|
|
468
|
+
|
|
469
|
+
Strips the leading "v" from a git tag-style version string.
|
|
470
|
+
|
|
471
|
+
```typescript
|
|
472
|
+
stripV('v1.2.3')
|
|
473
|
+
// => '1.2.3'
|
|
474
|
+
```
|
|
475
|
+
|
|
476
|
+
*No-op when there is no v prefix*
|
|
477
|
+
|
|
478
|
+
Returns the string unchanged when it does not start with "v".
|
|
479
|
+
|
|
480
|
+
```typescript
|
|
481
|
+
stripV('1.2.3')
|
|
482
|
+
// => '1.2.3'
|
|
483
|
+
```
|
|
484
|
+
|
|
485
|
+
---
|