-
Notifications
You must be signed in to change notification settings - Fork 492
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Best way to find the best theoretical version within a range #166
Comments
So, let me start by saying that what you're looking for is mathematically impossible. Here's why: Let R be the set of versions that satisfy the range If the version range does not have an upper bound, then it's even more transparently impossible. Given the set R of all versions satisfying the range For ranges with an inclusive upper limit, it is be possible. For example, the range If you wanted to determine if a range has an upper bound, and what that upper bound is, you could do this: function hasUpperBound (range) {
range = new semver.Range(range)
if (!range) return false
return range.set.filter(function (subset) {
return subset.some(function (comparator) {
return comparator.operator.match(/^</)
})
}).length === range.set.length
} Of course, if by "best" you mean something other than "highest precedence order", then I'm not sure what kind of normativity you're applying here, and the problem becomes even less well-defined :) |
To find out what the upper bound is, you can use this: function upperBound (range) {
range = new semver.Range(range)
if (!hasUpperBound(range)) return null
return range.set.map(function (subset) {
return subset.filter(function (comparator) {
return /^</.test(comparator.operator)
})
}).map(function (subset) {
return subset.sort(function (a, b) {
return semver.compare(a.semver, b.semver)
})[0]
}).sort(function (a, b) {
return semver.rcompare(a.semver, b.semver)
}).slice(0, 1).map(function (comparator) {
return comparator.value
})[0]
} |
FYI, the above doesn't take into account ranges with prerelease tags (
|
I understand this may be asking for the impossible. But what's the best approach to figuring out the best theoretical version within a given range, when you don't know what versions exist (ie:
maxSatisfying
won't do the trick).The only approach with the existing APIs I've found so far is to brute force attempt major/minor/patch combinations until it no longer
satisfies
the range. But with the semver range parser, I can imagine a faster solution might be possible. I understand that "don't do this" is a valid response, but nonetheless, thoughts and ideas welcome.The text was updated successfully, but these errors were encountered: