Skip to main content
Home
This release is 4 versions behind 1.0.5 — the latest version of @std/semver. Jump to latest

@std/semver@1.0.1
Built and signed on GitHub Actions

Parsing and comparing of semantic versions (SemVer)

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
100%
Published
9 months ago (1.0.1)
Package root>less_than_range.ts
// 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; } }