Chromium Code Reviews| Index: lib/src/utils.dart |
| diff --git a/lib/src/utils.dart b/lib/src/utils.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b0c5a19b54bc3d223a44e37ab4da5d20ebd4b7a0 |
| --- /dev/null |
| +++ b/lib/src/utils.dart |
| @@ -0,0 +1,58 @@ |
| +// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +library pub_semver.src.utils; |
| + |
| +import 'version.dart'; |
| +import 'version_constraint.dart'; |
| +import 'version_range.dart'; |
| + |
| +/// Returns whether [constraint1] is immediately next to, but not overlapping, |
| +/// [constraint2]. |
| +/// |
| +/// This assumes both constraints are either a [VersionRange] or a [Version]. |
| +bool areAdjacent(VersionConstraint constraint1, VersionConstraint constraint2) { |
| + if (_max(constraint1) != _min(constraint2)) return false; |
| + |
| + var includeMax = _includeMax(constraint1); |
| + var includeMin = _includeMin(constraint2); |
| + return (includeMax && !includeMin) || (!includeMax && includeMin); |
| +} |
| + |
| +/// A [Comparator] that compares the maximum versions of [constraint1] and |
| +/// [constraint2]. |
| +/// |
| +/// This assumes both constraints are either a [VersionRange] or a [Version]. |
| +int compareMax(VersionConstraint constraint1, VersionConstraint constraint2) { |
| + var max1 = _max(constraint1); |
| + var max2 = _max(constraint2); |
| + if (max1 < max2) return -1; |
| + if (max1 > max2) return 1; |
| + |
| + var includeMax1 = _includeMax(constraint1); |
| + var includeMax2 = _includeMax(constraint2); |
| + if (!includeMax1 && includeMax2) return -1; |
| + if (includeMax1 && !includeMax2) return 1; |
| + return 0; |
| +} |
| + |
|
Bob Nystrom
2015/05/05 20:49:09
Instead of these functions, how about just making
nweiz
2015/05/05 22:52:33
Done.
|
| +/// Returns the minimum version of [constraint], which may be a [VersionRange] |
| +/// or a [Version]. |
| +Version _min(VersionConstraint constraint) => |
| + constraint is VersionRange ? constraint.min : (constraint as Version); |
| + |
| +/// Returns whether [constraint], which may be a [VersionRange] or a [Version], |
| +/// includes its minimum value. |
| +bool _includeMin(VersionConstraint constraint) => |
|
Bob Nystrom
2015/05/05 20:49:09
How about "_includesMin"?
|
| + constraint is VersionRange ? constraint.includeMin : true; |
| + |
| +/// Returns the maximum version of [constraint], which may be a [VersionRange] |
| +/// or a [Version]. |
| +Version _max(VersionConstraint constraint) => |
| + constraint is VersionRange ? constraint.max : (constraint as Version); |
| + |
| +/// Returns whether [constraint], which may be a [VersionRange] or a [Version], |
| +/// includes its maximum value. |
| +bool _includeMax(VersionConstraint constraint) => |
|
Bob Nystrom
2015/05/05 20:49:08
Ditto.
|
| + constraint is VersionRange ? constraint.includeMax : true; |