All files / semver / less_than_range.ts

100.00% Branches 21/21
100.00% Functions 3/3
100.00% Lines 36/36
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
 
 
 
 
x24
x24
x24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x24
x133
x133
 
x133
 
x139
 
x139
 
x139
 
x139
x139
x139
 
x139
x60
x139
 
x124
x124
x124
 
x124
x124
x124
x124
x9
x124
x1
x124
x3
x124
x47
x124
x61
x124
x3
x124
x124



































































// Copyright 2018-2026 the Deno authors. 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 version1 = parse("1.2.3");
 * const version2 = parse("1.0.0");
 * const range = parseRange(">=1.2.3 <1.2.4");
 *
 * assert(!lessThanRange(version1, range));
 * assert(lessThanRange(version2, range));
 * ```
 *
 * @param version 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(version: SemVer, range: Range): boolean {
  return range.every((comparatorSet) =>
    lessThanComparatorSet(version, comparatorSet)
  );
}

function lessThanComparatorSet(version: 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(version, 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(version, comparator)
    )
  ) return false;
  return true;
}

function greaterThanComparator(
  version: SemVer,
  comparator: Comparator,
): boolean {
  const cmp = compare(version, comparator);
  switch (comparator.operator) {
    case "=":
    case undefined:
      return cmp > 0;
    case "!=":
      return cmp <= 0;
    case ">":
      return false;
    case "<":
      return cmp >= 0;
    case ">=":
      return false;
    case "<=":
      return cmp > 0;
  }
}