Skip to main content
Module

x/semver/mod.ts

The semver parser for Deno (a fork of node-semver using TypeScript)
Extremely Popular
Go to Latest
File
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800
export type ReleaseType = | "pre" | "major" | "premajor" | "minor" | "preminor" | "patch" | "prepatch" | "prerelease";
export type Operator = | "===" | "!==" | "" | "=" | "==" | "!=" | ">" | ">=" | "<" | "<=";
export interface Options { loose?: boolean; includePrerelease?: boolean;}
// Note: this is the semver.org version of the spec that it implements// Not necessarily the package version of this code.export const SEMVER_SPEC_VERSION = "2.0.0";
const MAX_LENGTH: number = 256;
// Max safe segment length for coercion.const MAX_SAFE_COMPONENT_LENGTH: number = 16;
// The actual regexpsconst re: RegExp[] = [];const src: string[] = [];let R: number = 0;
// The following Regular Expressions can be used for tokenizing,// validating, and parsing SemVer version strings.
// ## Numeric Identifier// A single `0`, or a non-zero digit followed by zero or more digits.
const NUMERICIDENTIFIER: number = R++;src[NUMERICIDENTIFIER] = "0|[1-9]\\d*";const NUMERICIDENTIFIERLOOSE: number = R++;src[NUMERICIDENTIFIERLOOSE] = "[0-9]+";
// ## Non-numeric Identifier// Zero or more digits, followed by a letter or hyphen, and then zero or// more letters, digits, or hyphens.
const NONNUMERICIDENTIFIER: number = R++;src[NONNUMERICIDENTIFIER] = "\\d*[a-zA-Z-][a-zA-Z0-9-]*";
// ## Main Version// Three dot-separated numeric identifiers.
const MAINVERSION: number = R++;const nid = src[NUMERICIDENTIFIER];src[MAINVERSION] = `(${nid})\\.(${nid})\\.(${nid})`;
const MAINVERSIONLOOSE: number = R++;const nidl = src[NUMERICIDENTIFIERLOOSE];src[MAINVERSIONLOOSE] = `(${nidl})\\.(${nidl})\\.(${nidl})`;
// ## Pre-release Version Identifier// A numeric identifier, or a non-numeric identifier.
const PRERELEASEIDENTIFIER: number = R++;src[PRERELEASEIDENTIFIER] = "(?:" + src[NUMERICIDENTIFIER] + "|" + src[NONNUMERICIDENTIFIER] + ")";
const PRERELEASEIDENTIFIERLOOSE: number = R++;src[PRERELEASEIDENTIFIERLOOSE] = "(?:" + src[NUMERICIDENTIFIERLOOSE] + "|" + src[NONNUMERICIDENTIFIER] + ")";
// ## Pre-release Version// Hyphen, followed by one or more dot-separated pre-release version// identifiers.
const PRERELEASE: number = R++;src[PRERELEASE] = "(?:-(" + src[PRERELEASEIDENTIFIER] + "(?:\\." + src[PRERELEASEIDENTIFIER] + ")*))";
const PRERELEASELOOSE: number = R++;src[PRERELEASELOOSE] = "(?:-?(" + src[PRERELEASEIDENTIFIERLOOSE] + "(?:\\." + src[PRERELEASEIDENTIFIERLOOSE] + ")*))";
// ## Build Metadata Identifier// Any combination of digits, letters, or hyphens.
const BUILDIDENTIFIER: number = R++;src[BUILDIDENTIFIER] = "[0-9A-Za-z-]+";
// ## Build Metadata// Plus sign, followed by one or more period-separated build metadata// identifiers.
const BUILD: number = R++;src[BUILD] = "(?:\\+(" + src[BUILDIDENTIFIER] + "(?:\\." + src[BUILDIDENTIFIER] + ")*))";
// ## Full Version String// A main version, followed optionally by a pre-release version and// build metadata.
// Note that the only major, minor, patch, and pre-release sections of// the version string are capturing groups. The build metadata is not a// capturing group, because it should not ever be used in version// comparison.
const FULL: number = R++;const FULLPLAIN = "v?" + src[MAINVERSION] + src[PRERELEASE] + "?" + src[BUILD] + "?";
src[FULL] = "^" + FULLPLAIN + "$";
// like full, but allows v1.2.3 and =1.2.3, which people do sometimes.// also, 1.0.0alpha1 (prerelease without the hyphen) which is pretty// common in the npm registry.const LOOSEPLAIN: string = "[v=\\s]*" + src[MAINVERSIONLOOSE] + src[PRERELEASELOOSE] + "?" + src[BUILD] + "?";
const LOOSE: number = R++;src[LOOSE] = "^" + LOOSEPLAIN + "$";
const GTLT: number = R++;src[GTLT] = "((?:<|>)?=?)";
// Something like "2.*" or "1.2.x".// Note that "x.x" is a valid xRange identifer, meaning "any version"// Only the first item is strictly required.const XRANGEIDENTIFIERLOOSE: number = R++;src[XRANGEIDENTIFIERLOOSE] = src[NUMERICIDENTIFIERLOOSE] + "|x|X|\\*";const XRANGEIDENTIFIER: number = R++;src[XRANGEIDENTIFIER] = src[NUMERICIDENTIFIER] + "|x|X|\\*";
const XRANGEPLAIN: number = R++;src[XRANGEPLAIN] = "[v=\\s]*(" + src[XRANGEIDENTIFIER] + ")" + "(?:\\.(" + src[XRANGEIDENTIFIER] + ")" + "(?:\\.(" + src[XRANGEIDENTIFIER] + ")" + "(?:" + src[PRERELEASE] + ")?" + src[BUILD] + "?" + ")?)?";
const XRANGEPLAINLOOSE: number = R++;src[XRANGEPLAINLOOSE] = "[v=\\s]*(" + src[XRANGEIDENTIFIERLOOSE] + ")" + "(?:\\.(" + src[XRANGEIDENTIFIERLOOSE] + ")" + "(?:\\.(" + src[XRANGEIDENTIFIERLOOSE] + ")" + "(?:" + src[PRERELEASELOOSE] + ")?" + src[BUILD] + "?" + ")?)?";
const XRANGE: number = R++;src[XRANGE] = "^" + src[GTLT] + "\\s*" + src[XRANGEPLAIN] + "$";const XRANGELOOSE = R++;src[XRANGELOOSE] = "^" + src[GTLT] + "\\s*" + src[XRANGEPLAINLOOSE] + "$";
// Coercion.// Extract anything that could conceivably be a part of a valid semverconst COERCE: number = R++;src[COERCE] = "(?:^|[^\\d])" + "(\\d{1," + MAX_SAFE_COMPONENT_LENGTH + "})" + "(?:\\.(\\d{1," + MAX_SAFE_COMPONENT_LENGTH + "}))?" + "(?:\\.(\\d{1," + MAX_SAFE_COMPONENT_LENGTH + "}))?" + "(?:$|[^\\d])";
// Tilde ranges.// Meaning is "reasonably at or greater than"const LONETILDE: number = R++;src[LONETILDE] = "(?:~>?)";
const TILDETRIM: number = R++;src[TILDETRIM] = "(\\s*)" + src[LONETILDE] + "\\s+";re[TILDETRIM] = new RegExp(src[TILDETRIM], "g");const tildeTrimReplace: string = "$1~";
const TILDE: number = R++;src[TILDE] = "^" + src[LONETILDE] + src[XRANGEPLAIN] + "$";const TILDELOOSE: number = R++;src[TILDELOOSE] = "^" + src[LONETILDE] + src[XRANGEPLAINLOOSE] + "$";
// Caret ranges.// Meaning is "at least and backwards compatible with"const LONECARET: number = R++;src[LONECARET] = "(?:\\^)";
const CARETTRIM: number = R++;src[CARETTRIM] = "(\\s*)" + src[LONECARET] + "\\s+";re[CARETTRIM] = new RegExp(src[CARETTRIM], "g");const caretTrimReplace: string = "$1^";
const CARET: number = R++;src[CARET] = "^" + src[LONECARET] + src[XRANGEPLAIN] + "$";const CARETLOOSE: number = R++;src[CARETLOOSE] = "^" + src[LONECARET] + src[XRANGEPLAINLOOSE] + "$";
// A simple gt/lt/eq thing, or just "" to indicate "any version"const COMPARATORLOOSE: number = R++;src[COMPARATORLOOSE] = "^" + src[GTLT] + "\\s*(" + LOOSEPLAIN + ")$|^$";const COMPARATOR: number = R++;src[COMPARATOR] = "^" + src[GTLT] + "\\s*(" + FULLPLAIN + ")$|^$";
// An expression to strip any whitespace between the gtlt and the thing// it modifies, so that `> 1.2.3` ==> `>1.2.3`const COMPARATORTRIM: number = R++;src[COMPARATORTRIM] = "(\\s*)" + src[GTLT] + "\\s*(" + LOOSEPLAIN + "|" + src[XRANGEPLAIN] + ")";
// this one has to use the /g flagre[COMPARATORTRIM] = new RegExp(src[COMPARATORTRIM], "g");const comparatorTrimReplace: string = "$1$2$3";
// Something like `1.2.3 - 1.2.4`// Note that these all use the loose form, because they'll be// checked against either the strict or loose comparator form// later.const HYPHENRANGE: number = R++;src[HYPHENRANGE] = "^\\s*(" + src[XRANGEPLAIN] + ")" + "\\s+-\\s+" + "(" + src[XRANGEPLAIN] + ")" + "\\s*$";
const HYPHENRANGELOOSE: number = R++;src[HYPHENRANGELOOSE] = "^\\s*(" + src[XRANGEPLAINLOOSE] + ")" + "\\s+-\\s+" + "(" + src[XRANGEPLAINLOOSE] + ")" + "\\s*$";
// Star ranges basically just allow anything at all.const STAR: number = R++;src[STAR] = "(<|>)?=?\\s*\\*";
// Compile to actual regexp objects.// All are flag-free, unless they were created above with a flag.for (let i: number = 0; i < R; i++) { if (!re[i]) { re[i] = new RegExp(src[i]); }}
export function parse( version: string | SemVer | null, optionsOrLoose?: boolean | Options,): SemVer | null { if (!optionsOrLoose || typeof optionsOrLoose !== "object") { optionsOrLoose = { loose: !!optionsOrLoose, includePrerelease: false, }; }
if (version instanceof SemVer) { return version; }
if (typeof version !== "string") { return null; }
if (version.length > MAX_LENGTH) { return null; }
const r: RegExp = optionsOrLoose.loose ? re[LOOSE] : re[FULL]; if (!r.test(version)) { return null; }
try { return new SemVer(version, optionsOrLoose); } catch (er) { return null; }}
export function valid( version: string | SemVer | null, optionsOrLoose?: boolean | Options,): string | null { if (version === null) return null; const v: SemVer | null = parse(version, optionsOrLoose); return v ? v.version : null;}
export function clean( version: string, optionsOrLoose?: boolean | Options,): string | null { const s: SemVer | null = parse( version.trim().replace(/^[=v]+/, ""), optionsOrLoose, ); return s ? s.version : null;}
export class SemVer { raw!: string; loose!: boolean; options!: Options;
major!: number; minor!: number; patch!: number; version!: string; build!: ReadonlyArray<string>; prerelease!: Array<string | number>;
constructor(version: string | SemVer, optionsOrLoose?: boolean | Options) { if (!optionsOrLoose || typeof optionsOrLoose !== "object") { optionsOrLoose = { loose: !!optionsOrLoose, includePrerelease: false, }; } if (version instanceof SemVer) { if (version.loose === optionsOrLoose.loose) { return version; } else { version = version.version; } } else if (typeof version !== "string") { throw new TypeError("Invalid Version: " + version); }
if (version.length > MAX_LENGTH) { throw new TypeError( "version is longer than " + MAX_LENGTH + " characters", ); }
if (!(this instanceof SemVer)) { return new SemVer(version, optionsOrLoose); }
this.options = optionsOrLoose; this.loose = !!optionsOrLoose.loose;
const m = version.trim().match(optionsOrLoose.loose ? re[LOOSE] : re[FULL]);
if (!m) { throw new TypeError("Invalid Version: " + version); }
this.raw = version;
// these are actually numbers this.major = +m[1]; this.minor = +m[2]; this.patch = +m[3];
if (this.major > Number.MAX_SAFE_INTEGER || this.major < 0) { throw new TypeError("Invalid major version"); }
if (this.minor > Number.MAX_SAFE_INTEGER || this.minor < 0) { throw new TypeError("Invalid minor version"); }
if (this.patch > Number.MAX_SAFE_INTEGER || this.patch < 0) { throw new TypeError("Invalid patch version"); }
// numberify any prerelease numeric ids if (!m[4]) { this.prerelease = []; } else { this.prerelease = m[4].split(".").map((id: string) => { if (/^[0-9]+$/.test(id)) { const num: number = +id; if (num >= 0 && num < Number.MAX_SAFE_INTEGER) { return num; } } return id; }); }
this.build = m[5] ? m[5].split(".") : []; this.format(); }
format(): string { this.version = this.major + "." + this.minor + "." + this.patch; if (this.prerelease.length) { this.version += "-" + this.prerelease.join("."); } return this.version; }
compare(other: string | SemVer): 1 | 0 | -1 { if (!(other instanceof SemVer)) { other = new SemVer(other, this.options); }
return this.compareMain(other) || this.comparePre(other); }
compareMain(other: string | SemVer): 1 | 0 | -1 { if (!(other instanceof SemVer)) { other = new SemVer(other, this.options); }
return ( compareIdentifiers(this.major, other.major) || compareIdentifiers(this.minor, other.minor) || compareIdentifiers(this.patch, other.patch) ); }
comparePre(other: string | SemVer): 1 | 0 | -1 { if (!(other instanceof SemVer)) { other = new SemVer(other, this.options); }
// NOT having a prerelease is > having one if (this.prerelease.length && !other.prerelease.length) { return -1; } else if (!this.prerelease.length && other.prerelease.length) { return 1; } else if (!this.prerelease.length && !other.prerelease.length) { return 0; }
let i: number = 0; do { const a: string | number = this.prerelease[i]; const b: string | number = other.prerelease[i]; if (a === undefined && b === undefined) { return 0; } else if (b === undefined) { return 1; } else if (a === undefined) { return -1; } else if (a === b) { continue; } else { return compareIdentifiers(a, b); } } while (++i); return 1; }
compareBuild(other: string | SemVer): 1 | 0 | -1 { if (!(other instanceof SemVer)) { other = new SemVer(other, this.options); }
let i: number = 0; do { const a: string = this.build[i]; const b: string = other.build[i]; if (a === undefined && b === undefined) { return 0; } else if (b === undefined) { return 1; } else if (a === undefined) { return -1; } else if (a === b) { continue; } else { return compareIdentifiers(a, b); } } while (++i); return 1; }
inc(release: ReleaseType, identifier?: string): SemVer { switch (release) { case "premajor": this.prerelease.length = 0; this.patch = 0; this.minor = 0; this.major++; this.inc("pre", identifier); break; case "preminor": this.prerelease.length = 0; this.patch = 0; this.minor++; this.inc("pre", identifier); break; case "prepatch": // If this is already a prerelease, it will bump to the next version // drop any prereleases that might already exist, since they are not // relevant at this point. this.prerelease.length = 0; this.inc("patch", identifier); this.inc("pre", identifier); break; // If the input is a non-prerelease version, this acts the same as // prepatch. case "prerelease": if (this.prerelease.length === 0) { this.inc("patch", identifier); } this.inc("pre", identifier); break;
case "major": // If this is a pre-major version, bump up to the same major version. // Otherwise increment major. // 1.0.0-5 bumps to 1.0.0 // 1.1.0 bumps to 2.0.0 if ( this.minor !== 0 || this.patch !== 0 || this.prerelease.length === 0 ) { this.major++; } this.minor = 0; this.patch = 0; this.prerelease = []; break; case "minor": // If this is a pre-minor version, bump up to the same minor version. // Otherwise increment minor. // 1.2.0-5 bumps to 1.2.0 // 1.2.1 bumps to 1.3.0 if (this.patch !== 0 || this.prerelease.length === 0) { this.minor++; } this.patch = 0; this.prerelease = []; break; case "patch": // If this is not a pre-release version, it will increment the patch. // If it is a pre-release it will bump up to the same patch version. // 1.2.0-5 patches to 1.2.0 // 1.2.0 patches to 1.2.1 if (this.prerelease.length === 0) { this.patch++; } this.prerelease = []; break; // This probably shouldn't be used publicly. // 1.0.0 "pre" would become 1.0.0-0 which is the wrong direction. case "pre": if (this.prerelease.length === 0) { this.prerelease = [0]; } else { let i: number = this.prerelease.length; while (--i >= 0) { if (typeof this.prerelease[i] === "number") { (this.prerelease[i] as number)++; i = -2; } } if (i === -1) { // didn't increment anything this.prerelease.push(0); } } if (identifier) { // 1.2.0-beta.1 bumps to 1.2.0-beta.2, // 1.2.0-beta.fooblz or 1.2.0-beta bumps to 1.2.0-beta.0 if (this.prerelease[0] === identifier) { if (isNaN(this.prerelease[1] as number)) { this.prerelease = [identifier, 0]; } } else { this.prerelease = [identifier, 0]; } } break;
default: throw new Error("invalid increment argument: " + release); } this.format(); this.raw = this.version; return this; }
toString(): string { return this.version; }}
/** * Return the version incremented by the release type (major, minor, patch, or prerelease), or null if it's not valid. */export function inc( version: string | SemVer, release: ReleaseType, optionsOrLoose?: boolean | Options, identifier?: string,): string | null { if (typeof optionsOrLoose === "string") { identifier = optionsOrLoose; optionsOrLoose = undefined; } try { return new SemVer(version, optionsOrLoose).inc(release, identifier).version; } catch (er) { return null; }}
export function diff( version1: string | SemVer, version2: string | SemVer, optionsOrLoose?: boolean | Options,): ReleaseType | null { if (eq(version1, version2, optionsOrLoose)) { return null; } else { const v1: SemVer | null = parse(version1); const v2: SemVer | null = parse(version2); let prefix: string = ""; let defaultResult: ReleaseType | null = null;
if (v1 && v2) { if (v1.prerelease.length || v2.prerelease.length) { prefix = "pre"; defaultResult = "prerelease"; }
for (const key in v1) { if (key === "major" || key === "minor" || key === "patch") { if (v1[key] !== v2[key]) { return (prefix + key) as ReleaseType; } } } } return defaultResult; // may be undefined }}
const numeric: RegExp = /^[0-9]+$/;
export function compareIdentifiers( a: string | number | null, b: string | number | null,): 1 | 0 | -1 { const anum: boolean = numeric.test(a as string); const bnum: boolean = numeric.test(b as string);
if (a === null || b === null) throw "Comparison against null invalid";
if (anum && bnum) { a = +a; b = +b; }
return a === b ? 0 : anum && !bnum ? -1 : bnum && !anum ? 1 : a < b ? -1 : 1;}
export function rcompareIdentifiers( a: string | null, b: string | null,): 1 | 0 | -1 { return compareIdentifiers(b, a);}
/** * Return the major version number. */export function major( v: string | SemVer, optionsOrLoose?: boolean | Options,): number { return new SemVer(v, optionsOrLoose).major;}
/** * Return the minor version number. */export function minor( v: string | SemVer, optionsOrLoose?: boolean | Options,): number { return new SemVer(v, optionsOrLoose).minor;}
/** * Return the patch version number. */export function patch( v: string | SemVer, optionsOrLoose?: boolean | Options,): number { return new SemVer(v, optionsOrLoose).patch;}
export function compare( v1: string | SemVer, v2: string | SemVer, optionsOrLoose?: boolean | Options,): 1 | 0 | -1 { return new SemVer(v1, optionsOrLoose).compare(new SemVer(v2, optionsOrLoose));}
export function compareLoose( a: string | SemVer, b: string | SemVer,): 1 | 0 | -1 { return compare(a, b, true);}
export function compareBuild( a: string | SemVer, b: string | SemVer, loose?: boolean | Options,): 1 | 0 | -1 { var versionA = new SemVer(a, loose); var versionB = new SemVer(b, loose); return versionA.compare(versionB) || versionA.compareBuild(versionB);}
export function rcompare( v1: string | SemVer, v2: string | SemVer, optionsOrLoose?: boolean | Options,): 1 | 0 | -1 { return compare(v2, v1, optionsOrLoose);}
export function sort<T extends string | SemVer>( list: T[], optionsOrLoose?: boolean | Options,): T[] { return list.sort((a, b) => { return compareBuild(a, b, optionsOrLoose); });}
export function rsort<T extends string | SemVer>( list: T[], optionsOrLoose?: boolean | Options,): T[] { return list.sort((a, b) => { return compareBuild(b, a, optionsOrLoose); });}
export function gt( v1: string | SemVer, v2: string | SemVer, optionsOrLoose?: boolean | Options,): boolean { return compare(v1, v2, optionsOrLoose) > 0;}
export function lt( v1: string | SemVer, v2: string | SemVer, optionsOrLoose?: boolean | Options,): boolean { return compare(v1, v2, optionsOrLoose) < 0;}
export function eq( v1: string | SemVer, v2: string | SemVer, optionsOrLoose?: boolean | Options,): boolean { return compare(v1, v2, optionsOrLoose) === 0;}
export function neq( v1: string | SemVer, v2: string | SemVer, optionsOrLoose?: boolean | Options,): boolean { return compare(v1, v2, optionsOrLoose) !== 0;}
export function gte( v1: string | SemVer, v2: string | SemVer, optionsOrLoose?: boolean | Options,): boolean { return compare(v1, v2, optionsOrLoose) >= 0;}
export function lte( v1: string | SemVer, v2: string | SemVer, optionsOrLoose?: boolean | Options,): boolean { return compare(v1, v2, optionsOrLoose) <= 0;}
export function cmp( v1: string | SemVer, operator: Operator, v2: string | SemVer, optionsOrLoose?: boolean | Options,): boolean { switch (operator) { case "===": if (typeof v1 === "object") v1 = v1.version; if (typeof v2 === "object") v2 = v2.version; return v1 === v2;
case "!==": if (typeof v1 === "object") v1 = v1.version; if (typeof v2 === "object") v2 = v2.version; return v1 !== v2;
case "": case "=": case "==": return eq(v1, v2, optionsOrLoose);
case "!=": return neq(v1, v2, optionsOrLoose);
case ">": return gt(v1, v2, optionsOrLoose);
case ">=": return gte(v1, v2, optionsOrLoose);
case "<": return lt(v1, v2, optionsOrLoose);
case "<=": return lte(v1, v2, optionsOrLoose);
default: throw new TypeError("Invalid operator: " + operator); }}
const ANY: SemVer = {} as SemVer;
export class Comparator { semver!: SemVer; operator!: "" | "=" | "<" | ">" | "<=" | ">="; value!: string; loose!: boolean; options!: Options;
constructor(comp: string | Comparator, optionsOrLoose?: boolean | Options) { if (!optionsOrLoose || typeof optionsOrLoose !== "object") { optionsOrLoose = { loose: !!optionsOrLoose, includePrerelease: false, }; }
if (comp instanceof Comparator) { if (comp.loose === !!optionsOrLoose.loose) { return comp; } else { comp = comp.value; } }
if (!(this instanceof Comparator)) { return new Comparator(comp, optionsOrLoose); }
this.options = optionsOrLoose; this.loose = !!optionsOrLoose.loose; this.parse(comp);
if (this.semver === ANY) { this.value = ""; } else { this.value = this.operator + this.semver.version; } }
parse(comp: string): void { const r = this.options.loose ? re[COMPARATORLOOSE] : re[COMPARATOR]; const m = comp.match(r);
if (!m) { throw new TypeError("Invalid comparator: " + comp); }
const m1 = m[1] as "" | "=" | "<" | ">" | "<=" | ">="; this.operator = m1 !== undefined ? m1 : "";
if (this.operator === "=") { this.operator = ""; }
// if it literally is just '>' or '' then allow anything. if (!m[2]) { this.semver = ANY; } else { this.semver = new SemVer(m[2], this.options.loose); } }
test(version: string | SemVer): boolean { if (this.semver === ANY || version === ANY) { return true; }
if (typeof version === "string") { version = new SemVer(version, this.options); }
return cmp(version, this.operator, this.semver, this.options); }
intersects(comp: Comparator, optionsOrLoose?: boolean | Options): boolean { if (!(comp instanceof Comparator)) { throw new TypeError("a Comparator is required"); }
if (!optionsOrLoose || typeof optionsOrLoose !== "object") { optionsOrLoose = { loose: !!optionsOrLoose, includePrerelease: false, }; }
let rangeTmp: Range;
if (this.operator === "") { if (this.value === "") { return true; } rangeTmp = new Range(comp.value, optionsOrLoose); return satisfies(this.value, rangeTmp, optionsOrLoose); } else if (comp.operator === "") { if (comp.value === "") { return true; } rangeTmp = new Range(this.value, optionsOrLoose); return satisfies(comp.semver, rangeTmp, optionsOrLoose); }
const sameDirectionIncreasing: boolean = (this.operator === ">=" || this.operator === ">") && (comp.operator === ">=" || comp.operator === ">"); const sameDirectionDecreasing: boolean = (this.operator === "<=" || this.operator === "<") && (comp.operator === "<=" || comp.operator === "<"); const sameSemVer: boolean = this.semver.version === comp.semver.version; const differentDirectionsInclusive: boolean = (this.operator === ">=" || this.operator === "<=") && (comp.operator === ">=" || comp.operator === "<="); const oppositeDirectionsLessThan: boolean = cmp(this.semver, "<", comp.semver, optionsOrLoose) && (this.operator === ">=" || this.operator === ">") && (comp.operator === "<=" || comp.operator === "<"); const oppositeDirectionsGreaterThan: boolean = cmp(this.semver, ">", comp.semver, optionsOrLoose) && (this.operator === "<=" || this.operator === "<") && (comp.operator === ">=" || comp.operator === ">");
return ( sameDirectionIncreasing || sameDirectionDecreasing || (sameSemVer && differentDirectionsInclusive) || oppositeDirectionsLessThan || oppositeDirectionsGreaterThan ); }
toString(): string { return this.value; }}
export class Range { range!: string; raw!: string; loose!: boolean; options!: Options; includePrerelease!: boolean; set!: ReadonlyArray<ReadonlyArray<Comparator>>;
constructor( range: string | Range | Comparator, optionsOrLoose?: boolean | Options, ) { if (!optionsOrLoose || typeof optionsOrLoose !== "object") { optionsOrLoose = { loose: !!optionsOrLoose, includePrerelease: false, }; }
if (range instanceof Range) { if ( range.loose === !!optionsOrLoose.loose && range.includePrerelease === !!optionsOrLoose.includePrerelease ) { return range; } else { return new Range(range.raw, optionsOrLoose); } }
if (range instanceof Comparator) { return new Range(range.value, optionsOrLoose); }
if (!(this instanceof Range)) { return new Range(range, optionsOrLoose); }
this.options = optionsOrLoose; this.loose = !!optionsOrLoose.loose; this.includePrerelease = !!optionsOrLoose.includePrerelease;
// First, split based on boolean or || this.raw = range; this.set = range .split(/\s*\|\|\s*/) .map((range) => this.parseRange(range.trim())) .filter((c) => { // throw out any that are not relevant for whatever reason return c.length; });
if (!this.set.length) { throw new TypeError("Invalid SemVer Range: " + range); }
this.format(); }
format(): string { this.range = this.set .map((comps) => comps.join(" ").trim()) .join("||") .trim(); return this.range; }
parseRange(range: string): ReadonlyArray<Comparator> { const loose = this.options.loose; range = range.trim(); // `1.2.3 - 1.2.4` => `>=1.2.3 <=1.2.4` const hr: RegExp = loose ? re[HYPHENRANGELOOSE] : re[HYPHENRANGE]; range = range.replace(hr, hyphenReplace);
// `> 1.2.3 < 1.2.5` => `>1.2.3 <1.2.5` range = range.replace(re[COMPARATORTRIM], comparatorTrimReplace);
// `~ 1.2.3` => `~1.2.3` range = range.replace(re[TILDETRIM], tildeTrimReplace);
// `^ 1.2.3` => `^1.2.3` range = range.replace(re[CARETTRIM], caretTrimReplace);
// normalize spaces range = range.split(/\s+/).join(" ");
// At this point, the range is completely trimmed and // ready to be split into comparators.
const compRe: RegExp = loose ? re[COMPARATORLOOSE] : re[COMPARATOR]; let set: string[] = range .split(" ") .map((comp) => parseComparator(comp, this.options)) .join(" ") .split(/\s+/); if (this.options.loose) { // in loose mode, throw out any that are not valid comparators set = set.filter((comp) => { return !!comp.match(compRe); }); }
return set.map((comp) => new Comparator(comp, this.options)); }
test(version: string | SemVer): boolean { if (typeof version === "string") { version = new SemVer(version, this.options); }
for (var i = 0; i < this.set.length; i++) { if (testSet(this.set[i], version, this.options)) { return true; } } return false; }
intersects(range?: Range, optionsOrLoose?: boolean | Options): boolean { if (!(range instanceof Range)) { throw new TypeError("a Range is required"); }
return this.set.some((thisComparators) => { return ( isSatisfiable(thisComparators, optionsOrLoose) && range.set.some((rangeComparators) => { return ( isSatisfiable(rangeComparators, optionsOrLoose) && thisComparators.every((thisComparator) => { return rangeComparators.every((rangeComparator) => { return thisComparator.intersects( rangeComparator, optionsOrLoose, ); }); }) ); }) ); }); }
toString(): string { return this.range; }}
function testSet( set: ReadonlyArray<Comparator>, version: SemVer, options: Options,): boolean { for (let i: number = 0; i < set.length; i++) { if (!set[i].test(version)) { return false; } }
if (version.prerelease.length && !options.includePrerelease) { // Find the set of versions that are allowed to have prereleases // For example, ^1.2.3-pr.1 desugars to >=1.2.3-pr.1 <2.0.0 // That should allow `1.2.3-pr.2` to pass. // However, `1.2.4-alpha.notready` should NOT be allowed, // even though it's within the range set by the comparators. for (let i: number = 0; i < set.length; i++) { if (set[i].semver === ANY) { continue; }
if (set[i].semver.prerelease.length > 0) { const allowed: SemVer = set[i].semver; if ( allowed.major === version.major && allowed.minor === version.minor && allowed.patch === version.patch ) { return true; } } }
// Version has a -pre, but it's not one of the ones we like. return false; }
return true;}
// take a set of comparators and determine whether there// exists a version which can satisfy itfunction isSatisfiable( comparators: readonly Comparator[], options?: boolean | Options,): boolean { let result: boolean = true; const remainingComparators: Comparator[] = comparators.slice(); let testComparator = remainingComparators.pop();
while (result && remainingComparators.length) { result = remainingComparators.every((otherComparator) => { return testComparator?.intersects(otherComparator, options); });
testComparator = remainingComparators.pop(); }
return result;}
// Mostly just for testing and legacy API reasonsexport function toComparators( range: string | Range, optionsOrLoose?: boolean | Options,): string[][] { return new Range(range, optionsOrLoose).set.map((comp) => { return comp .map((c) => c.value) .join(" ") .trim() .split(" "); });}
// comprised of xranges, tildes, stars, and gtlt's at this point.// already replaced the hyphen ranges// turn into a set of JUST comparators.function parseComparator(comp: string, options: Options): string { comp = replaceCarets(comp, options); comp = replaceTildes(comp, options); comp = replaceXRanges(comp, options); comp = replaceStars(comp, options); return comp;}
function isX(id: string): boolean { return !id || id.toLowerCase() === "x" || id === "*";}
// ~, ~> --> * (any, kinda silly)// ~2, ~2.x, ~2.x.x, ~>2, ~>2.x ~>2.x.x --> >=2.0.0 <3.0.0// ~2.0, ~2.0.x, ~>2.0, ~>2.0.x --> >=2.0.0 <2.1.0// ~1.2, ~1.2.x, ~>1.2, ~>1.2.x --> >=1.2.0 <1.3.0// ~1.2.3, ~>1.2.3 --> >=1.2.3 <1.3.0// ~1.2.0, ~>1.2.0 --> >=1.2.0 <1.3.0function replaceTildes(comp: string, options: Options): string { return comp .trim() .split(/\s+/) .map((comp) => replaceTilde(comp, options)) .join(" ");}
function replaceTilde(comp: string, options: Options): string { const r: RegExp = options.loose ? re[TILDELOOSE] : re[TILDE]; return comp.replace( r, (_: string, M: string, m: string, p: string, pr: string) => { let ret: string;
if (isX(M)) { ret = ""; } else if (isX(m)) { ret = ">=" + M + ".0.0 <" + (+M + 1) + ".0.0"; } else if (isX(p)) { // ~1.2 == >=1.2.0 <1.3.0 ret = ">=" + M + "." + m + ".0 <" + M + "." + (+m + 1) + ".0"; } else if (pr) { ret = ">=" + M + "." + m + "." + p + "-" + pr + " <" + M + "." + (+m + 1) + ".0"; } else { // ~1.2.3 == >=1.2.3 <1.3.0 ret = ">=" + M + "." + m + "." + p + " <" + M + "." + (+m + 1) + ".0"; }
return ret; }, );}
// ^ --> * (any, kinda silly)// ^2, ^2.x, ^2.x.x --> >=2.0.0 <3.0.0// ^2.0, ^2.0.x --> >=2.0.0 <3.0.0// ^1.2, ^1.2.x --> >=1.2.0 <2.0.0// ^1.2.3 --> >=1.2.3 <2.0.0// ^1.2.0 --> >=1.2.0 <2.0.0function replaceCarets(comp: string, options: Options): string { return comp .trim() .split(/\s+/) .map((comp) => replaceCaret(comp, options)) .join(" ");}
function replaceCaret(comp: string, options: Options): string { const r: RegExp = options.loose ? re[CARETLOOSE] : re[CARET]; return comp.replace(r, (_: string, M, m, p, pr) => { let ret: string;
if (isX(M)) { ret = ""; } else if (isX(m)) { ret = ">=" + M + ".0.0 <" + (+M + 1) + ".0.0"; } else if (isX(p)) { if (M === "0") { ret = ">=" + M + "." + m + ".0 <" + M + "." + (+m + 1) + ".0"; } else { ret = ">=" + M + "." + m + ".0 <" + (+M + 1) + ".0.0"; } } else if (pr) { if (M === "0") { if (m === "0") { ret = ">=" + M + "." + m + "." + p + "-" + pr + " <" + M + "." + m + "." + (+p + 1); } else { ret = ">=" + M + "." + m + "." + p + "-" + pr + " <" + M + "." + (+m + 1) + ".0"; } } else { ret = ">=" + M + "." + m + "." + p + "-" + pr + " <" + (+M + 1) + ".0.0"; } } else { if (M === "0") { if (m === "0") { ret = ">=" + M + "." + m + "." + p + " <" + M + "." + m + "." + (+p + 1); } else { ret = ">=" + M + "." + m + "." + p + " <" + M + "." + (+m + 1) + ".0"; } } else { ret = ">=" + M + "." + m + "." + p + " <" + (+M + 1) + ".0.0"; } }
return ret; });}
function replaceXRanges(comp: string, options: Options): string { return comp .split(/\s+/) .map((comp) => replaceXRange(comp, options)) .join(" ");}
function replaceXRange(comp: string, options: Options): string { comp = comp.trim(); const r: RegExp = options.loose ? re[XRANGELOOSE] : re[XRANGE]; return comp.replace(r, (ret: string, gtlt, M, m, p, pr) => { const xM: boolean = isX(M); const xm: boolean = xM || isX(m); const xp: boolean = xm || isX(p); const anyX: boolean = xp;
if (gtlt === "=" && anyX) { gtlt = ""; }
if (xM) { if (gtlt === ">" || gtlt === "<") { // nothing is allowed ret = "<0.0.0"; } else { // nothing is forbidden ret = "*"; } } else if (gtlt && anyX) { // we know patch is an x, because we have any x at all. // replace X with 0 if (xm) { m = 0; } p = 0;
if (gtlt === ">") { // >1 => >=2.0.0 // >1.2 => >=1.3.0 // >1.2.3 => >= 1.2.4 gtlt = ">="; if (xm) { M = +M + 1; m = 0; p = 0; } else { m = +m + 1; p = 0; } } else if (gtlt === "<=") { // <=0.7.x is actually <0.8.0, since any 0.7.x should // pass. Similarly, <=7.x is actually <8.0.0, etc. gtlt = "<"; if (xm) { M = +M + 1; } else { m = +m + 1; } }
ret = gtlt + M + "." + m + "." + p; } else if (xm) { ret = ">=" + M + ".0.0 <" + (+M + 1) + ".0.0"; } else if (xp) { ret = ">=" + M + "." + m + ".0 <" + M + "." + (+m + 1) + ".0"; }
return ret; });}
// Because * is AND-ed with everything else in the comparator,// and '' means "any version", just remove the *s entirely.function replaceStars(comp: string, options: Options): string { // Looseness is ignored here. star is always as loose as it gets! return comp.trim().replace(re[STAR], "");}
// This function is passed to string.replace(re[HYPHENRANGE])// M, m, patch, prerelease, build// 1.2 - 3.4.5 => >=1.2.0 <=3.4.5// 1.2.3 - 3.4 => >=1.2.0 <3.5.0 Any 3.4.x will do// 1.2 - 3.4 => >=1.2.0 <3.5.0function hyphenReplace( $0: any, from: any, fM: any, fm: any, fp: any, fpr: any, fb: any, to: any, tM: any, tm: any, tp: any, tpr: any, tb: any,) { if (isX(fM)) { from = ""; } else if (isX(fm)) { from = ">=" + fM + ".0.0"; } else if (isX(fp)) { from = ">=" + fM + "." + fm + ".0"; } else { from = ">=" + from; }
if (isX(tM)) { to = ""; } else if (isX(tm)) { to = "<" + (+tM + 1) + ".0.0"; } else if (isX(tp)) { to = "<" + tM + "." + (+tm + 1) + ".0"; } else if (tpr) { to = "<=" + tM + "." + tm + "." + tp + "-" + tpr; } else { to = "<=" + to; }
return (from + " " + to).trim();}
export function satisfies( version: string | SemVer, range: string | Range, optionsOrLoose?: boolean | Options,): boolean { try { range = new Range(range, optionsOrLoose); } catch (er) { return false; } return range.test(version);}
export function maxSatisfying<T extends string | SemVer>( versions: ReadonlyArray<T>, range: string | Range, optionsOrLoose?: boolean | Options,): T | null { //todo var max: T | SemVer | null = null; var maxSV: SemVer | null = null; try { var rangeObj = new Range(range, optionsOrLoose); } catch (er) { return null; } versions.forEach((v) => { if (rangeObj.test(v)) { // satisfies(v, range, options) if (!max || (maxSV && maxSV.compare(v) === -1)) { // compare(max, v, true) max = v; maxSV = new SemVer(max, optionsOrLoose); } } }); return max;}
export function minSatisfying<T extends string | SemVer>( versions: ReadonlyArray<T>, range: string | Range, optionsOrLoose?: boolean | Options,): T | null { //todo var min: any = null; var minSV: any = null; try { var rangeObj = new Range(range, optionsOrLoose); } catch (er) { return null; } versions.forEach((v) => { if (rangeObj.test(v)) { // satisfies(v, range, options) if (!min || minSV.compare(v) === 1) { // compare(min, v, true) min = v; minSV = new SemVer(min, optionsOrLoose); } } }); return min;}
export function minVersion( range: string | Range, optionsOrLoose?: boolean | Options,): SemVer | null { range = new Range(range, optionsOrLoose);
var minver: SemVer | null = new SemVer("0.0.0"); if (range.test(minver)) { return minver; }
minver = new SemVer("0.0.0-0"); if (range.test(minver)) { return minver; }
minver = null; for (var i = 0; i < range.set.length; ++i) { var comparators = range.set[i];
comparators.forEach((comparator) => { // Clone to avoid manipulating the comparator's semver object. var compver = new SemVer(comparator.semver.version); switch (comparator.operator) { case ">": if (compver.prerelease.length === 0) { compver.patch++; } else { compver.prerelease.push(0); } compver.raw = compver.format(); /* fallthrough */ case "": case ">=": if (!minver || gt(minver, compver)) { minver = compver; } break; case "<": case "<=": /* Ignore maximum versions */ break; /* istanbul ignore next */ default: throw new Error("Unexpected operation: " + comparator.operator); } }); }
if (minver && range.test(minver)) { return minver; }
return null;}
export function validRange( range: string | Range | null, optionsOrLoose?: boolean | Options,): string | null { try { if (range === null) return null; // Return '*' instead of '' so that truthiness works. // This will throw if it's invalid anyway return new Range(range, optionsOrLoose).range || "*"; } catch (er) { return null; }}
/** * Return true if version is less than all the versions possible in the range. */export function ltr( version: string | SemVer, range: string | Range, optionsOrLoose?: boolean | Options,): boolean { return outside(version, range, "<", optionsOrLoose);}
/** * Return true if version is greater than all the versions possible in the range. */export function gtr( version: string | SemVer, range: string | Range, optionsOrLoose?: boolean | Options,): boolean { return outside(version, range, ">", optionsOrLoose);}
/** * Return true if the version is outside the bounds of the range in either the high or low direction. * The hilo argument must be either the string '>' or '<'. (This is the function called by gtr and ltr.) */export function outside( version: string | SemVer, range: string | Range, hilo: ">" | "<", optionsOrLoose?: boolean | Options,): boolean { version = new SemVer(version, optionsOrLoose); range = new Range(range, optionsOrLoose);
let gtfn: typeof gt; let ltefn: typeof lte; let ltfn: typeof lt; let comp: string; let ecomp: string; switch (hilo) { case ">": gtfn = gt; ltefn = lte; ltfn = lt; comp = ">"; ecomp = ">="; break; case "<": gtfn = lt; ltefn = gte; ltfn = gt; comp = "<"; ecomp = "<="; break; default: throw new TypeError('Must provide a hilo val of "<" or ">"'); }
// If it satisifes the range it is not outside if (satisfies(version, range, optionsOrLoose)) { return false; }
// From now on, variable terms are as if we're in "gtr" mode. // but note that everything is flipped for the "ltr" function.
for (let i: number = 0; i < range.set.length; ++i) { const comparators: readonly Comparator[] = range.set[i];
let high: Comparator | null = null; let low: Comparator | null = null;
for (let comparator of comparators) { if (comparator.semver === ANY) { comparator = new Comparator(">=0.0.0"); } high = high || comparator; low = low || comparator; if (gtfn(comparator.semver, high.semver, optionsOrLoose)) { high = comparator; } else if (ltfn(comparator.semver, low.semver, optionsOrLoose)) { low = comparator; } }
if (high === null || low === null) return true;
// If the edge version comparator has a operator then our version // isn't outside it if (high!.operator === comp || high!.operator === ecomp) { return false; }
// If the lowest version comparator has an operator and our version // is less than it then it isn't higher than the range if ( (!low!.operator || low!.operator === comp) && ltefn(version, low!.semver) ) { return false; } else if (low!.operator === ecomp && ltfn(version, low!.semver)) { return false; } } return true;}
export function prerelease( version: string | SemVer, optionsOrLoose?: boolean | Options,): ReadonlyArray<string | number> | null { var parsed = parse(version, optionsOrLoose); return parsed && parsed.prerelease.length ? parsed.prerelease : null;}
/** * Return true if any of the ranges comparators intersect */export function intersects( range1: string | Range | Comparator, range2: string | Range | Comparator, optionsOrLoose?: boolean | Options,): boolean { range1 = new Range(range1, optionsOrLoose); range2 = new Range(range2, optionsOrLoose); return range1.intersects(range2);}
/** * Coerces a string to semver if possible */export function coerce( version: string | SemVer, optionsOrLoose?: boolean | Options,): SemVer | null { if (version instanceof SemVer) { return version; }
if (typeof version !== "string") { return null; }
const match = version.match(re[COERCE]);
if (match == null) { return null; }
return parse( match[1] + "." + (match[2] || "0") + "." + (match[3] || "0"), optionsOrLoose, );}
export default SemVer;