Skip to main content
Using Deno in production at your company? Earn free Deno merch.
Give us feedback
Module

std/semver/format.ts

The Deno Standard Library
Go to Latest
File
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.// This module is browser compatible.import type { SemVer } from "./types.ts";import { isWildcardComparator } from "./_shared.ts";
function formatNumber(value: number) { if (value === Number.POSITIVE_INFINITY) { return "∞"; } else if (value === Number.NEGATIVE_INFINITY) { return "⧞"; } else { return value.toFixed(0); }}
/** * Format a SemVer object into a string. * * If any number is NaN then NaN will be printed. * * If any number is positive or negative infinity then '∞' or '⧞' will be printed instead. * * @param semver The semantic version to format * @returns The string representation of a semantic version. */export function format(semver: SemVer): string { if (isWildcardComparator(semver)) { return "*"; }
const major = formatNumber(semver.major); const minor = formatNumber(semver.minor); const patch = formatNumber(semver.patch); const pre = semver.prerelease?.join(".") ?? ""; const build = semver.build?.join(".") ?? "";
const primary = `${major}.${minor}.${patch}`; const release = [primary, pre].filter((v) => v).join("-"); return [release, build].filter((v) => v).join("+");}