All files / semver / try_parse_range.ts

100.00% Branches 1/1
100.00% Lines 8/8
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
 
 
 
 
x25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x25
x28
 
 
x28
x28
x29
x29
x28

































// Copyright 2018-2025 the Deno authors. MIT license.
// This module is browser compatible.

import type { Range } from "./types.ts";
import { parseRange } from "./parse_range.ts";

/**
 * Parses the given range string and returns a Range object. If the range string
 * is invalid, `undefined` is returned.
 *
 * @example Usage
 * ```ts
 * import { tryParseRange } from "@std/semver";
 * import { assertEquals } from "@std/assert";
 *
 * assertEquals(tryParseRange(">=1.2.3 <1.2.4"), [
 *  [
 *    { operator: ">=", major: 1, minor: 2, patch: 3, prerelease: [], build: [] },
 *    { operator: "<", major: 1, minor: 2, patch: 4, prerelease: [], build: [] },
 *  ],
 * ]);
 * ```
 *
 * @param value The range string
 * @returns A Range object if valid otherwise `undefined`
 */
export function tryParseRange(value: string): Range | undefined {
  try {
    // Return '*' instead of '' so that truthiness works.
    // This will throw if it's invalid anyway
    return parseRange(value);
  } catch {
    return undefined;
  }
}