This release is 4 versions behind 1.0.5 — the latest version of @std/semver. Jump to latest
Built and signed on GitHub ActionsBuilt and signed on GitHub Actions
Built and signed on GitHub Actions
Parsing and comparing of semantic versions (SemVer)
This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers




JSR Score
100%
Published
9 months ago (1.0.1)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. // This module is browser compatible. import type { Comparator, Range, SemVer } from "./types.ts"; import { testComparatorSet } from "./_test_comparator_set.ts"; import { isWildcardComparator } from "./_shared.ts"; import { compare } from "./compare.ts"; /** * Check if the SemVer is less than the range. * * @example Usage * ```ts * import { parse, parseRange, lessThanRange } from "@std/semver"; * import { assert } from "@std/assert"; * * const v0 = parse("1.2.3"); * const v1 = parse("1.0.0"); * const range = parseRange(">=1.2.3 <1.2.4"); * * assert(!lessThanRange(v0, range)); * assert(lessThanRange(v1, range)); * ``` * * @param semver The version to check. * @param range The range to check against. * @returns `true` if the SemVer is less than the range, `false` otherwise. */ export function lessThanRange(semver: SemVer, range: Range): boolean { return range.every((comparatorSet) => lessThanComparatorSet(semver, comparatorSet) ); } function lessThanComparatorSet(semver: SemVer, comparatorSet: Comparator[]) { // If the comparator set contains wildcard, then the semver is not greater than the range. if (comparatorSet.some(isWildcardComparator)) return false; // If the SemVer satisfies the comparator set, then it's not less than the range. if (testComparatorSet(semver, comparatorSet)) return false; // If the SemVer is greater than any of the comparator set, then it's not less than the range. if ( comparatorSet.some((comparator) => greaterThanComparator(semver, comparator) ) ) return false; return true; } function greaterThanComparator( semver: SemVer, comparator: Comparator, ): boolean { const cmp = compare(semver, comparator); switch (comparator.operator) { case "=": case undefined: return cmp > 0; case "!=": return false; case ">": return false; case "<": return cmp >= 0; case ">=": return false; case "<=": return cmp > 0; } }