Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optional comparer, export equality #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/dart_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export 'package:dart_extensions/src/flutter/container.dart';
export 'package:dart_extensions/src/flutter/gesture_detector.dart';
export 'package:dart_extensions/src/flutter/navigation.dart';
export 'package:dart_extensions/src/flutter/padding.dart';
export 'package:dart_extensions/src/equality.dart';
export 'package:dart_extensions/src/http.dart';
export 'package:dart_extensions/src/int.dart';
export 'package:dart_extensions/src/iterable.dart';
Expand All @@ -32,4 +33,3 @@ export 'package:dart_extensions/src/search_algorithms.dart';
export 'package:dart_extensions/src/sort_algorithms.dart';
export 'package:dart_extensions/src/string_ext.dart';
export 'package:dart_extensions/src/flutter/widgets.dart';

17 changes: 11 additions & 6 deletions lib/src/iterable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extension CollectionsNullableExtensions<T> on Iterable<T>? {
///Returns `true` if this nullable iterable is either null or empty.
bool get isEmptyOrNull => (this?.isEmpty ?? true);

/// Returns `true` if at least one element matches the given [predicate].
/// Returns `true` if at least one element matches the given [predicate].
bool any(bool predicate(T element)) {
if (this.isEmptyOrNull) return false;
for (final element in this.orEmpty()) if (predicate(element)) return true;
Expand All @@ -52,7 +52,9 @@ extension CollectionsNullableExtensions<T> on Iterable<T>? {
/// the combination of them two.
zip<T>(Iterable<T> iterable) sync* {
if (iterable.isEmptyOrNull) return;
final iterables = List<Iterable>.empty()..add(this.orEmpty())..add(iterable);
final iterables = List<Iterable>.empty()
..add(this.orEmpty())
..add(iterable);

final iterators = iterables.map((e) => e.iterator).toList(growable: false);
while (iterators.every((e) => e.moveNext())) {
Expand All @@ -65,9 +67,10 @@ extension CollectionsExtensions<T> on Iterable<T> {
///Sorts elements in the array in-place according to natural sort order of the value returned by specified [selector] function.
Iterable<T> sortBy<TKey>(
TKey Function(T) keySelector, {
required EqualityComparer<TKey> keyComparer,
EqualityComparer<TKey>? keyComparer,
}) {
return InternalOrderedIterable(this, keySelector, keyComparer, false);
return InternalOrderedIterable(
this, keySelector, keyComparer ?? EqualityComparer<TKey>(), false);
}

/// Convert iterable to set
Expand Down Expand Up @@ -205,7 +208,8 @@ extension CollectionsExtensions<T> on Iterable<T> {
T firstOrDefault(T defaultValue) => firstOrNull ?? defaultValue;

/// Will retrun new [Iterable] with all elements that satisfy the predicate [predicate],
Iterable<T> whereIndexed(IndexedPredicate<T> predicate) => _IndexedWhereIterable(this, predicate);
Iterable<T> whereIndexed(IndexedPredicate<T> predicate) =>
_IndexedWhereIterable(this, predicate);

///
/// Performs the given action on each element on iterable, providing sequential index with the element.
Expand Down Expand Up @@ -365,7 +369,8 @@ extension CollectionsExtensions<T> on Iterable<T> {
Iterable<List<T>> chunks(int size) => partition(this, size);

/// Creates a Map instance in which the keys and values are computed from the iterable.
Map<dynamic, dynamic> associate(key(element), value(element)) => Map.fromIterable(this, key: key, value: value);
Map<dynamic, dynamic> associate(key(element), value(element)) =>
Map.fromIterable(this, key: key, value: value);

/// Returns the first element matching the given [predicate], or `null`
/// if element was not found.
Expand Down