compatRange

tcmts.core.semver.compatRange · #2hehwtlq · term · runs anywhere

The range of versions semver says are compatible with version, as npm writes it: the major (4 for 4.17.21), the minor below 1.0 (0.3 for 0.3.7, 0.0.4 for 0.0.4), and the exact version for a prerelease or anything that isn't semver, which promise nothing. Applied to a range it returns the range.

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

export function compatRange(version: string): string {
  if (/^\d+$/.test(version) || /^0\.\d+$/.test(version)) return version;
  const p = parse(version);
  if (!p || p.pre) return version;
  if (p.major > 0) return String(p.major);
  if (p.minor > 0) return `0.${p.minor}`;
  return `0.0.${p.patch}`;
}

uses

used by