-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrange.ts
More file actions
75 lines (70 loc) · 2.14 KB
/
range.ts
File metadata and controls
75 lines (70 loc) · 2.14 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
/**
* @fileoverview Range / set helpers — `satisfiesVersion` / `filterVersions`
* check membership against a semver range, `maxVersion` / `minVersion`
* pick the bounds of an arbitrary version array. The bound pickers use
* the vendored `semver` directly because the prerelease-inclusion
* options aren't part of the smol-versions surface.
*/
import { getSemver, impl } from './_internal'
/**
* Get all versions from an array that satisfy a semver range.
*
* @example
* ```typescript
* filterVersions(['1.0.0', '1.5.0', '2.0.0'], '>=1.0.0 <2.0.0')
* // ['1.0.0', '1.5.0']
* ```
*/
export function filterVersions(versions: string[], range: string): string[] {
/* c8 ignore next - External semver call */
return versions.filter(v => impl.satisfies(v, range))
}
/**
* Get the highest version from an array of versions.
*
* @example
* ```typescript
* maxVersion(['1.0.0', '2.0.0', '1.5.0']) // '2.0.0'
* ```
*/
export function maxVersion(versions: string[]): string | undefined {
/* c8 ignore next - External semver call */
const semver = getSemver()
// includePrerelease: true so an all-prerelease input like
// ['1.0.0-alpha', '1.0.0-beta'] resolves to the latest prerelease
// instead of returning undefined under semver's default (which filters
// prereleases out against '*').
return (
semver.maxSatisfying(versions, '*', { includePrerelease: true }) ||
undefined
)
}
/**
* Get the lowest version from an array of versions.
*
* @example
* ```typescript
* minVersion(['1.0.0', '2.0.0', '1.5.0']) // '1.0.0'
* ```
*/
export function minVersion(versions: string[]): string | undefined {
/* c8 ignore next - External semver call */
const semver = getSemver()
return (
semver.minSatisfying(versions, '*', { includePrerelease: true }) ||
undefined
)
}
/**
* Check if a version satisfies a semver range.
*
* @example
* ```typescript
* satisfiesVersion('1.5.0', '>=1.0.0 <2.0.0') // true
* satisfiesVersion('3.0.0', '>=1.0.0 <2.0.0') // false
* ```
*/
export function satisfiesVersion(version: string, range: string): boolean {
/* c8 ignore next - External semver call */
return impl.satisfies(version, range)
}