Skip to content

stringify

Reconstructs a version string from a AnyParsedVersion object — the scheme is read from the object’s own scheme field, not passed separately (a ParsedGentooVersion can only ever stringify as Gentoo, so there’s nothing to disambiguate).

This is the inverse of parse: stringify(parse(v)) === stripV(v) for any valid version string v, in any scheme — up to canonicalization (a Debian 0: epoch is dropped, a Gentoo _beta0 becomes _beta).

Available since v2.0.0

import { stringify } from '@helpers4/version';
// or, from the all-in-one package (same code, one install):
import { stringify } from 'helpers4/version';
stringify(parsed: AnyParsedVersion): string
ParameterTypeDescription
parsedAnyParsedVersionA parsed version object, as returned by parse.

string — The reconstructed version string (without leading v).

Converts a ParsedVersion object back to a version string.

stringify({ scheme: 'semver', major: 1, minor: 2, patch: 3, prerelease: [], build: [] })
// => '1.2.3'

stringify(parse(v)) returns the original version string (without leading v).

stringify(parse('2.0.0-alpha.1'))
// => '2.0.0-alpha.1'

stringify(parse('1.0.0-beta+exp.sha.5114f85'))
// => '1.0.0-beta+exp.sha.5114f85'

The scheme is read from the parsed object itself, not passed separately.

stringify({ scheme: 'gentoo', components: [1, 2, 3], letter: 'b', suffixes: [{ type: 'rc', number: 1 }], revision: 2 })
// => '1.2.3b_rc1-r2'

A version parsed by parse, in whichever scheme it was parsed as — narrow on the scheme field to access scheme-specific properties (major/minor/patch for 'semver', components/letter/suffixes/revision for 'gentoo', epoch/upstream/revision for 'debian').

Deliberately not named ParsedVersion: that name has been public API since 2.0.0 for the flat SemVer-only shape (.major/.minor/.patch directly, no narrowing needed), and turning it into a union would silently break any existing code typed as ParsedVersion that reads those fields without narrowing first — a real compatibility break with no MIGRATION.md entry, since this repo ties breaking changes to major-version bumps.

type AnyParsedVersion = ParsedSemVerVersion | ParsedGentooVersion | ParsedDebianVersion

View source on GitHub