2024-04-24 19:38:52 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
|
|
|
import type { Range, SemVer } from "./types.ts";
|
|
|
|
import { testComparatorSet } from "./_test_comparator_set.ts";
|
|
|
|
|
|
|
|
/**
|
2024-06-28 05:47:34 +00:00
|
|
|
* Test to see if the SemVer satisfies the range.
|
2024-05-27 10:03:20 +00:00
|
|
|
*
|
|
|
|
* @example Usage
|
|
|
|
* ```ts
|
|
|
|
* import { parse, parseRange, satisfies } 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 { assert } from "@std/assert";
|
2024-05-27 10:03:20 +00:00
|
|
|
*
|
|
|
|
* const version = parse("1.2.3");
|
|
|
|
* const range0 = parseRange(">=1.0.0 <2.0.0");
|
|
|
|
* const range1 = parseRange(">=1.0.0 <1.3.0");
|
|
|
|
* const range2 = parseRange(">=1.0.0 <1.2.3");
|
|
|
|
*
|
|
|
|
* assert(satisfies(version, range0));
|
|
|
|
* assert(satisfies(version, range1));
|
2024-06-28 05:47:34 +00:00
|
|
|
* assert(!satisfies(version, range2));
|
2024-05-27 10:03:20 +00:00
|
|
|
* ```
|
2024-04-24 19:38:52 +00:00
|
|
|
* @param version The version to test
|
|
|
|
* @param range The range to check
|
|
|
|
* @returns true if the version is in the range
|
|
|
|
*/
|
2024-08-19 04:33:42 +00:00
|
|
|
export function satisfies(version: SemVer, range: Range): boolean {
|
2024-04-24 19:38:52 +00:00
|
|
|
return range.some((set) => testComparatorSet(version, set));
|
|
|
|
}
|