diff --git a/sdk/lib/collection/iterable_comparable_extensions.dart b/sdk/lib/collection/iterable_comparable_extensions.dart new file mode 100644 index 000000000000..52de6d40c2bd --- /dev/null +++ b/sdk/lib/collection/iterable_comparable_extensions.dart @@ -0,0 +1,39 @@ +// Copyright (c) 2012, 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. + +// @dart = 2.6 + +part of dart.collection; + +extension IterableComparableExtensions on Iterable { + /* + * Returns the largest value in the iterable determined via + * [Comparable.toCompare()]. + * + * The iterable must have at least one element, otherwise + * [IterableElementError] gets thrown. + * If it has only one element, that element is returned. + * + * If multiple items are maximal, the function returns the first one encountered. + */ + Comparable get max { + return this.reduce((Comparable value, Comparable element) => + value.compareTo(element) >= 0 ? value : element); + } + + /* + * Returns the largest value in the iterable determined via + * [Comparable.toCompare()]. + * + * The iterable must have at least one element, otherwise + * [IterableElementError] gets thrown. + * If it has only one element, that element is returned. + * + * If multiple items are maximal, the function returns the first one encountered. + */ + Comparable get min { + return this.reduce((Comparable value, Comparable element) => + value.compareTo(element) <= 0 ? value : element); + } +} \ No newline at end of file diff --git a/tests/lib/collection/iterable_comparable_extensions_test.dart b/tests/lib/collection/iterable_comparable_extensions_test.dart new file mode 100644 index 000000000000..fa8c5f5aa3f0 --- /dev/null +++ b/tests/lib/collection/iterable_comparable_extensions_test.dart @@ -0,0 +1,36 @@ +// Copyright (c) 2011, 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. + +import "dart:collection"; +import "package:expect/expect.dart"; + +void main() { + test('Iterable.max return the highest int', () { + expect([2, 1, 3, 0].max, 3); + expect([2, 1, 3, 10].max, 10); + }); + test('Iterable.max return the lowest int', () { + expect([-2, 1, -1, 0].min, -2); + expect([2, -1, -1, 0].min, -1); + }); + test('Iterable.min handles nan as bigger then any other double', () { + expect([double.infinity, double.nan].min, double.infinity); + }); + test('Iterable.max return the longest Duration', () { + expect([Duration(hours: 2), Duration(hours: 1)].max, Duration(hours: 2)); + }); + test('Iterable.min return the shortest Duration', () { + expect([Duration(hours: 2), Duration(hours: 1)].min, Duration(hours: 1)); + }); + test( + 'Iterable.min returns the first item when ' + 'multiple items have the same value', () { + Duration firstDuration = Duration(hours: 1); + Duration secondDuration = Duration(hours: 1); + Duration minimumDuration = [firstDuration, secondDuration].min; + + expect(identical(firstDuration, minimumDuration), true); + expect(identical(secondDuration, minimumDuration), false); + }); +}