version-range
Advanced tools
Comparing version 4.1.0 to 4.2.0-next.1700433755.ebd54a15d9693193124b08085beded5c03fee6d3
@@ -1,3 +0,5 @@ | ||
const orRegex = /\s*\|\|\s*/; | ||
const rangeRegex = /^\s*([<>=~^]*)\s*([\d.]+?)[.x]*(-.+)?\s*$/; | ||
const orRegex = /\s*\|\|\s*/g; | ||
const andRegex = /\s*&&\s*/g; | ||
const rangeRegex = /^\s*([<>=~^]*)\s*([\d.x]+)(-[^\s]+)?\s*/; | ||
const xRegex = /[.]x/g; | ||
/** | ||
@@ -18,159 +20,189 @@ * Check if the version is within the range | ||
// cycle through the conditions | ||
let combinedResult = false; | ||
if (!Array.isArray(range)) | ||
range = String(range).split(orRegex); | ||
for (const condition of range) { | ||
// process range | ||
const [_, comparator, target, prerelease] = String(condition).match(rangeRegex) || []; | ||
// prepare and verify target | ||
const [targetMajor = null, targetMinor = null, targetPatch = null] = (target || '').split('.'); | ||
if (!target || targetMajor == null || prerelease) | ||
throw new Error(`range condition was invalid: ${JSON.stringify(condition)}`); | ||
const targetMajorNumber = Number(targetMajor || 0); | ||
const targetMinorNumber = Number(targetMinor || 0); | ||
const targetPatchNumber = Number(targetPatch || 0); | ||
// handle comparator | ||
const pass = false; | ||
switch (comparator) { | ||
case '^': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (subjectMinorNumber === targetMinorNumber) { | ||
if (subjectPatchNumber >= targetPatchNumber) { | ||
combinedResult = true; | ||
const orRanges = Array.isArray(range) | ||
? range.slice() | ||
: String(range).split(orRegex); | ||
for (const orRange of orRanges) { | ||
let orResult = false; | ||
const andRanges = String(orRange).split(andRegex); | ||
for (const andRange of andRanges) { | ||
let andResult = false; | ||
// process range | ||
const [match, comparator, targetRaw, prerelease] = String(andRange).match(rangeRegex) || []; | ||
const target = (targetRaw || '').replace(xRegex, ''); | ||
// // log | ||
// console.log({ | ||
// orRange, | ||
// andRange, | ||
// match, | ||
// comparator, | ||
// target, | ||
// prerelease, | ||
// }) | ||
// prepare and verify target | ||
const [targetMajor = null, targetMinor = null, targetPatch = null] = target.split('.'); | ||
if (!match || !target || targetMajor == null || prerelease) | ||
throw new Error(`range condition was invalid: ${JSON.stringify(andRange)}`); | ||
const targetMajorNumber = Number(targetMajor || 0); | ||
const targetMinorNumber = Number(targetMinor || 0); | ||
const targetPatchNumber = Number(targetPatch || 0); | ||
// is there more range matches? add it to and condition | ||
const remainder = String(andRange).slice(match.length); | ||
if (remainder) | ||
andRanges.push(remainder); | ||
// handle comparator | ||
switch (comparator) { | ||
case '^': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (subjectMinorNumber === targetMinorNumber) { | ||
if (subjectPatchNumber >= targetPatchNumber) { | ||
andResult = true; | ||
} | ||
} | ||
} | ||
else if (subjectMinorNumber >= targetMinorNumber) { | ||
combinedResult = true; | ||
} | ||
} | ||
break; | ||
case '~': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (subjectMinor !== null && | ||
subjectMinorNumber === targetMinorNumber) { | ||
if (subjectPatchNumber >= targetPatchNumber) { | ||
combinedResult = true; | ||
else if (subjectMinorNumber >= targetMinorNumber) { | ||
andResult = true; | ||
} | ||
} | ||
} | ||
break; | ||
case '>=': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (subjectMinorNumber === targetMinorNumber) { | ||
if (subjectPatchNumber >= targetPatchNumber) { | ||
combinedResult = true; | ||
break; | ||
case '~': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (subjectMinor !== null && | ||
subjectMinorNumber === targetMinorNumber) { | ||
if (subjectPatchNumber >= targetPatchNumber) { | ||
andResult = true; | ||
} | ||
} | ||
} | ||
else if (subjectMinorNumber >= targetMinorNumber) { | ||
combinedResult = true; | ||
} | ||
} | ||
else if (subjectMajorNumber >= targetMajorNumber) { | ||
combinedResult = true; | ||
} | ||
break; | ||
case '>': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (targetMinor === null) { | ||
// x > x = false | ||
// x.y > x = false | ||
} | ||
else if (subjectMinorNumber === targetMinorNumber) { | ||
if (targetPatch === null) { | ||
// x.y > x.y = false | ||
// x.y.z > x.y = false | ||
break; | ||
case '>=': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (subjectMinorNumber === targetMinorNumber) { | ||
if (subjectPatchNumber >= targetPatchNumber) { | ||
andResult = true; | ||
} | ||
} | ||
else if (subjectPatchNumber > targetPatchNumber) { | ||
combinedResult = true; | ||
else if (subjectMinorNumber >= targetMinorNumber) { | ||
andResult = true; | ||
} | ||
} | ||
else if (subjectMinorNumber > targetMinorNumber) { | ||
combinedResult = true; | ||
else if (subjectMajorNumber >= targetMajorNumber) { | ||
andResult = true; | ||
} | ||
} | ||
else if (subjectMajorNumber > targetMajorNumber) { | ||
combinedResult = true; | ||
} | ||
break; | ||
case '<': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (subjectMinor === null) { | ||
// x < x = false | ||
// x < x.y = false | ||
} | ||
else if (subjectMinorNumber === targetMinorNumber) { | ||
if (subjectPatch === null) { | ||
// x.y < x.y = false | ||
// x.y < x.y.z = false | ||
break; | ||
case '>': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (targetMinor === null) { | ||
// x > x = false | ||
// x.y > x = false | ||
} | ||
else if (subjectPatchNumber < targetPatchNumber) { | ||
combinedResult = true; | ||
else if (subjectMinorNumber === targetMinorNumber) { | ||
if (targetPatch === null) { | ||
// x.y > x.y = false | ||
// x.y.z > x.y = false | ||
} | ||
else if (subjectPatchNumber > targetPatchNumber) { | ||
andResult = true; | ||
} | ||
} | ||
else if (subjectMinorNumber > targetMinorNumber) { | ||
andResult = true; | ||
} | ||
} | ||
else if (subjectMinorNumber < targetMinorNumber) { | ||
combinedResult = true; | ||
else if (subjectMajorNumber > targetMajorNumber) { | ||
andResult = true; | ||
} | ||
} | ||
else if (subjectMajorNumber < targetMajorNumber) { | ||
combinedResult = true; | ||
} | ||
break; | ||
case '<=': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (subjectMinor === null) { | ||
if (targetMinor === null) { | ||
// x <= x = true | ||
combinedResult = true; | ||
break; | ||
case '<': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (subjectMinor === null) { | ||
// x < x = false | ||
// x < x.y = false | ||
} | ||
// x <= x.y = false | ||
else if (subjectMinorNumber === targetMinorNumber) { | ||
if (subjectPatch === null) { | ||
// x.y < x.y = false | ||
// x.y < x.y.z = false | ||
} | ||
else if (subjectPatchNumber < targetPatchNumber) { | ||
andResult = true; | ||
} | ||
} | ||
else if (subjectMinorNumber < targetMinorNumber) { | ||
andResult = true; | ||
} | ||
} | ||
else if (targetMinor === null) { | ||
// x.y <= x = true | ||
combinedResult = true; | ||
else if (subjectMajorNumber < targetMajorNumber) { | ||
andResult = true; | ||
} | ||
else if (subjectMinorNumber === targetMinorNumber) { | ||
if (subjectPatch === null) { | ||
if (targetPatch === null) { | ||
// x.y <= x.y = true | ||
combinedResult = true; | ||
break; | ||
case '<=': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (subjectMinor === null) { | ||
if (targetMinor === null) { | ||
// x <= x = true | ||
andResult = true; | ||
} | ||
// x.y <= x.y.z = false | ||
// x <= x.y = false | ||
} | ||
else if (targetPatch === null) { | ||
// x.y.z <= x.y = true | ||
combinedResult = true; | ||
else if (targetMinor === null) { | ||
// x.y <= x = true | ||
andResult = true; | ||
} | ||
else if (subjectPatchNumber <= targetPatchNumber) { | ||
// x.y.z <= x.y.z = true | ||
combinedResult = true; | ||
else if (subjectMinorNumber === targetMinorNumber) { | ||
if (subjectPatch === null) { | ||
if (targetPatch === null) { | ||
// x.y <= x.y = true | ||
andResult = true; | ||
} | ||
// x.y <= x.y.z = false | ||
} | ||
else if (targetPatch === null) { | ||
// x.y.z <= x.y = true | ||
andResult = true; | ||
} | ||
else if (subjectPatchNumber <= targetPatchNumber) { | ||
// x.y.z <= x.y.z = true | ||
andResult = true; | ||
} | ||
} | ||
else if (subjectMinorNumber <= targetMinorNumber) { | ||
andResult = true; | ||
} | ||
} | ||
else if (subjectMinorNumber <= targetMinorNumber) { | ||
combinedResult = true; | ||
else if (subjectMajorNumber <= targetMajorNumber) { | ||
andResult = true; | ||
} | ||
} | ||
else if (subjectMajorNumber <= targetMajorNumber) { | ||
combinedResult = true; | ||
} | ||
break; | ||
case '=': | ||
case '': | ||
if (subjectMajor === targetMajor) { | ||
if (targetMinor === null) { | ||
combinedResult = true; | ||
} | ||
else if (subjectMinor === targetMinor) { | ||
if (targetPatch === null || subjectPatch === targetPatch) { | ||
combinedResult = true; | ||
break; | ||
case '=': | ||
case '': | ||
if (subjectMajor === targetMajor) { | ||
if (targetMinor === null) { | ||
andResult = true; | ||
} | ||
else if (subjectMinor === targetMinor) { | ||
if (targetPatch === null || subjectPatch === targetPatch) { | ||
andResult = true; | ||
} | ||
} | ||
} | ||
} | ||
break; | ||
default: | ||
throw new Error(`range comparator was invalid: ${JSON.stringify(andRange)}`); | ||
} | ||
// if one of the and conditions failed, don't continue and checks, and note failure to the or condition | ||
if (!andResult) { | ||
orResult = false; | ||
break; | ||
default: | ||
throw new Error(`range comparator was invalid: ${JSON.stringify(condition)}`); | ||
} | ||
else { | ||
// otherwise note success to the or condition | ||
orResult = true; | ||
} | ||
} | ||
if (pass) | ||
combinedResult = true; | ||
// if the entire and conditions passed, then we can break out of the or conditions | ||
if (orResult) { | ||
return true; | ||
} | ||
} | ||
return combinedResult; | ||
// nothing passed | ||
return false; | ||
} |
export type Version = string | number | ||
export type Range = Version | Version[] | ||
const orRegex = /\s*\|\|\s*/ | ||
const rangeRegex = /^\s*([<>=~^]*)\s*([\d.]+?)[.x]*(-.+)?\s*$/ | ||
const orRegex = /\s*\|\|\s*/g | ||
const andRegex = /\s*&&\s*/g | ||
const rangeRegex = /^\s*([<>=~^]*)\s*([\d.x]+)(-[^\s]+)?\s*/ | ||
const xRegex = /[.]x/g | ||
@@ -27,150 +29,183 @@ /** | ||
// cycle through the conditions | ||
let combinedResult: boolean = false | ||
if (!Array.isArray(range)) range = String(range).split(orRegex) | ||
for (const condition of range) { | ||
// process range | ||
const [_, comparator, target, prerelease] = | ||
String(condition).match(rangeRegex) || [] | ||
const orRanges = Array.isArray(range) | ||
? range.slice() | ||
: String(range).split(orRegex) | ||
for (const orRange of orRanges) { | ||
let orResult: boolean = false | ||
const andRanges = String(orRange).split(andRegex) | ||
for (const andRange of andRanges) { | ||
let andResult: boolean = false | ||
// prepare and verify target | ||
const [targetMajor = null, targetMinor = null, targetPatch = null] = ( | ||
target || '' | ||
).split('.') | ||
if (!target || targetMajor == null || prerelease) | ||
throw new Error( | ||
`range condition was invalid: ${JSON.stringify(condition)}` | ||
) | ||
const targetMajorNumber = Number(targetMajor || 0) | ||
const targetMinorNumber = Number(targetMinor || 0) | ||
const targetPatchNumber = Number(targetPatch || 0) | ||
// process range | ||
const [match, comparator, targetRaw, prerelease] = | ||
String(andRange).match(rangeRegex) || [] | ||
const target = (targetRaw || '').replace(xRegex, '') | ||
// handle comparator | ||
const pass: boolean = false | ||
switch (comparator) { | ||
case '^': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (subjectMinorNumber === targetMinorNumber) { | ||
if (subjectPatchNumber >= targetPatchNumber) { | ||
combinedResult = true | ||
// // log | ||
// console.log({ | ||
// orRange, | ||
// andRange, | ||
// match, | ||
// comparator, | ||
// target, | ||
// prerelease, | ||
// }) | ||
// prepare and verify target | ||
const [targetMajor = null, targetMinor = null, targetPatch = null] = | ||
target.split('.') | ||
if (!match || !target || targetMajor == null || prerelease) | ||
throw new Error( | ||
`range condition was invalid: ${JSON.stringify(andRange)}` | ||
) | ||
const targetMajorNumber = Number(targetMajor || 0) | ||
const targetMinorNumber = Number(targetMinor || 0) | ||
const targetPatchNumber = Number(targetPatch || 0) | ||
// is there more range matches? add it to and condition | ||
const remainder = String(andRange).slice(match.length) | ||
if (remainder) andRanges.push(remainder) | ||
// handle comparator | ||
switch (comparator) { | ||
case '^': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (subjectMinorNumber === targetMinorNumber) { | ||
if (subjectPatchNumber >= targetPatchNumber) { | ||
andResult = true | ||
} | ||
} else if (subjectMinorNumber >= targetMinorNumber) { | ||
andResult = true | ||
} | ||
} else if (subjectMinorNumber >= targetMinorNumber) { | ||
combinedResult = true | ||
} | ||
} | ||
break | ||
case '~': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if ( | ||
subjectMinor !== null && | ||
subjectMinorNumber === targetMinorNumber | ||
) { | ||
if (subjectPatchNumber >= targetPatchNumber) { | ||
combinedResult = true | ||
break | ||
case '~': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if ( | ||
subjectMinor !== null && | ||
subjectMinorNumber === targetMinorNumber | ||
) { | ||
if (subjectPatchNumber >= targetPatchNumber) { | ||
andResult = true | ||
} | ||
} | ||
} | ||
} | ||
break | ||
case '>=': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (subjectMinorNumber === targetMinorNumber) { | ||
if (subjectPatchNumber >= targetPatchNumber) { | ||
combinedResult = true | ||
break | ||
case '>=': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (subjectMinorNumber === targetMinorNumber) { | ||
if (subjectPatchNumber >= targetPatchNumber) { | ||
andResult = true | ||
} | ||
} else if (subjectMinorNumber >= targetMinorNumber) { | ||
andResult = true | ||
} | ||
} else if (subjectMinorNumber >= targetMinorNumber) { | ||
combinedResult = true | ||
} else if (subjectMajorNumber >= targetMajorNumber) { | ||
andResult = true | ||
} | ||
} else if (subjectMajorNumber >= targetMajorNumber) { | ||
combinedResult = true | ||
} | ||
break | ||
case '>': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (targetMinor === null) { | ||
// x > x = false | ||
// x.y > x = false | ||
} else if (subjectMinorNumber === targetMinorNumber) { | ||
if (targetPatch === null) { | ||
// x.y > x.y = false | ||
// x.y.z > x.y = false | ||
} else if (subjectPatchNumber > targetPatchNumber) { | ||
combinedResult = true | ||
break | ||
case '>': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (targetMinor === null) { | ||
// x > x = false | ||
// x.y > x = false | ||
} else if (subjectMinorNumber === targetMinorNumber) { | ||
if (targetPatch === null) { | ||
// x.y > x.y = false | ||
// x.y.z > x.y = false | ||
} else if (subjectPatchNumber > targetPatchNumber) { | ||
andResult = true | ||
} | ||
} else if (subjectMinorNumber > targetMinorNumber) { | ||
andResult = true | ||
} | ||
} else if (subjectMinorNumber > targetMinorNumber) { | ||
combinedResult = true | ||
} else if (subjectMajorNumber > targetMajorNumber) { | ||
andResult = true | ||
} | ||
} else if (subjectMajorNumber > targetMajorNumber) { | ||
combinedResult = true | ||
} | ||
break | ||
case '<': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (subjectMinor === null) { | ||
// x < x = false | ||
// x < x.y = false | ||
} else if (subjectMinorNumber === targetMinorNumber) { | ||
if (subjectPatch === null) { | ||
// x.y < x.y = false | ||
// x.y < x.y.z = false | ||
} else if (subjectPatchNumber < targetPatchNumber) { | ||
combinedResult = true | ||
break | ||
case '<': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (subjectMinor === null) { | ||
// x < x = false | ||
// x < x.y = false | ||
} else if (subjectMinorNumber === targetMinorNumber) { | ||
if (subjectPatch === null) { | ||
// x.y < x.y = false | ||
// x.y < x.y.z = false | ||
} else if (subjectPatchNumber < targetPatchNumber) { | ||
andResult = true | ||
} | ||
} else if (subjectMinorNumber < targetMinorNumber) { | ||
andResult = true | ||
} | ||
} else if (subjectMinorNumber < targetMinorNumber) { | ||
combinedResult = true | ||
} else if (subjectMajorNumber < targetMajorNumber) { | ||
andResult = true | ||
} | ||
} else if (subjectMajorNumber < targetMajorNumber) { | ||
combinedResult = true | ||
} | ||
break | ||
case '<=': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (subjectMinor === null) { | ||
if (targetMinor === null) { | ||
// x <= x = true | ||
combinedResult = true | ||
} | ||
// x <= x.y = false | ||
} else if (targetMinor === null) { | ||
// x.y <= x = true | ||
combinedResult = true | ||
} else if (subjectMinorNumber === targetMinorNumber) { | ||
if (subjectPatch === null) { | ||
if (targetPatch === null) { | ||
// x.y <= x.y = true | ||
combinedResult = true | ||
break | ||
case '<=': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (subjectMinor === null) { | ||
if (targetMinor === null) { | ||
// x <= x = true | ||
andResult = true | ||
} | ||
// x.y <= x.y.z = false | ||
} else if (targetPatch === null) { | ||
// x.y.z <= x.y = true | ||
combinedResult = true | ||
} else if (subjectPatchNumber <= targetPatchNumber) { | ||
// x.y.z <= x.y.z = true | ||
combinedResult = true | ||
// x <= x.y = false | ||
} else if (targetMinor === null) { | ||
// x.y <= x = true | ||
andResult = true | ||
} else if (subjectMinorNumber === targetMinorNumber) { | ||
if (subjectPatch === null) { | ||
if (targetPatch === null) { | ||
// x.y <= x.y = true | ||
andResult = true | ||
} | ||
// x.y <= x.y.z = false | ||
} else if (targetPatch === null) { | ||
// x.y.z <= x.y = true | ||
andResult = true | ||
} else if (subjectPatchNumber <= targetPatchNumber) { | ||
// x.y.z <= x.y.z = true | ||
andResult = true | ||
} | ||
} else if (subjectMinorNumber <= targetMinorNumber) { | ||
andResult = true | ||
} | ||
} else if (subjectMinorNumber <= targetMinorNumber) { | ||
combinedResult = true | ||
} else if (subjectMajorNumber <= targetMajorNumber) { | ||
andResult = true | ||
} | ||
} else if (subjectMajorNumber <= targetMajorNumber) { | ||
combinedResult = true | ||
} | ||
break | ||
case '=': | ||
case '': | ||
if (subjectMajor === targetMajor) { | ||
if (targetMinor === null) { | ||
combinedResult = true | ||
} else if (subjectMinor === targetMinor) { | ||
if (targetPatch === null || subjectPatch === targetPatch) { | ||
combinedResult = true | ||
break | ||
case '=': | ||
case '': | ||
if (subjectMajor === targetMajor) { | ||
if (targetMinor === null) { | ||
andResult = true | ||
} else if (subjectMinor === targetMinor) { | ||
if (targetPatch === null || subjectPatch === targetPatch) { | ||
andResult = true | ||
} | ||
} | ||
} | ||
} | ||
break | ||
default: | ||
throw new Error( | ||
`range comparator was invalid: ${JSON.stringify(andRange)}` | ||
) | ||
} | ||
// if one of the and conditions failed, don't continue and checks, and note failure to the or condition | ||
if (!andResult) { | ||
orResult = false | ||
break | ||
default: | ||
throw new Error( | ||
`range comparator was invalid: ${JSON.stringify(condition)}` | ||
) | ||
} else { | ||
// otherwise note success to the or condition | ||
orResult = true | ||
} | ||
} | ||
if (pass) combinedResult = true | ||
// if the entire and conditions passed, then we can break out of the or conditions | ||
if (orResult) { | ||
return true | ||
} | ||
} | ||
return combinedResult | ||
// nothing passed | ||
return false | ||
} |
@@ -1,3 +0,5 @@ | ||
const orRegex = /\s*\|\|\s*/; | ||
const rangeRegex = /^\s*([<>=~^]*)\s*([\d.]+?)[.x]*(-.+)?\s*$/; | ||
const orRegex = /\s*\|\|\s*/g; | ||
const andRegex = /\s*&&\s*/g; | ||
const rangeRegex = /^\s*([<>=~^]*)\s*([\d.x]+)(-[^\s]+)?\s*/; | ||
const xRegex = /[.]x/g; | ||
/** | ||
@@ -18,159 +20,189 @@ * Check if the version is within the range | ||
// cycle through the conditions | ||
let combinedResult = false; | ||
if (!Array.isArray(range)) | ||
range = String(range).split(orRegex); | ||
for (const condition of range) { | ||
// process range | ||
const [_, comparator, target, prerelease] = String(condition).match(rangeRegex) || []; | ||
// prepare and verify target | ||
const [targetMajor = null, targetMinor = null, targetPatch = null] = (target || '').split('.'); | ||
if (!target || targetMajor == null || prerelease) | ||
throw new Error(`range condition was invalid: ${JSON.stringify(condition)}`); | ||
const targetMajorNumber = Number(targetMajor || 0); | ||
const targetMinorNumber = Number(targetMinor || 0); | ||
const targetPatchNumber = Number(targetPatch || 0); | ||
// handle comparator | ||
const pass = false; | ||
switch (comparator) { | ||
case '^': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (subjectMinorNumber === targetMinorNumber) { | ||
if (subjectPatchNumber >= targetPatchNumber) { | ||
combinedResult = true; | ||
const orRanges = Array.isArray(range) | ||
? range.slice() | ||
: String(range).split(orRegex); | ||
for (const orRange of orRanges) { | ||
let orResult = false; | ||
const andRanges = String(orRange).split(andRegex); | ||
for (const andRange of andRanges) { | ||
let andResult = false; | ||
// process range | ||
const [match, comparator, targetRaw, prerelease] = String(andRange).match(rangeRegex) || []; | ||
const target = (targetRaw || '').replace(xRegex, ''); | ||
// // log | ||
// console.log({ | ||
// orRange, | ||
// andRange, | ||
// match, | ||
// comparator, | ||
// target, | ||
// prerelease, | ||
// }) | ||
// prepare and verify target | ||
const [targetMajor = null, targetMinor = null, targetPatch = null] = target.split('.'); | ||
if (!match || !target || targetMajor == null || prerelease) | ||
throw new Error(`range condition was invalid: ${JSON.stringify(andRange)}`); | ||
const targetMajorNumber = Number(targetMajor || 0); | ||
const targetMinorNumber = Number(targetMinor || 0); | ||
const targetPatchNumber = Number(targetPatch || 0); | ||
// is there more range matches? add it to and condition | ||
const remainder = String(andRange).slice(match.length); | ||
if (remainder) | ||
andRanges.push(remainder); | ||
// handle comparator | ||
switch (comparator) { | ||
case '^': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (subjectMinorNumber === targetMinorNumber) { | ||
if (subjectPatchNumber >= targetPatchNumber) { | ||
andResult = true; | ||
} | ||
} | ||
} | ||
else if (subjectMinorNumber >= targetMinorNumber) { | ||
combinedResult = true; | ||
} | ||
} | ||
break; | ||
case '~': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (subjectMinor !== null && | ||
subjectMinorNumber === targetMinorNumber) { | ||
if (subjectPatchNumber >= targetPatchNumber) { | ||
combinedResult = true; | ||
else if (subjectMinorNumber >= targetMinorNumber) { | ||
andResult = true; | ||
} | ||
} | ||
} | ||
break; | ||
case '>=': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (subjectMinorNumber === targetMinorNumber) { | ||
if (subjectPatchNumber >= targetPatchNumber) { | ||
combinedResult = true; | ||
break; | ||
case '~': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (subjectMinor !== null && | ||
subjectMinorNumber === targetMinorNumber) { | ||
if (subjectPatchNumber >= targetPatchNumber) { | ||
andResult = true; | ||
} | ||
} | ||
} | ||
else if (subjectMinorNumber >= targetMinorNumber) { | ||
combinedResult = true; | ||
} | ||
} | ||
else if (subjectMajorNumber >= targetMajorNumber) { | ||
combinedResult = true; | ||
} | ||
break; | ||
case '>': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (targetMinor === null) { | ||
// x > x = false | ||
// x.y > x = false | ||
} | ||
else if (subjectMinorNumber === targetMinorNumber) { | ||
if (targetPatch === null) { | ||
// x.y > x.y = false | ||
// x.y.z > x.y = false | ||
break; | ||
case '>=': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (subjectMinorNumber === targetMinorNumber) { | ||
if (subjectPatchNumber >= targetPatchNumber) { | ||
andResult = true; | ||
} | ||
} | ||
else if (subjectPatchNumber > targetPatchNumber) { | ||
combinedResult = true; | ||
else if (subjectMinorNumber >= targetMinorNumber) { | ||
andResult = true; | ||
} | ||
} | ||
else if (subjectMinorNumber > targetMinorNumber) { | ||
combinedResult = true; | ||
else if (subjectMajorNumber >= targetMajorNumber) { | ||
andResult = true; | ||
} | ||
} | ||
else if (subjectMajorNumber > targetMajorNumber) { | ||
combinedResult = true; | ||
} | ||
break; | ||
case '<': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (subjectMinor === null) { | ||
// x < x = false | ||
// x < x.y = false | ||
} | ||
else if (subjectMinorNumber === targetMinorNumber) { | ||
if (subjectPatch === null) { | ||
// x.y < x.y = false | ||
// x.y < x.y.z = false | ||
break; | ||
case '>': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (targetMinor === null) { | ||
// x > x = false | ||
// x.y > x = false | ||
} | ||
else if (subjectPatchNumber < targetPatchNumber) { | ||
combinedResult = true; | ||
else if (subjectMinorNumber === targetMinorNumber) { | ||
if (targetPatch === null) { | ||
// x.y > x.y = false | ||
// x.y.z > x.y = false | ||
} | ||
else if (subjectPatchNumber > targetPatchNumber) { | ||
andResult = true; | ||
} | ||
} | ||
else if (subjectMinorNumber > targetMinorNumber) { | ||
andResult = true; | ||
} | ||
} | ||
else if (subjectMinorNumber < targetMinorNumber) { | ||
combinedResult = true; | ||
else if (subjectMajorNumber > targetMajorNumber) { | ||
andResult = true; | ||
} | ||
} | ||
else if (subjectMajorNumber < targetMajorNumber) { | ||
combinedResult = true; | ||
} | ||
break; | ||
case '<=': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (subjectMinor === null) { | ||
if (targetMinor === null) { | ||
// x <= x = true | ||
combinedResult = true; | ||
break; | ||
case '<': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (subjectMinor === null) { | ||
// x < x = false | ||
// x < x.y = false | ||
} | ||
// x <= x.y = false | ||
else if (subjectMinorNumber === targetMinorNumber) { | ||
if (subjectPatch === null) { | ||
// x.y < x.y = false | ||
// x.y < x.y.z = false | ||
} | ||
else if (subjectPatchNumber < targetPatchNumber) { | ||
andResult = true; | ||
} | ||
} | ||
else if (subjectMinorNumber < targetMinorNumber) { | ||
andResult = true; | ||
} | ||
} | ||
else if (targetMinor === null) { | ||
// x.y <= x = true | ||
combinedResult = true; | ||
else if (subjectMajorNumber < targetMajorNumber) { | ||
andResult = true; | ||
} | ||
else if (subjectMinorNumber === targetMinorNumber) { | ||
if (subjectPatch === null) { | ||
if (targetPatch === null) { | ||
// x.y <= x.y = true | ||
combinedResult = true; | ||
break; | ||
case '<=': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (subjectMinor === null) { | ||
if (targetMinor === null) { | ||
// x <= x = true | ||
andResult = true; | ||
} | ||
// x.y <= x.y.z = false | ||
// x <= x.y = false | ||
} | ||
else if (targetPatch === null) { | ||
// x.y.z <= x.y = true | ||
combinedResult = true; | ||
else if (targetMinor === null) { | ||
// x.y <= x = true | ||
andResult = true; | ||
} | ||
else if (subjectPatchNumber <= targetPatchNumber) { | ||
// x.y.z <= x.y.z = true | ||
combinedResult = true; | ||
else if (subjectMinorNumber === targetMinorNumber) { | ||
if (subjectPatch === null) { | ||
if (targetPatch === null) { | ||
// x.y <= x.y = true | ||
andResult = true; | ||
} | ||
// x.y <= x.y.z = false | ||
} | ||
else if (targetPatch === null) { | ||
// x.y.z <= x.y = true | ||
andResult = true; | ||
} | ||
else if (subjectPatchNumber <= targetPatchNumber) { | ||
// x.y.z <= x.y.z = true | ||
andResult = true; | ||
} | ||
} | ||
else if (subjectMinorNumber <= targetMinorNumber) { | ||
andResult = true; | ||
} | ||
} | ||
else if (subjectMinorNumber <= targetMinorNumber) { | ||
combinedResult = true; | ||
else if (subjectMajorNumber <= targetMajorNumber) { | ||
andResult = true; | ||
} | ||
} | ||
else if (subjectMajorNumber <= targetMajorNumber) { | ||
combinedResult = true; | ||
} | ||
break; | ||
case '=': | ||
case '': | ||
if (subjectMajor === targetMajor) { | ||
if (targetMinor === null) { | ||
combinedResult = true; | ||
} | ||
else if (subjectMinor === targetMinor) { | ||
if (targetPatch === null || subjectPatch === targetPatch) { | ||
combinedResult = true; | ||
break; | ||
case '=': | ||
case '': | ||
if (subjectMajor === targetMajor) { | ||
if (targetMinor === null) { | ||
andResult = true; | ||
} | ||
else if (subjectMinor === targetMinor) { | ||
if (targetPatch === null || subjectPatch === targetPatch) { | ||
andResult = true; | ||
} | ||
} | ||
} | ||
} | ||
break; | ||
default: | ||
throw new Error(`range comparator was invalid: ${JSON.stringify(andRange)}`); | ||
} | ||
// if one of the and conditions failed, don't continue and checks, and note failure to the or condition | ||
if (!andResult) { | ||
orResult = false; | ||
break; | ||
default: | ||
throw new Error(`range comparator was invalid: ${JSON.stringify(condition)}`); | ||
} | ||
else { | ||
// otherwise note success to the or condition | ||
orResult = true; | ||
} | ||
} | ||
if (pass) | ||
combinedResult = true; | ||
// if the entire and conditions passed, then we can break out of the or conditions | ||
if (orResult) { | ||
return true; | ||
} | ||
} | ||
return combinedResult; | ||
// nothing passed | ||
return false; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const orRegex = /\s*\|\|\s*/; | ||
const rangeRegex = /^\s*([<>=~^]*)\s*([\d.]+?)[.x]*(-.+)?\s*$/; | ||
const orRegex = /\s*\|\|\s*/g; | ||
const andRegex = /\s*&&\s*/g; | ||
const rangeRegex = /^\s*([<>=~^]*)\s*([\d.x]+)(-[^\s]+)?\s*/; | ||
const xRegex = /[.]x/g; | ||
/** | ||
@@ -20,160 +22,190 @@ * Check if the version is within the range | ||
// cycle through the conditions | ||
let combinedResult = false; | ||
if (!Array.isArray(range)) | ||
range = String(range).split(orRegex); | ||
for (const condition of range) { | ||
// process range | ||
const [_, comparator, target, prerelease] = String(condition).match(rangeRegex) || []; | ||
// prepare and verify target | ||
const [targetMajor = null, targetMinor = null, targetPatch = null] = (target || '').split('.'); | ||
if (!target || targetMajor == null || prerelease) | ||
throw new Error(`range condition was invalid: ${JSON.stringify(condition)}`); | ||
const targetMajorNumber = Number(targetMajor || 0); | ||
const targetMinorNumber = Number(targetMinor || 0); | ||
const targetPatchNumber = Number(targetPatch || 0); | ||
// handle comparator | ||
const pass = false; | ||
switch (comparator) { | ||
case '^': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (subjectMinorNumber === targetMinorNumber) { | ||
if (subjectPatchNumber >= targetPatchNumber) { | ||
combinedResult = true; | ||
const orRanges = Array.isArray(range) | ||
? range.slice() | ||
: String(range).split(orRegex); | ||
for (const orRange of orRanges) { | ||
let orResult = false; | ||
const andRanges = String(orRange).split(andRegex); | ||
for (const andRange of andRanges) { | ||
let andResult = false; | ||
// process range | ||
const [match, comparator, targetRaw, prerelease] = String(andRange).match(rangeRegex) || []; | ||
const target = (targetRaw || '').replace(xRegex, ''); | ||
// // log | ||
// console.log({ | ||
// orRange, | ||
// andRange, | ||
// match, | ||
// comparator, | ||
// target, | ||
// prerelease, | ||
// }) | ||
// prepare and verify target | ||
const [targetMajor = null, targetMinor = null, targetPatch = null] = target.split('.'); | ||
if (!match || !target || targetMajor == null || prerelease) | ||
throw new Error(`range condition was invalid: ${JSON.stringify(andRange)}`); | ||
const targetMajorNumber = Number(targetMajor || 0); | ||
const targetMinorNumber = Number(targetMinor || 0); | ||
const targetPatchNumber = Number(targetPatch || 0); | ||
// is there more range matches? add it to and condition | ||
const remainder = String(andRange).slice(match.length); | ||
if (remainder) | ||
andRanges.push(remainder); | ||
// handle comparator | ||
switch (comparator) { | ||
case '^': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (subjectMinorNumber === targetMinorNumber) { | ||
if (subjectPatchNumber >= targetPatchNumber) { | ||
andResult = true; | ||
} | ||
} | ||
} | ||
else if (subjectMinorNumber >= targetMinorNumber) { | ||
combinedResult = true; | ||
} | ||
} | ||
break; | ||
case '~': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (subjectMinor !== null && | ||
subjectMinorNumber === targetMinorNumber) { | ||
if (subjectPatchNumber >= targetPatchNumber) { | ||
combinedResult = true; | ||
else if (subjectMinorNumber >= targetMinorNumber) { | ||
andResult = true; | ||
} | ||
} | ||
} | ||
break; | ||
case '>=': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (subjectMinorNumber === targetMinorNumber) { | ||
if (subjectPatchNumber >= targetPatchNumber) { | ||
combinedResult = true; | ||
break; | ||
case '~': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (subjectMinor !== null && | ||
subjectMinorNumber === targetMinorNumber) { | ||
if (subjectPatchNumber >= targetPatchNumber) { | ||
andResult = true; | ||
} | ||
} | ||
} | ||
else if (subjectMinorNumber >= targetMinorNumber) { | ||
combinedResult = true; | ||
} | ||
} | ||
else if (subjectMajorNumber >= targetMajorNumber) { | ||
combinedResult = true; | ||
} | ||
break; | ||
case '>': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (targetMinor === null) { | ||
// x > x = false | ||
// x.y > x = false | ||
} | ||
else if (subjectMinorNumber === targetMinorNumber) { | ||
if (targetPatch === null) { | ||
// x.y > x.y = false | ||
// x.y.z > x.y = false | ||
break; | ||
case '>=': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (subjectMinorNumber === targetMinorNumber) { | ||
if (subjectPatchNumber >= targetPatchNumber) { | ||
andResult = true; | ||
} | ||
} | ||
else if (subjectPatchNumber > targetPatchNumber) { | ||
combinedResult = true; | ||
else if (subjectMinorNumber >= targetMinorNumber) { | ||
andResult = true; | ||
} | ||
} | ||
else if (subjectMinorNumber > targetMinorNumber) { | ||
combinedResult = true; | ||
else if (subjectMajorNumber >= targetMajorNumber) { | ||
andResult = true; | ||
} | ||
} | ||
else if (subjectMajorNumber > targetMajorNumber) { | ||
combinedResult = true; | ||
} | ||
break; | ||
case '<': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (subjectMinor === null) { | ||
// x < x = false | ||
// x < x.y = false | ||
} | ||
else if (subjectMinorNumber === targetMinorNumber) { | ||
if (subjectPatch === null) { | ||
// x.y < x.y = false | ||
// x.y < x.y.z = false | ||
break; | ||
case '>': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (targetMinor === null) { | ||
// x > x = false | ||
// x.y > x = false | ||
} | ||
else if (subjectPatchNumber < targetPatchNumber) { | ||
combinedResult = true; | ||
else if (subjectMinorNumber === targetMinorNumber) { | ||
if (targetPatch === null) { | ||
// x.y > x.y = false | ||
// x.y.z > x.y = false | ||
} | ||
else if (subjectPatchNumber > targetPatchNumber) { | ||
andResult = true; | ||
} | ||
} | ||
else if (subjectMinorNumber > targetMinorNumber) { | ||
andResult = true; | ||
} | ||
} | ||
else if (subjectMinorNumber < targetMinorNumber) { | ||
combinedResult = true; | ||
else if (subjectMajorNumber > targetMajorNumber) { | ||
andResult = true; | ||
} | ||
} | ||
else if (subjectMajorNumber < targetMajorNumber) { | ||
combinedResult = true; | ||
} | ||
break; | ||
case '<=': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (subjectMinor === null) { | ||
if (targetMinor === null) { | ||
// x <= x = true | ||
combinedResult = true; | ||
break; | ||
case '<': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (subjectMinor === null) { | ||
// x < x = false | ||
// x < x.y = false | ||
} | ||
// x <= x.y = false | ||
else if (subjectMinorNumber === targetMinorNumber) { | ||
if (subjectPatch === null) { | ||
// x.y < x.y = false | ||
// x.y < x.y.z = false | ||
} | ||
else if (subjectPatchNumber < targetPatchNumber) { | ||
andResult = true; | ||
} | ||
} | ||
else if (subjectMinorNumber < targetMinorNumber) { | ||
andResult = true; | ||
} | ||
} | ||
else if (targetMinor === null) { | ||
// x.y <= x = true | ||
combinedResult = true; | ||
else if (subjectMajorNumber < targetMajorNumber) { | ||
andResult = true; | ||
} | ||
else if (subjectMinorNumber === targetMinorNumber) { | ||
if (subjectPatch === null) { | ||
if (targetPatch === null) { | ||
// x.y <= x.y = true | ||
combinedResult = true; | ||
break; | ||
case '<=': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (subjectMinor === null) { | ||
if (targetMinor === null) { | ||
// x <= x = true | ||
andResult = true; | ||
} | ||
// x.y <= x.y.z = false | ||
// x <= x.y = false | ||
} | ||
else if (targetPatch === null) { | ||
// x.y.z <= x.y = true | ||
combinedResult = true; | ||
else if (targetMinor === null) { | ||
// x.y <= x = true | ||
andResult = true; | ||
} | ||
else if (subjectPatchNumber <= targetPatchNumber) { | ||
// x.y.z <= x.y.z = true | ||
combinedResult = true; | ||
else if (subjectMinorNumber === targetMinorNumber) { | ||
if (subjectPatch === null) { | ||
if (targetPatch === null) { | ||
// x.y <= x.y = true | ||
andResult = true; | ||
} | ||
// x.y <= x.y.z = false | ||
} | ||
else if (targetPatch === null) { | ||
// x.y.z <= x.y = true | ||
andResult = true; | ||
} | ||
else if (subjectPatchNumber <= targetPatchNumber) { | ||
// x.y.z <= x.y.z = true | ||
andResult = true; | ||
} | ||
} | ||
else if (subjectMinorNumber <= targetMinorNumber) { | ||
andResult = true; | ||
} | ||
} | ||
else if (subjectMinorNumber <= targetMinorNumber) { | ||
combinedResult = true; | ||
else if (subjectMajorNumber <= targetMajorNumber) { | ||
andResult = true; | ||
} | ||
} | ||
else if (subjectMajorNumber <= targetMajorNumber) { | ||
combinedResult = true; | ||
} | ||
break; | ||
case '=': | ||
case '': | ||
if (subjectMajor === targetMajor) { | ||
if (targetMinor === null) { | ||
combinedResult = true; | ||
} | ||
else if (subjectMinor === targetMinor) { | ||
if (targetPatch === null || subjectPatch === targetPatch) { | ||
combinedResult = true; | ||
break; | ||
case '=': | ||
case '': | ||
if (subjectMajor === targetMajor) { | ||
if (targetMinor === null) { | ||
andResult = true; | ||
} | ||
else if (subjectMinor === targetMinor) { | ||
if (targetPatch === null || subjectPatch === targetPatch) { | ||
andResult = true; | ||
} | ||
} | ||
} | ||
} | ||
break; | ||
default: | ||
throw new Error(`range comparator was invalid: ${JSON.stringify(andRange)}`); | ||
} | ||
// if one of the and conditions failed, don't continue and checks, and note failure to the or condition | ||
if (!andResult) { | ||
orResult = false; | ||
break; | ||
default: | ||
throw new Error(`range comparator was invalid: ${JSON.stringify(condition)}`); | ||
} | ||
else { | ||
// otherwise note success to the or condition | ||
orResult = true; | ||
} | ||
} | ||
if (pass) | ||
combinedResult = true; | ||
// if the entire and conditions passed, then we can break out of the or conditions | ||
if (orResult) { | ||
return true; | ||
} | ||
} | ||
return combinedResult; | ||
// nothing passed | ||
return false; | ||
} | ||
exports.default = withinVersionRange; |
{ | ||
"name": "version-range", | ||
"version": "4.1.0", | ||
"version": "4.2.0-next.1700433755.ebd54a15d9693193124b08085beded5c03fee6d3", | ||
"description": "Check version ranges like `>=N` and `X || Y || Z` with support for Node.js, Web Browsers, Deno, and TypeScript.", | ||
@@ -239,2 +239,2 @@ "homepage": "https://github.com/bevry/version-range", | ||
} | ||
} | ||
} |
@@ -74,3 +74,3 @@ <!-- TITLE/ --> | ||
Doesn't support ranges that include `&&`, `-releaseTag`, and `>X <Y`, and does not do the special handling for `0.x` versions. | ||
Doesn't do special handling for `-releaseTag` and `0.x` versions. | ||
@@ -91,3 +91,3 @@ <!-- INSTALL/ --> | ||
``` typescript | ||
import pkg from 'https://unpkg.com/version-range@^4.1.0/edition-deno/index.ts' | ||
import pkg from 'https://unpkg.com/version-range@^4.2.0/edition-deno/index.ts' | ||
``` | ||
@@ -99,3 +99,3 @@ | ||
<script type="module"> | ||
import pkg from '//cdn.skypack.dev/version-range@^4.1.0' | ||
import pkg from '//cdn.skypack.dev/version-range@^4.2.0' | ||
</script> | ||
@@ -108,3 +108,3 @@ ``` | ||
<script type="module"> | ||
import pkg from '//unpkg.com/version-range@^4.1.0' | ||
import pkg from '//unpkg.com/version-range@^4.2.0' | ||
</script> | ||
@@ -117,3 +117,3 @@ ``` | ||
<script type="module"> | ||
import pkg from '//dev.jspm.io/version-range@4.1.0' | ||
import pkg from '//dev.jspm.io/version-range@4.2.0' | ||
</script> | ||
@@ -120,0 +120,0 @@ ``` |
export type Version = string | number | ||
export type Range = Version | Version[] | ||
const orRegex = /\s*\|\|\s*/ | ||
const rangeRegex = /^\s*([<>=~^]*)\s*([\d.]+?)[.x]*(-.+)?\s*$/ | ||
const orRegex = /\s*\|\|\s*/g | ||
const andRegex = /\s*&&\s*/g | ||
const rangeRegex = /^\s*([<>=~^]*)\s*([\d.x]+)(-[^\s]+)?\s*/ | ||
const xRegex = /[.]x/g | ||
@@ -27,150 +29,183 @@ /** | ||
// cycle through the conditions | ||
let combinedResult: boolean = false | ||
if (!Array.isArray(range)) range = String(range).split(orRegex) | ||
for (const condition of range) { | ||
// process range | ||
const [_, comparator, target, prerelease] = | ||
String(condition).match(rangeRegex) || [] | ||
const orRanges = Array.isArray(range) | ||
? range.slice() | ||
: String(range).split(orRegex) | ||
for (const orRange of orRanges) { | ||
let orResult: boolean = false | ||
const andRanges = String(orRange).split(andRegex) | ||
for (const andRange of andRanges) { | ||
let andResult: boolean = false | ||
// prepare and verify target | ||
const [targetMajor = null, targetMinor = null, targetPatch = null] = ( | ||
target || '' | ||
).split('.') | ||
if (!target || targetMajor == null || prerelease) | ||
throw new Error( | ||
`range condition was invalid: ${JSON.stringify(condition)}` | ||
) | ||
const targetMajorNumber = Number(targetMajor || 0) | ||
const targetMinorNumber = Number(targetMinor || 0) | ||
const targetPatchNumber = Number(targetPatch || 0) | ||
// process range | ||
const [match, comparator, targetRaw, prerelease] = | ||
String(andRange).match(rangeRegex) || [] | ||
const target = (targetRaw || '').replace(xRegex, '') | ||
// handle comparator | ||
const pass: boolean = false | ||
switch (comparator) { | ||
case '^': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (subjectMinorNumber === targetMinorNumber) { | ||
if (subjectPatchNumber >= targetPatchNumber) { | ||
combinedResult = true | ||
// // log | ||
// console.log({ | ||
// orRange, | ||
// andRange, | ||
// match, | ||
// comparator, | ||
// target, | ||
// prerelease, | ||
// }) | ||
// prepare and verify target | ||
const [targetMajor = null, targetMinor = null, targetPatch = null] = | ||
target.split('.') | ||
if (!match || !target || targetMajor == null || prerelease) | ||
throw new Error( | ||
`range condition was invalid: ${JSON.stringify(andRange)}` | ||
) | ||
const targetMajorNumber = Number(targetMajor || 0) | ||
const targetMinorNumber = Number(targetMinor || 0) | ||
const targetPatchNumber = Number(targetPatch || 0) | ||
// is there more range matches? add it to and condition | ||
const remainder = String(andRange).slice(match.length) | ||
if (remainder) andRanges.push(remainder) | ||
// handle comparator | ||
switch (comparator) { | ||
case '^': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (subjectMinorNumber === targetMinorNumber) { | ||
if (subjectPatchNumber >= targetPatchNumber) { | ||
andResult = true | ||
} | ||
} else if (subjectMinorNumber >= targetMinorNumber) { | ||
andResult = true | ||
} | ||
} else if (subjectMinorNumber >= targetMinorNumber) { | ||
combinedResult = true | ||
} | ||
} | ||
break | ||
case '~': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if ( | ||
subjectMinor !== null && | ||
subjectMinorNumber === targetMinorNumber | ||
) { | ||
if (subjectPatchNumber >= targetPatchNumber) { | ||
combinedResult = true | ||
break | ||
case '~': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if ( | ||
subjectMinor !== null && | ||
subjectMinorNumber === targetMinorNumber | ||
) { | ||
if (subjectPatchNumber >= targetPatchNumber) { | ||
andResult = true | ||
} | ||
} | ||
} | ||
} | ||
break | ||
case '>=': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (subjectMinorNumber === targetMinorNumber) { | ||
if (subjectPatchNumber >= targetPatchNumber) { | ||
combinedResult = true | ||
break | ||
case '>=': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (subjectMinorNumber === targetMinorNumber) { | ||
if (subjectPatchNumber >= targetPatchNumber) { | ||
andResult = true | ||
} | ||
} else if (subjectMinorNumber >= targetMinorNumber) { | ||
andResult = true | ||
} | ||
} else if (subjectMinorNumber >= targetMinorNumber) { | ||
combinedResult = true | ||
} else if (subjectMajorNumber >= targetMajorNumber) { | ||
andResult = true | ||
} | ||
} else if (subjectMajorNumber >= targetMajorNumber) { | ||
combinedResult = true | ||
} | ||
break | ||
case '>': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (targetMinor === null) { | ||
// x > x = false | ||
// x.y > x = false | ||
} else if (subjectMinorNumber === targetMinorNumber) { | ||
if (targetPatch === null) { | ||
// x.y > x.y = false | ||
// x.y.z > x.y = false | ||
} else if (subjectPatchNumber > targetPatchNumber) { | ||
combinedResult = true | ||
break | ||
case '>': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (targetMinor === null) { | ||
// x > x = false | ||
// x.y > x = false | ||
} else if (subjectMinorNumber === targetMinorNumber) { | ||
if (targetPatch === null) { | ||
// x.y > x.y = false | ||
// x.y.z > x.y = false | ||
} else if (subjectPatchNumber > targetPatchNumber) { | ||
andResult = true | ||
} | ||
} else if (subjectMinorNumber > targetMinorNumber) { | ||
andResult = true | ||
} | ||
} else if (subjectMinorNumber > targetMinorNumber) { | ||
combinedResult = true | ||
} else if (subjectMajorNumber > targetMajorNumber) { | ||
andResult = true | ||
} | ||
} else if (subjectMajorNumber > targetMajorNumber) { | ||
combinedResult = true | ||
} | ||
break | ||
case '<': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (subjectMinor === null) { | ||
// x < x = false | ||
// x < x.y = false | ||
} else if (subjectMinorNumber === targetMinorNumber) { | ||
if (subjectPatch === null) { | ||
// x.y < x.y = false | ||
// x.y < x.y.z = false | ||
} else if (subjectPatchNumber < targetPatchNumber) { | ||
combinedResult = true | ||
break | ||
case '<': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (subjectMinor === null) { | ||
// x < x = false | ||
// x < x.y = false | ||
} else if (subjectMinorNumber === targetMinorNumber) { | ||
if (subjectPatch === null) { | ||
// x.y < x.y = false | ||
// x.y < x.y.z = false | ||
} else if (subjectPatchNumber < targetPatchNumber) { | ||
andResult = true | ||
} | ||
} else if (subjectMinorNumber < targetMinorNumber) { | ||
andResult = true | ||
} | ||
} else if (subjectMinorNumber < targetMinorNumber) { | ||
combinedResult = true | ||
} else if (subjectMajorNumber < targetMajorNumber) { | ||
andResult = true | ||
} | ||
} else if (subjectMajorNumber < targetMajorNumber) { | ||
combinedResult = true | ||
} | ||
break | ||
case '<=': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (subjectMinor === null) { | ||
if (targetMinor === null) { | ||
// x <= x = true | ||
combinedResult = true | ||
} | ||
// x <= x.y = false | ||
} else if (targetMinor === null) { | ||
// x.y <= x = true | ||
combinedResult = true | ||
} else if (subjectMinorNumber === targetMinorNumber) { | ||
if (subjectPatch === null) { | ||
if (targetPatch === null) { | ||
// x.y <= x.y = true | ||
combinedResult = true | ||
break | ||
case '<=': | ||
if (subjectMajorNumber === targetMajorNumber) { | ||
if (subjectMinor === null) { | ||
if (targetMinor === null) { | ||
// x <= x = true | ||
andResult = true | ||
} | ||
// x.y <= x.y.z = false | ||
} else if (targetPatch === null) { | ||
// x.y.z <= x.y = true | ||
combinedResult = true | ||
} else if (subjectPatchNumber <= targetPatchNumber) { | ||
// x.y.z <= x.y.z = true | ||
combinedResult = true | ||
// x <= x.y = false | ||
} else if (targetMinor === null) { | ||
// x.y <= x = true | ||
andResult = true | ||
} else if (subjectMinorNumber === targetMinorNumber) { | ||
if (subjectPatch === null) { | ||
if (targetPatch === null) { | ||
// x.y <= x.y = true | ||
andResult = true | ||
} | ||
// x.y <= x.y.z = false | ||
} else if (targetPatch === null) { | ||
// x.y.z <= x.y = true | ||
andResult = true | ||
} else if (subjectPatchNumber <= targetPatchNumber) { | ||
// x.y.z <= x.y.z = true | ||
andResult = true | ||
} | ||
} else if (subjectMinorNumber <= targetMinorNumber) { | ||
andResult = true | ||
} | ||
} else if (subjectMinorNumber <= targetMinorNumber) { | ||
combinedResult = true | ||
} else if (subjectMajorNumber <= targetMajorNumber) { | ||
andResult = true | ||
} | ||
} else if (subjectMajorNumber <= targetMajorNumber) { | ||
combinedResult = true | ||
} | ||
break | ||
case '=': | ||
case '': | ||
if (subjectMajor === targetMajor) { | ||
if (targetMinor === null) { | ||
combinedResult = true | ||
} else if (subjectMinor === targetMinor) { | ||
if (targetPatch === null || subjectPatch === targetPatch) { | ||
combinedResult = true | ||
break | ||
case '=': | ||
case '': | ||
if (subjectMajor === targetMajor) { | ||
if (targetMinor === null) { | ||
andResult = true | ||
} else if (subjectMinor === targetMinor) { | ||
if (targetPatch === null || subjectPatch === targetPatch) { | ||
andResult = true | ||
} | ||
} | ||
} | ||
} | ||
break | ||
default: | ||
throw new Error( | ||
`range comparator was invalid: ${JSON.stringify(andRange)}` | ||
) | ||
} | ||
// if one of the and conditions failed, don't continue and checks, and note failure to the or condition | ||
if (!andResult) { | ||
orResult = false | ||
break | ||
default: | ||
throw new Error( | ||
`range comparator was invalid: ${JSON.stringify(condition)}` | ||
) | ||
} else { | ||
// otherwise note success to the or condition | ||
orResult = true | ||
} | ||
} | ||
if (pass) combinedResult = true | ||
// if the entire and conditions passed, then we can break out of the or conditions | ||
if (orResult) { | ||
return true | ||
} | ||
} | ||
return combinedResult | ||
// nothing passed | ||
return false | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
73651
1047
1