compareVersions

tcmts.core.semver.compareVersions · #o747y5uy · term · runs anywhere

Order versions: negative when a comes first. Prereleases come before their release.

import { parse } from "#codebase/tcmts/core/semver";

export function compareVersions(a: string, b: string): number {
  const x = parse(a);
  const y = parse(b);
  if (!x || !y) return a < b ? -1 : a > b ? 1 : 0;
  const d = x.major - y.major || x.minor - y.minor || x.patch - y.patch;
  if (d) return d;
  if (x.pre === y.pre) return 0;
  if (!x.pre) return 1;
  if (!y.pre) return -1;
  return x.pre < y.pre ? -1 : 1;
}

uses

used by