VersionConstraint.unionOf constructor

VersionConstraint.unionOf(
  1. Iterable<VersionConstraint> constraints
)

Creates a new version constraint that is the union of constraints.

It allows any versions that any of those constraints allows. If constraints is empty, this returns a constraint that allows no versions.

Implementation

factory VersionConstraint.unionOf(Iterable<VersionConstraint> constraints) {
  var flattened = constraints.expand((constraint) {
    if (constraint.isEmpty) return <VersionRange>[];
    if (constraint is VersionUnion) return constraint.ranges;
    if (constraint is VersionRange) return [constraint];
    throw ArgumentError('Unknown VersionConstraint type $constraint.');
  }).toList();

  if (flattened.isEmpty) return VersionConstraint.empty;

  if (flattened.any((constraint) => constraint.isAny)) {
    return VersionConstraint.any;
  }

  flattened.sort();

  var merged = <VersionRange>[];
  for (var constraint in flattened) {
    // Merge this constraint with the previous one, but only if they touch.
    if (merged.isEmpty ||
        (!merged.last.allowsAny(constraint) &&
            !areAdjacent(merged.last, constraint))) {
      merged.add(constraint);
    } else {
      merged[merged.length - 1] =
          merged.last.union(constraint) as VersionRange;
    }
  }

  if (merged.length == 1) return merged.single;
  return VersionUnion.fromRanges(merged);
}