diff --git a/semver/is_comparator.ts b/semver/is_comparator.ts new file mode 100644 index 000000000000..cba27a31ef66 --- /dev/null +++ b/semver/is_comparator.ts @@ -0,0 +1,30 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. +import { isSemVer } from "./is_semver.ts"; +import { isValidOperator } from "./_shared.ts"; +import type { Comparator } from "./types.ts"; +import { ALL, NONE } from "./constants.ts"; + +/** + * Does a deep check on the value to see if it is a valid Comparator object. + * + * Objects with extra fields are still considered valid if they have at + * least the correct fields. + * + * Adds a type assertion if true. + * @param value The value to check if its a Comparator + * @returns True if the object is a Comparator otherwise false + */ +export function isComparator(value: unknown): value is Comparator { + if (value === null || value === undefined) return false; + if (value === NONE) return true; + if (value === ALL) return true; + if (Array.isArray(value)) return false; + if (typeof value !== "object") return false; + const { operator, semver, min, max } = value as Comparator; + return ( + isValidOperator(operator) && + isSemVer(semver) && + isSemVer(min) && + isSemVer(max) + ); +} diff --git a/semver/is_semver_comparator_test.ts b/semver/is_comparator_test.ts similarity index 88% rename from semver/is_semver_comparator_test.ts rename to semver/is_comparator_test.ts index a9bafe274708..92bd1a7987a9 100644 --- a/semver/is_semver_comparator_test.ts +++ b/semver/is_comparator_test.ts @@ -1,7 +1,7 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. import { assert } from "../assert/mod.ts"; import { INVALID, MIN } from "./constants.ts"; -import { isSemVerComparator } from "./is_semver_comparator.ts"; +import { isComparator } from "./is_comparator.ts"; Deno.test({ name: "valid_comparator", @@ -25,7 +25,7 @@ Deno.test({ await t.step( `valid_comparator_${(i++).toString().padStart(2, "0")}`, () => { - const actual = isSemVerComparator(c); + const actual = isComparator(c); assert(actual); }, ); diff --git a/semver/is_semver_comparator.ts b/semver/is_semver_comparator.ts index 23fd136aed7a..66281a2ca028 100644 --- a/semver/is_semver_comparator.ts +++ b/semver/is_semver_comparator.ts @@ -1,8 +1,5 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. -import { isSemVer } from "./is_semver.ts"; -import { isValidOperator } from "./_shared.ts"; -import type { SemVerComparator } from "./types.ts"; -import { ALL, NONE } from "./constants.ts"; +import { isComparator } from "./is_comparator.ts"; /** * Does a deep check on the value to see if it is a valid SemVerComparator object. @@ -13,18 +10,6 @@ import { ALL, NONE } from "./constants.ts"; * Adds a type assertion if true. * @param value The value to check if its a SemVerComparator * @returns True if the object is a SemVerComparator otherwise false + * @deprecated (will be removed in 0.212.0) Use {@linkcode isComparator} instead. */ -export function isSemVerComparator(value: unknown): value is SemVerComparator { - if (value === null || value === undefined) return false; - if (value === NONE) return true; - if (value === ALL) return true; - if (Array.isArray(value)) return false; - if (typeof value !== "object") return false; - const { operator, semver, min, max } = value as SemVerComparator; - return ( - isValidOperator(operator) && - isSemVer(semver) && - isSemVer(min) && - isSemVer(max) - ); -} +export const isSemVerComparator = isComparator; diff --git a/semver/types.ts b/semver/types.ts index 96db9e7b5aea..1d9d5c68b21e 100644 --- a/semver/types.ts +++ b/semver/types.ts @@ -46,6 +46,15 @@ export type FormatStyle = * The shape of a valid semantic version comparator * @example >=0.0.0 */ +export interface Comparator { + operator: Operator; + semver: SemVer; + min: SemVer; + max: SemVer; +} +/** + * @deprecated (will be removed in 0.212.0) Use {@linkcode Comparator} instead. + */ export interface SemVerComparator { operator: Operator; semver: SemVer;