-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.ts
More file actions
118 lines (110 loc) · 3.1 KB
/
parse.ts
File metadata and controls
118 lines (110 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
* @fileoverview Parsing helpers — `coerceVersion` rounds a sloppy
* input ("1.2") up to a valid semver triple, `parseVersion` returns
* `{major, minor, patch, prerelease, build}`, and the `getMajor*` /
* `getMinor*` / `getPatchVersion` accessors return a single
* component. `isValidVersion` is also here because validation is
* effectively "did parse succeed". All go through the vendored
* `semver` directly — smol-versions doesn't expose the parsed shape.
*/
import { getSemver, impl } from './_internal'
import type { ParsedVersion } from './types'
/**
* Coerce a version string to valid semver format.
*
* @example
* ```typescript
* coerceVersion('1.2') // '1.2.0'
* coerceVersion('v3') // '3.0.0'
* coerceVersion('abc') // undefined
* ```
*/
export function coerceVersion(version: string): string | undefined {
/* c8 ignore next - External semver call */
const semver = getSemver()
const coerced = semver.coerce(version)
return coerced?.version
}
/**
* Get the major version number from a version string.
*
* @example
* ```typescript
* getMajorVersion('3.2.1') // 3
* ```
*/
export function getMajorVersion(version: string): number | undefined {
/* c8 ignore next - External semver call */
const semver = getSemver()
const parsed = semver.parse(version)
return parsed?.major
}
/**
* Get the minor version number from a version string.
*
* @example
* ```typescript
* getMinorVersion('3.2.1') // 2
* ```
*/
export function getMinorVersion(version: string): number | undefined {
/* c8 ignore next - External semver call */
const semver = getSemver()
const parsed = semver.parse(version)
return parsed?.minor
}
/**
* Get the patch version number from a version string.
*
* @example
* ```typescript
* getPatchVersion('3.2.1') // 1
* ```
*/
export function getPatchVersion(version: string): number | undefined {
/* c8 ignore next - External semver call */
const semver = getSemver()
const parsed = semver.parse(version)
return parsed?.patch
}
/**
* Validate if a string is a valid semantic version.
*
* @example
* ```typescript
* isValidVersion('1.2.3') // true
* isValidVersion('abc') // false
* ```
*/
export function isValidVersion(version: string): boolean {
/* c8 ignore next - External semver call */
// semver.valid returns string | null; smol-versions returns
// string | undefined. Both branch correctly under loose-equality
// truthiness check, so a generic `!= null` (loose) covers both.
// eslint-disable-next-line eqeqeq
return impl.valid(version) != null
}
/**
* Parse a version string and return major, minor, patch components.
*
* @example
* ```typescript
* parseVersion('1.2.3')
* // { major: 1, minor: 2, patch: 3, prerelease: [], build: [] }
* ```
*/
export function parseVersion(version: string): ParsedVersion | undefined {
/* c8 ignore next - External semver call */
const semver = getSemver()
const parsed = semver.parse(version)
if (!parsed) {
return undefined
}
return {
major: parsed.major,
minor: parsed.minor,
patch: parsed.patch,
prerelease: parsed.prerelease,
build: parsed.build,
}
}