Skip to content

Commit

Permalink
Merge pull request #44 from KevBelisle/master
Browse files Browse the repository at this point in the history
Add bool Comparator.Intersects(Comparator) method
  • Loading branch information
adamreeve authored Sep 28, 2019
2 parents 314af27 + fcdcdf2 commit 5ba45f9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
29 changes: 29 additions & 0 deletions src/SemVer/Comparator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,35 @@ public bool IsSatisfied(Version version)
}
}

public bool Intersects(Comparator other)
{
Func<Comparator, bool> operatorIsGreaterThan = c =>
c.ComparatorType == Operator.GreaterThan ||
c.ComparatorType == Operator.GreaterThanOrEqual;
Func<Comparator, bool> operatorIsLessThan = c =>
c.ComparatorType == Operator.LessThan ||
c.ComparatorType == Operator.LessThanOrEqual;
Func<Comparator, bool> operatorIncludesEqual = c =>
c.ComparatorType == Operator.GreaterThanOrEqual ||
c.ComparatorType == Operator.Equal ||
c.ComparatorType == Operator.LessThanOrEqual;

if (this.Version > other.Version && (operatorIsLessThan(this) || operatorIsGreaterThan(other)))
return true;

if (this.Version < other.Version && (operatorIsGreaterThan(this) || operatorIsLessThan(other)))
return true;

if (this.Version == other.Version && (
(operatorIncludesEqual(this) && operatorIncludesEqual(other)) ||
(operatorIsLessThan(this) && operatorIsLessThan(other)) ||
(operatorIsGreaterThan(this) && operatorIsGreaterThan(other))
))
return true;

return false;
}

public enum Operator
{
Equal = 0,
Expand Down
2 changes: 1 addition & 1 deletion src/SemVer/ComparatorSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public ComparatorSet Intersect(ComparatorSet other)
_comparators.Concat(other._comparators)
.Where(operatorIsLessThan)
.OrderBy(c => c.Version).FirstOrDefault();
if (maxOfMins != null && minOfMaxs != null && maxOfMins.Version > minOfMaxs.Version)
if (maxOfMins != null && minOfMaxs != null && !maxOfMins.Intersects(minOfMaxs))
{
return null;
}
Expand Down
2 changes: 2 additions & 0 deletions test/SemVer.Tests/RangeOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class RangeOperations
[InlineData("=1.2.3", "=1.2.3", "=1.2.3")]
[InlineData("=1.2.3", "=1.2.4", "<0.0.0")]
[InlineData("~1.2.3 || ~1.3.2", ">=1.2.9 < 1.3.8", ">=1.2.9 < 1.3.0 || >=1.3.2 < 1.3.8")]
[InlineData("<3.0.0", ">=3.0.0", "<0.0.0")]
[InlineData("^2", "^3", "<0.0.0")]
public void Intersect(string a, string b, string intersect)
{
var rangeA = new Range(a);
Expand Down

0 comments on commit 5ba45f9

Please sign in to comment.