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-01-14 11:16:56 +00:00
|
|
|
import type { SemVer } from "./types.ts";
|
2023-06-12 03:47:03 +00:00
|
|
|
|
|
|
|
function formatNumber(value: number) {
|
2024-07-31 06:26:57 +00:00
|
|
|
return value.toFixed(0);
|
2023-06-12 03:47:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Format a SemVer object into a string.
|
|
|
|
*
|
2024-05-27 10:03:20 +00:00
|
|
|
* @example Usage
|
|
|
|
* ```ts
|
|
|
|
* import { format } from "@std/semver/format";
|
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 semver = {
|
|
|
|
* major: 1,
|
|
|
|
* minor: 2,
|
|
|
|
* patch: 3,
|
|
|
|
* };
|
|
|
|
* assertEquals(format(semver), "1.2.3");
|
|
|
|
* ```
|
|
|
|
*
|
2024-08-19 04:33:42 +00:00
|
|
|
* @param version The SemVer to format
|
2023-06-12 03:47:03 +00:00
|
|
|
* @returns The string representation of a semantic version.
|
|
|
|
*/
|
2024-08-19 04:33:42 +00:00
|
|
|
export function format(version: SemVer): string {
|
|
|
|
const major = formatNumber(version.major);
|
|
|
|
const minor = formatNumber(version.minor);
|
|
|
|
const patch = formatNumber(version.patch);
|
|
|
|
const pre = version.prerelease?.join(".") ?? "";
|
|
|
|
const build = version.build?.join(".") ?? "";
|
2023-06-12 03:47:03 +00:00
|
|
|
|
|
|
|
const primary = `${major}.${minor}.${patch}`;
|
|
|
|
const release = [primary, pre].filter((v) => v).join("-");
|
2024-01-14 11:16:56 +00:00
|
|
|
return [release, build].filter((v) => v).join("+");
|
2023-06-12 03:47:03 +00:00
|
|
|
}
|