deno.land / std@0.224.0 / semver / parse.ts

View Documentation
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
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.// This module is browser compatible.import type { SemVer } from "./types.ts";import { parseBuild, parseNumber, parsePrerelease } from "./_shared.ts";import { FULL_REGEXP, MAX_LENGTH } from "./_shared.ts";
/** * Attempt to parse a string as a semantic version, returning either a `SemVer` * object or throws a TypeError. * @param version The version string to parse * @returns A valid SemVer */export function parse(version: string): SemVer { if (typeof version !== "string") { throw new TypeError( `version must be a string`, ); }
if (version.length > MAX_LENGTH) { throw new TypeError( `version is longer than ${MAX_LENGTH} characters`, ); }
version = version.trim();
const groups = version.match(FULL_REGEXP)?.groups; if (!groups) throw new TypeError(`Invalid Version: ${version}`);
const major = parseNumber(groups.major!, "Invalid major version"); const minor = parseNumber(groups.minor!, "Invalid minor version"); const patch = parseNumber(groups.patch!, "Invalid patch version");
const prerelease = groups.prerelease ? parsePrerelease(groups.prerelease) : []; const build = groups.buildmetadata ? parseBuild(groups.buildmetadata) : [];
return { major, minor, patch, prerelease, build, };}
std

Version Info

Tagged at
6 months ago