🏷️ Parses a Semantic Versioning 2.0.0 string.

Info

A leading v and surrounding whitespace are tolerated, because that is how versions appear in git tags and changelogs.

Syntax

TypeScript
import { semverParse } from '@opentf/std';

semverParse(version: string): Semver;

interface Semver {
  major: number;
  minor: number;
  patch: number;
  prerelease: (string | number)[];
  build: string[];
}

Parameters

NameTypeDescription
versionstringThe version string to parse.

Returns

The parsed version. Numeric pre-release identifiers come back as numbers, so they compare numerically rather than as strings.

Throws a TypeError if the string is not a valid version. Use semverIsValid when you want a boolean instead.

Examples

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

semverParse('v2.0.0-alpha.1+build.5')
//=> { major: 2, minor: 0, patch: 0, prerelease: ['alpha', 1], build: ['build', '5'] }

Partial versions and ranges are not versions:

TypeScript
semverParse('1.2')    // throws TypeError
semverParse('^1.2.3') // throws TypeError
semverParse('01.2.3') // throws TypeError — leading zeroes are invalid
Last updated on
Edit this page