/** * Semantic versions (https://semver.org): `1.4.2`, `2.0.0-rc.1`, * `1.0.0+build.5`. * * `compareTo` orders versions by semver precedence, which ignores build * metadata, so `1.0.0+a` and `1.0.0+b` are equal in order and different * in text. */ /** Text that is not a version or a range. */ export class VersionError extends Error { new(message: String) : super(message); } /** 2^53 - 1, so that every version number reads the same in JSON. */ let MAX_NUMBER: i64 = 0x1fffffffffffff; /** A semantic version. */ export final class Version { major: i64; minor: i64; patch: i64; /** * The pre-release identifiers: `['rc', '1']` for `2.0.0-rc.1`, and none * for a release. */ prerelease: FixedArray; /** The build metadata's identifiers: `['build', '5']` for `1.0.0+build.5`. */ build: FixedArray; new(major: i64, minor: i64, patch: i64, prerelease: FixedArray, build: FixedArray = new FixedArray(0, '')) : major = major, minor = minor, patch = patch, prerelease = prerelease, build = build; /** * Reads a version such as `1.4.2`, `2.0.0-rc.1` or `1.0.0+build.5`. * Throws `VersionError` for anything else. */ static parse(text: String): Version { var coreEnd = text.length; var buildStart = text.length; for (var i = 0; i < text.length; i += 1) { let b = text.getByteAt(i); if (b == 43 && buildStart == text.length) { // the first '+' buildStart = i; if (coreEnd == text.length) { coreEnd = i; } } if (b == 45 && coreEnd == text.length) { // the first '-' coreEnd = i; } } let core = text.sliceBytes(0, coreEnd).split('.'); if (core.length != 3) { throw new VersionError(`'${text}' is not a version: expected major.minor.patch`); } var prerelease = new FixedArray(0, ''); if (coreEnd < buildStart) { prerelease = parsePrerelease(text.sliceBytes(coreEnd + 1, buildStart), text); } var build = new FixedArray(0, ''); if (buildStart < text.length) { build = parseBuild(text.sliceBytes(buildStart + 1, text.length), text); } return new Version(parseVersionNumber(core[0], text), parseVersionNumber(core[1], text), parseVersionNumber(core[2], text), prerelease, build); } /** Whether this is a pre-release, such as `2.0.0-rc.1`. */ isPrerelease: boolean { get { return this.prerelease.length > 0; } } /** * The line of versions semver lets replace this one without a breaking * change, as `^` reads it: the major version, `1` for `1.4.2`; or, before * 1.0, the first number that is not zero and those before it, `0.2` for * `0.2.3` and `0.0.3` for `0.0.3`, since a breaking change may raise the * minor version then, or the patch version before 0.1. Two versions on * one line are one dependency at two versions; on two lines, two * dependencies. */ compatibleLine(): String { if (this.major != (0 as i64)) { return `${this.major}`; } if (this.minor != (0 as i64)) { return `0.${this.minor}`; } return `0.0.${this.patch}`; } /** * Negative when this version has lower precedence than `other`, zero * when they are equal, and positive when it has higher precedence. Build * metadata does not count. */ compareTo(other: Version): i32 { if (this.major != other.major) { return if (this.major < other.major) -1 else 1; } if (this.minor != other.minor) { return if (this.minor < other.minor) -1 else 1; } if (this.patch != other.patch) { return if (this.patch < other.patch) -1 else 1; } let a = this.prerelease; let b = other.prerelease; // A release has higher precedence than its pre-releases. if (a.length == 0 || b.length == 0) { return b.length - a.length; } let n = if (a.length < b.length) a.length else b.length; for (var i = 0; i < n; i += 1) { let order = compareIdentifiers(a[i], b[i]); if (order != 0) { return order; } } return a.length - b.length; } /** Whether this version and `other` have the same major, minor and patch numbers. */ hasSameCore(other: Version): boolean { return this.major == other.major && this.minor == other.minor && this.patch == other.patch; } toString(): String { var text = `${this.major}.${this.minor}.${this.patch}`; for (var i = 0; i < this.prerelease.length; i += 1) { text = `${text}${if (i == 0) '-' else '.'}${this.prerelease[i]}`; } for (var i = 0; i < this.build.length; i += 1) { text = `${text}${if (i == 0) '+' else '.'}${this.build[i]}`; } return text; } } /** Reads a major, minor or patch number of `text`: digits without a leading zero. */ export function parseVersionNumber(part: String, text: String): i64 { if (!isNumeric(part) || (part.length > 1 && part.getByteAt(0) == 48)) { throw new VersionError(`'${text}' is not a version: '${part}' is not a number without leading zeros`); } if (part.length > 16) { throw new VersionError(`'${text}' is not a version: '${part}' is too large`); } var n: i64 = 0; for (var i = 0; i < part.length; i += 1) { n = n * 10 + (part.getByteAt(i) - 48); } if (n > MAX_NUMBER) { throw new VersionError(`'${text}' is not a version: '${part}' is too large`); } return n; } /** Reads the pre-release identifiers `part` of `text`, after its `-`. */ export function parsePrerelease(part: String, text: String): FixedArray { let identifiers = part.split('.'); for (let identifier in identifiers) { checkIdentifier(identifier, text); if (isNumeric(identifier) && identifier.length > 1 && identifier.getByteAt(0) == 48) { throw new VersionError(`'${text}' is not a version: '${identifier}' has a leading zero`); } } return identifiers; } /** * Reads the build metadata `part` of `text`, after its `+`. Unlike * pre-release identifiers, these may have leading zeros. */ export function parseBuild(part: String, text: String): FixedArray { let identifiers = part.split('.'); for (let identifier in identifiers) { checkIdentifier(identifier, text); } return identifiers; } /** Checks that an identifier of `text` is ASCII letters, digits and `-`, and not empty. */ function checkIdentifier(identifier: String, text: String): void { if (identifier.length == 0) { throw new VersionError(`'${text}' is not a version: an identifier is empty`); } for (var i = 0; i < identifier.length; i += 1) { let b = identifier.getByteAt(i); let alphanumeric = (b >= 48 && b <= 57) || (b >= 65 && b <= 90) || (b >= 97 && b <= 122); if (!alphanumeric && b != 45) { throw new VersionError(`'${text}' is not a version: '${identifier}' has a character other than letters, digits and '-'`); } } } /** Whether `text` is one or more ASCII digits. */ export function isNumeric(text: String): boolean { if (text.length == 0) { return false; } for (var i = 0; i < text.length; i += 1) { let b = text.getByteAt(i); if (b < 48 || b > 57) { return false; } } return true; } /** * Semver precedence between two pre-release identifiers: numeric ones * compare as numbers and come before alphanumeric ones, which compare * byte by byte. */ function compareIdentifiers(a: String, b: String): i32 { let aNumeric = isNumeric(a); let bNumeric = isNumeric(b); if (aNumeric != bNumeric) { return if (aNumeric) -1 else 1; } // Numeric identifiers have no leading zeros, so the longer is larger. if (aNumeric && a.length != b.length) { return a.length - b.length; } let n = if (a.length < b.length) a.length else b.length; for (var i = 0; i < n; i += 1) { let diff = a.getByteAt(i) - b.getByteAt(i); if (diff != 0) { return diff; } } return a.length - b.length; }