2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2024-04-10 02:43:44 +00:00
|
|
|
// This module is browser compatible.
|
2024-02-09 23:30:39 +00:00
|
|
|
import type { Range, SemVer } from "./types.ts";
|
2024-04-24 19:38:52 +00:00
|
|
|
import { satisfies } from "./satisfies.ts";
|
2024-02-02 06:00:47 +00:00
|
|
|
import { lessThan } from "./less_than.ts";
|
2023-06-12 03:47:03 +00:00
|
|
|
|
|
|
|
/**
|
2024-06-28 05:47:34 +00:00
|
|
|
* Returns the lowest SemVer in the list that satisfies the range, or `undefined` if
|
2023-06-12 03:47:03 +00:00
|
|
|
* none of them do.
|
2024-05-27 10:03:20 +00:00
|
|
|
*
|
|
|
|
* @example Usage
|
|
|
|
* ```ts
|
|
|
|
* import { parse, parseRange, minSatisfying } from "@std/semver";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2024-05-27 10:03:20 +00:00
|
|
|
*
|
|
|
|
* const versions = ["0.2.0", "1.2.3", "1.3.0", "2.0.0", "2.1.0"].map(parse);
|
|
|
|
* const range = parseRange(">=1.0.0 <2.0.0");
|
|
|
|
*
|
|
|
|
* assertEquals(minSatisfying(versions, range), parse("1.2.3"));
|
|
|
|
* ```
|
|
|
|
*
|
2023-06-12 03:47:03 +00:00
|
|
|
* @param versions The versions to check.
|
|
|
|
* @param range The range of possible versions to compare to.
|
|
|
|
* @returns The lowest version in versions that satisfies the range.
|
|
|
|
*/
|
|
|
|
export function minSatisfying(
|
|
|
|
versions: SemVer[],
|
2024-02-09 23:30:39 +00:00
|
|
|
range: Range,
|
2023-06-12 03:47:03 +00:00
|
|
|
): SemVer | undefined {
|
2024-01-04 23:01:26 +00:00
|
|
|
let min;
|
|
|
|
for (const version of versions) {
|
2024-04-24 19:38:52 +00:00
|
|
|
if (!satisfies(version, range)) continue;
|
2024-02-02 06:00:47 +00:00
|
|
|
min = min && lessThan(min, version) ? min : version;
|
2024-01-04 23:01:26 +00:00
|
|
|
}
|
|
|
|
return min;
|
2023-06-12 03:47:03 +00:00
|
|
|
}
|