adamreeve / semver.net

Semantic versioning for .NET
MIT License
117 stars 22 forks source link

Suggestion - Range Operators: Intersect/Union/Except #25

Closed athibodeau-hibernum closed 5 years ago

athibodeau-hibernum commented 7 years ago

Since the internal constituents of a parsed range are not exposed, it would be great to have methods to manipulate ranges. I would suggest Union, Intersect and Except. This would allow, for example:

var range1a = new Range("^1.3.0");
var range1b = new Range("1.2.0");
var union = range1a.Union(range1b); // Equivalent to ^1.3.0 || 1.2.0

var range2a = new Range("~1.2.3");
var range2b = new Range(">=1.2.0 < 1.2.6");
var intersect = range2a.Intersect(range2b); // Equivalent to 1.2.3 - 1.2.5

var range3a = new Range("^1.3.0");
var range3b = new Range("1.4.3");
var except = range3a.Except(range3b); // Equivalent to 1.3.0 - 1.4.2 || ^1.4.4
adamreeve commented 7 years ago

This seems like it could get a bit complicated for more complex Range definitions, so I'm not that keen to implement it unless there's a strong desire for it. Do you have a use-case where this would be useful?

athibodeau-hibernum commented 7 years ago

In the package management application that we are developing, if a dependency is resolved to a version that is higher than the minimum allowed, we want to bump the minimum of the dependency to that point:

var minRange = new Range(">=" + resolvedVersion.ToString());
var newRange = currentRange.Intersect(minRange);

Without an access to the parsed components, we will have to parse it by ourselves, reinventing Range.

athibodeau-hibernum commented 7 years ago

Otherwise, what about making the internals of Range available? With proper interfaces and separation, Range could remain immutable, while easing the creation of new ranges based on current ranges.

adamreeve commented 7 years ago

Ok that seems like a pretty legitimate use case and it would be good to be able to make that possible. Exposing the internal Comparator and ComparatorSet classes would be more flexible and powerful but might take a little bit more thinking about how to do that nicely, so I'd lean towards going with just providing an Intersect method.

athibodeau-hibernum commented 7 years ago

Thanks for implementing Intersect!