std/semver/can_parse.ts

28 lines
680 B
TypeScript
Raw Normal View History

// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
2023-10-24 04:39:55 +00:00
import { parse } from "./parse.ts";
/**
* Returns true if the string can be parsed as SemVer.
*
* @example Usage
* ```ts
* import { canParse } from "@std/semver/can-parse";
* import { assert, assertFalse } from "@std/assert";
*
* assert(canParse("1.2.3"));
* assertFalse(canParse("invalid"));
* ```
*
* @param value The version string to check
* @returns `true` if the string can be parsed as SemVer, `false` otherwise
*/
export function canParse(value: string): boolean {
2023-10-24 04:39:55 +00:00
try {
parse(value);
2023-10-24 04:39:55 +00:00
return true;
} catch {
2023-10-24 04:39:55 +00:00
return false;
}
}