forked from denoland/std
-
Notifications
You must be signed in to change notification settings - Fork 0
/
range_max.ts
21 lines (20 loc) · 774 Bytes
/
range_max.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
import { INVALID } from "./constants.ts";
import { sort } from "./sort.ts";
import type { SemVer, SemVerRange } from "./types.ts";
import { testRange } from "./test_range.ts";
/**
* The maximum valid SemVer for a given range or INVALID
* @param range The range to calculate the max for
* @returns A valid SemVer or INVALID
*/
export function rangeMax(range: SemVerRange): SemVer | undefined {
// For and's, you take the smallest max
// For or's, you take the biggest max
//[ [1 and 2] or [2 and 3] ] = [ 1 or 2 ] = 2
return sort(
range.ranges.map((r) =>
sort(r.filter((c) => testRange(c.max, range)).map((c) => c.max)).shift()!
),
).filter((v) => v).pop() ?? INVALID;
}