2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2024-04-10 02:43:44 +00:00
|
|
|
// This module is browser compatible.
|
2024-02-27 21:57:25 +00:00
|
|
|
import type { SemVer } from "./types.ts";
|
2023-12-30 09:43:56 +00:00
|
|
|
import { parseBuild, parseNumber, parsePrerelease } from "./_shared.ts";
|
2023-12-13 20:08:35 +00:00
|
|
|
import { FULL_REGEXP, MAX_LENGTH } from "./_shared.ts";
|
2023-06-12 03:47:03 +00:00
|
|
|
|
|
|
|
/**
|
2024-06-28 05:47:34 +00:00
|
|
|
* Attempt to parse a string as a semantic version, returning a SemVer object.
|
2024-05-27 10:03:20 +00:00
|
|
|
*
|
|
|
|
* @example Usage
|
|
|
|
* ```ts
|
|
|
|
* import { parse } from "@std/semver/parse";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2024-05-27 10:03:20 +00:00
|
|
|
*
|
|
|
|
* const version = parse("1.2.3");
|
|
|
|
* assertEquals(version, {
|
|
|
|
* major: 1,
|
|
|
|
* minor: 2,
|
|
|
|
* patch: 3,
|
|
|
|
* prerelease: [],
|
|
|
|
* build: [],
|
|
|
|
* });
|
|
|
|
* ```
|
|
|
|
*
|
2024-06-28 05:47:34 +00:00
|
|
|
* @throws {TypeError} If the input string is invalid.
|
2024-08-19 04:33:42 +00:00
|
|
|
* @param value The version string to parse
|
2023-06-12 03:47:03 +00:00
|
|
|
* @returns A valid SemVer
|
|
|
|
*/
|
2024-08-19 04:33:42 +00:00
|
|
|
export function parse(value: string): SemVer {
|
|
|
|
if (typeof value !== "string") {
|
2023-06-12 03:47:03 +00:00
|
|
|
throw new TypeError(
|
2024-08-23 03:39:56 +00:00
|
|
|
`Cannot parse version as version must be a string: received ${typeof value}`,
|
2023-06-12 03:47:03 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-08-19 04:33:42 +00:00
|
|
|
if (value.length > MAX_LENGTH) {
|
2023-06-12 03:47:03 +00:00
|
|
|
throw new TypeError(
|
2024-08-23 03:39:56 +00:00
|
|
|
`Cannot parse version as version length is too long: length is ${value.length}, max length is ${MAX_LENGTH}`,
|
2023-06-12 03:47:03 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-08-19 04:33:42 +00:00
|
|
|
value = value.trim();
|
2023-06-12 03:47:03 +00:00
|
|
|
|
2024-08-19 04:33:42 +00:00
|
|
|
const groups = value.match(FULL_REGEXP)?.groups;
|
2024-08-23 03:39:56 +00:00
|
|
|
if (!groups) throw new TypeError(`Cannot parse version: ${value}`);
|
2023-06-12 03:47:03 +00:00
|
|
|
|
2024-08-23 03:39:56 +00:00
|
|
|
const major = parseNumber(
|
|
|
|
groups.major!,
|
|
|
|
`Cannot parse version ${value}: invalid major version`,
|
|
|
|
);
|
|
|
|
const minor = parseNumber(
|
|
|
|
groups.minor!,
|
|
|
|
`Cannot parse version ${value}: invalid minor version`,
|
|
|
|
);
|
|
|
|
const patch = parseNumber(
|
|
|
|
groups.patch!,
|
|
|
|
`Cannot parse version ${value}: invalid patch version`,
|
|
|
|
);
|
2023-06-12 03:47:03 +00:00
|
|
|
|
2023-12-30 09:43:56 +00:00
|
|
|
const prerelease = groups.prerelease
|
|
|
|
? parsePrerelease(groups.prerelease)
|
|
|
|
: [];
|
|
|
|
const build = groups.buildmetadata ? parseBuild(groups.buildmetadata) : [];
|
2023-06-12 03:47:03 +00:00
|
|
|
|
2024-08-19 04:33:42 +00:00
|
|
|
return { major, minor, patch, prerelease, build };
|
2023-06-12 03:47:03 +00:00
|
|
|
}
|