Skip to content

compare

Compares two semantic version strings according to SemVer 2.0.0 specification

Supports:

  • Core version: MAJOR.MINOR.PATCH
  • Pre-release: -alpha, -beta.1, -rc.1, etc.
  • Build metadata: +build, +sha.abc123 (ignored in comparison per spec)
  • Optional ‘v’ prefix

Available since v1.9.0

import { compare } from '@helpers4/version';
compare(version1: string, version2: string): number
ParameterTypeDescription
version1stringFirst version string
version2stringSecond version string

number — -1 if version1 < version2, 0 if equal, 1 if version1 > version2

Returns -1, 0, or 1 based on SemVer ordering.

compare('1.0.0', '2.0.0') // => -1
compare('1.0.0', '1.0.0') // => 0
compare('2.0.0', '1.0.0') // => 1

A prerelease version is always less than the release.

compare('1.0.0-alpha', '1.0.0')
// => -1

View source on GitHub