diff --git a/CHANGELOG.md b/CHANGELOG.md index c5d838b..84c024c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,29 @@ -## 0.4.2+1 - -- Support the `package:collection` `^1.14.11`. +## 0.5.0 + +- [PR-94](https://github.com/leisim/dartx/pull/94) Breaking: `Iterable.zip(other, transform)` now support zipping iterables of other types the `T`. This is a breaking change because it is now required to add types in the transform function. + ```dart + final amounts = [2, 3, 4]; + final animals = ['dogs', 'birds', 'cats']; + final all = amounts.zip(animals, (int amount, String animal) => '$amount $animal'); + // lambda types are now required: ⇧ ⇧ + // all: ['2 dogs', '3 birds', '4 cats'] + ``` +- [PR-90](https://github.com/leisim/dartx/pull/90) New `String` extensions + - `String removePrefix(String prefix)` + - `String removeSuffix(String suffix)` + - `String removeSurrounding({String prefix, String suffix})` + ```dart + final name = 'James Bond'.removePrefix('James '); // Bond + final milliseconds = '100ms'.removeSuffix('ms'); // 100 + final text = '

Some HTML

'.removeSurrounding(prefix: '

', suffix: '

'); // Some HTML + ``` +- [PR-88](https://github.com/leisim/dartx/pull/88) New `List` extension `void swap(int, int)` which swaps the elements in the indices provided. + ```dart + final list = [1, 2, 3, 4]; + list.swap(0, 2); // [3, 2, 1, 4] + ``` +- [PR-100](https://github.com/leisim/dartx/pull/100) Relax the upper version constraint of `collection` to support Flutter 1.20 (which uses `collection: 1.3.0-nullsafety`) +- [PR-101](https://github.com/leisim/dartx/pull/101) Relax the upper version constraint of `crypto` ## 0.4.2 diff --git a/lib/dartx.dart b/lib/dartx.dart index b95ffd4..82e691d 100644 --- a/lib/dartx.dart +++ b/lib/dartx.dart @@ -9,6 +9,7 @@ import 'dart:typed_data'; import 'package:characters/characters.dart'; import 'package:collection/collection.dart' hide DelegatingList; import 'package:crypto/crypto.dart' as crypto; +import 'package:meta/meta.dart'; export 'package:time/time.dart'; export 'package:characters/characters.dart'; diff --git a/lib/src/iterable.dart b/lib/src/iterable.dart index 3e2a2bd..3a5057d 100644 --- a/lib/src/iterable.dart +++ b/lib/src/iterable.dart @@ -58,7 +58,7 @@ extension IterableX on Iterable { /// var first = list.elementAtOrElse(0); // 1 /// var fifth = list.elementAtOrElse(4, -1); // -1 /// ``` - E elementAtOrElse(int index, E defaultValue(int index)) { + E elementAtOrElse(int index, E Function(int index) defaultValue) { if (index < 0) return defaultValue(index); var count = 0; for (var element in this) { @@ -91,7 +91,7 @@ extension IterableX on Iterable { /// var firstLong= list.firstOrNullWhere((e) => e.length > 1); // 'Test' /// var firstVeryLong = list.firstOrNullWhere((e) => e.length > 5); // null /// ``` - E firstOrNullWhere(bool predicate(E element)) { + E firstOrNullWhere(bool Function(E element) predicate) { return firstWhere(predicate, orElse: () => null); } @@ -108,7 +108,7 @@ extension IterableX on Iterable { /// Returns the last element matching the given [predicate], or `null` if no /// such element was found. - E lastOrNullWhere(bool predicate(E element)) { + E lastOrNullWhere(bool Function(E element) predicate) { return lastWhere(predicate, orElse: () => null); } @@ -122,7 +122,7 @@ extension IterableX on Iterable { /// Returns true if all elements match the given [predicate] or if the /// collection is empty. - bool all(bool predicate(E element)) { + bool all(bool Function(E element) predicate) { for (var element in this) { if (!predicate(element)) { return false; @@ -133,7 +133,7 @@ extension IterableX on Iterable { /// Returns true if no entries match the given [predicate] or if the /// collection is empty. - bool none(bool predicate(E element)) => !any(predicate); + bool none(bool Function(E element) predicate) => !any(predicate); /// Returns a new list containing elements at indices between [start] /// (inclusive) and [end] (inclusive). @@ -156,7 +156,7 @@ extension IterableX on Iterable { /// Performs the given [action] on each element, providing sequential index /// with the element. - void forEachIndexed(void action(E element, int index)) { + void forEachIndexed(void Function(E element, int index) action) { var index = 0; for (var element in this) { action(element, index++); @@ -188,7 +188,7 @@ extension IterableX on Iterable { /// /// If [checkEqual] is provided, it is used to check if two elements are the /// same. - bool contentEquals(Iterable other, [bool checkEqual(E a, E b)]) { + bool contentEquals(Iterable other, [bool Function(E a, E b) checkEqual]) { var it1 = iterator; var it2 = other.iterator; if (checkEqual != null) { @@ -231,7 +231,7 @@ extension IterableX on Iterable { /// /// **Note:** The actual sorting is performed when an element is accessed for /// the first time. - _SortedList sortedBy(Comparable selector(E element)) { + _SortedList sortedBy(Comparable Function(E element) selector) { return _SortedList._withSelector(this, selector, 1, null); } @@ -244,7 +244,7 @@ extension IterableX on Iterable { /// /// **Note:** The actual sorting is performed when an element is accessed for /// the first time. - _SortedList sortedByDescending(Comparable selector(E element)) { + _SortedList sortedByDescending(Comparable Function(E element) selector) { return _SortedList._withSelector(this, selector, -1, null); } @@ -268,7 +268,7 @@ extension IterableX on Iterable { /// followed by the [truncated] string (which defaults to `'...'`). String joinToString({ String separator = ', ', - String transform(E element), + String Function(E element) transform, String prefix = '', String postfix = '', int limit, @@ -303,7 +303,7 @@ extension IterableX on Iterable { /// each element in the collection. /// /// `null` values are not counted. - double sumBy(num selector(E element)) { + double sumBy(num Function(E element) selector) { var sum = 0.0; for (var current in this) { sum += selector(current) ?? 0; @@ -315,7 +315,7 @@ extension IterableX on Iterable { /// the collection. /// /// `null` values are counted as 0. Empty collections return `null`. - double averageBy(num selector(E element)) { + double averageBy(num Function(E element) selector) { var count = 0; num sum = 0; @@ -341,7 +341,7 @@ extension IterableX on Iterable { /// Returns the first element yielding the smallest value of the given /// [selector] or `null` if there are no elements. - E minBy(Comparable selector(E element)) => _minMaxBy(-1, selector); + E minBy(Comparable Function(E element) selector) => _minMaxBy(-1, selector); /// Returns the first element having the smallest value according to the /// provided [comparator] or `null` if there are no elements. @@ -354,7 +354,7 @@ extension IterableX on Iterable { /// Returns the first element yielding the largest value of the given /// [selector] or `null` if there are no elements. - E maxBy(Comparable selector(E element)) => _minMaxBy(1, selector); + E maxBy(Comparable Function(E element) selector) => _minMaxBy(1, selector); /// Returns the first element having the largest value according to the /// provided [comparator] or `null` if there are no elements. @@ -374,7 +374,7 @@ extension IterableX on Iterable { return currentMin; } - E _minMaxBy(int order, Comparable selector(E element)) { + E _minMaxBy(int order, Comparable Function(E element) selector) { var it = iterator; it.moveNext(); @@ -408,7 +408,7 @@ extension IterableX on Iterable { /// Returns the number of elements matching the given [predicate]. /// /// If no [predicate] is given, this equals to [length]. - int count([bool predicate(E element)]) { + int count([bool Function(E element) predicate]) { var count = 0; if (predicate == null) { return length; @@ -467,7 +467,7 @@ extension IterableX on Iterable { /// print(chars.takeLast(2)) // [8, 9] /// print(chars.takeLastWhile((it) => it > 5 }) // [6, 7, 8, 9] /// ``` - Iterable firstWhile(bool predicate(E element)) sync* { + Iterable firstWhile(bool Function(E element) predicate) sync* { for (var element in this) { if (!predicate(element)) break; yield element; @@ -483,7 +483,7 @@ extension IterableX on Iterable { /// print(chars.takeLast(2)) // [8, 9] /// print(chars.takeLastWhile((it) => it > 5 }) // [6, 7, 8, 9] /// ``` - Iterable lastWhile(bool predicate(E element)) { + Iterable lastWhile(bool Function(E element) predicate) { var list = ListQueue(); for (var element in reversed) { if (!predicate(element)) break; @@ -493,46 +493,48 @@ extension IterableX on Iterable { } /// Returns all elements matching the given [predicate]. - Iterable filter(bool predicate(E element)) => where(predicate); + Iterable filter(bool Function(E element) predicate) => where(predicate); /// Returns all elements that satisfy the given [predicate]. - Iterable filterIndexed(bool predicate(E element, int index)) => + Iterable filterIndexed(bool Function(E element, int index) predicate) => whereIndexed(predicate); /// Appends all elements matching the given [predicate] to the given /// [destination]. - void filterTo(List destination, bool predicate(E element)) => + void filterTo(List destination, bool Function(E element) predicate) => whereTo(destination, predicate); /// Appends all elements matching the given [predicate] to the given /// [destination]. void filterIndexedTo( - List destination, bool predicate(E element, int index)) => + List destination, bool Function(E element, int index) predicate) => whereIndexedTo(destination, predicate); /// Returns all elements not matching the given [predicate]. - Iterable filterNot(bool predicate(E element)) => whereNot(predicate); + Iterable filterNot(bool Function(E element) predicate) => + whereNot(predicate); /// Returns all elements not matching the given [predicate]. - Iterable filterNotIndexed(bool predicate(E element, int index)) => + Iterable filterNotIndexed(bool Function(E element, int index) predicate) => whereNotIndexed(predicate); /// Appends all elements not matching the given [predicate] to the given /// [destination]. - void filterNotTo(List destination, bool predicate(E element)) => + void filterNotTo(List destination, bool Function(E element) predicate) => whereNotTo(destination, predicate); /// Appends all elements not matching the given [predicate] to the given /// [destination]. void filterNotToIndexed( - List destination, bool predicate(E element, int index)) => + List destination, bool Function(E element, int index) predicate) => whereNotToIndexed(destination, predicate); /// Returns a new lazy [Iterable] with all elements which are not null. Iterable filterNotNull() => where((element) => element != null); /// Returns all elements that satisfy the given [predicate]. - Iterable whereIndexed(bool predicate(E element, int index)) sync* { + Iterable whereIndexed( + bool Function(E element, int index) predicate) sync* { var index = 0; for (var element in this) { if (predicate(element, index++)) { @@ -543,7 +545,7 @@ extension IterableX on Iterable { /// Appends all elements matching the given [predicate] to the given /// [destination]. - void whereTo(List destination, bool predicate(E element)) { + void whereTo(List destination, bool Function(E element) predicate) { for (var element in this) { if (predicate(element)) { destination.add(element); @@ -554,7 +556,7 @@ extension IterableX on Iterable { /// Appends all elements matching the given [predicate] to the given /// [destination]. void whereIndexedTo( - List destination, bool predicate(E element, int index)) { + List destination, bool Function(E element, int index) predicate) { var index = 0; for (var element in this) { if (predicate(element, index++)) { @@ -564,7 +566,7 @@ extension IterableX on Iterable { } /// Returns all elements not matching the given [predicate]. - Iterable whereNot(bool predicate(E element)) sync* { + Iterable whereNot(bool Function(E element) predicate) sync* { for (var element in this) { if (!predicate(element)) { yield element; @@ -573,7 +575,8 @@ extension IterableX on Iterable { } /// Returns all elements not matching the given [predicate]. - Iterable whereNotIndexed(bool predicate(E element, int index)) sync* { + Iterable whereNotIndexed( + bool Function(E element, int index) predicate) sync* { var index = 0; for (var element in this) { if (!predicate(element, index++)) { @@ -584,7 +587,7 @@ extension IterableX on Iterable { /// Appends all elements not matching the given [predicate] to the given /// [destination]. - void whereNotTo(List destination, bool predicate(E element)) { + void whereNotTo(List destination, bool Function(E element) predicate) { for (var element in this) { if (!predicate(element)) { destination.add(element); @@ -595,7 +598,7 @@ extension IterableX on Iterable { /// Appends all elements not matching the given [predicate] to the given /// [destination]. void whereNotToIndexed( - List destination, bool predicate(E element, int index)) { + List destination, bool Function(E element, int index) predicate) { var index = 0; for (var element in this) { if (!predicate(element, index++)) { @@ -610,7 +613,7 @@ extension IterableX on Iterable { /// Returns a new lazy [Iterable] containing only the non-null results of /// applying the given [transform] function to each element in the original /// collection. - Iterable mapNotNull(R transform(E element)) sync* { + Iterable mapNotNull(R Function(E element) transform) sync* { for (var element in this) { var result = transform(element); if (result != null) { @@ -644,7 +647,7 @@ extension IterableX on Iterable { /// Returns a new lazy [Iterable] which performs the given action on each /// element. - Iterable onEach(void action(E element)) sync* { + Iterable onEach(void Function(E element) action) sync* { for (var element in this) { action(element); yield element; @@ -670,7 +673,7 @@ extension IterableX on Iterable { /// /// The elements in the resulting list are in the same order as they were in /// the source collection. - Iterable distinctBy(R selector(E element)) sync* { + Iterable distinctBy(R Function(E element) selector) sync* { var existing = HashSet(); for (var current in this) { if (existing.add(selector(current))) { @@ -722,7 +725,8 @@ extension IterableX on Iterable { Iterable> chunkWhile(bool Function(E, E) predicate) sync* { var currentChunk = []; var hasPrevious = false; - /*late*/ E previous; + /*late*/ + E previous; for (final element in this) { if (!hasPrevious || predicate(previous, element)) { @@ -817,7 +821,7 @@ extension IterableX on Iterable { /// Returns a new lazy [Iterable] of all elements yielded from results of /// [transform] function being invoked on each element of this collection. - Iterable flatMap(Iterable transform(E element)) sync* { + Iterable flatMap(Iterable Function(E element) transform) sync* { for (var current in this) { yield* transform(current); } @@ -996,7 +1000,7 @@ extension IterableX on Iterable { /// /// If any of two pairs would have the same key the last one gets added to the /// map. - Map associate(MapEntry transform(E element)) { + Map associate(MapEntry Function(E element) transform) { var map = {}; for (var element in this) { var entry = transform(element); @@ -1010,7 +1014,7 @@ extension IterableX on Iterable { /// /// If any two elements would have the same key returned by [keySelector] the /// last one gets added to the map. - Map associateBy(K keySelector(E element)) { + Map associateBy(K Function(E element) keySelector) { var map = {}; for (var current in this) { map[keySelector(current)] = current; @@ -1023,7 +1027,7 @@ extension IterableX on Iterable { /// /// If any of elements (-> keys) would be the same the last one gets added /// to the map. - Map associateWith(V valueSelector(E element)) { + Map associateWith(V Function(E element) valueSelector) { var map = {}; for (var current in this) { map[current] = valueSelector(current); @@ -1038,7 +1042,7 @@ extension IterableX on Iterable { /// /// The returned map preserves the entry iteration order of the keys produced /// from the original collection. - Map> groupBy(K keySelector(E element)) { + Map> groupBy(K Function(E element) keySelector) { return _groupBy(this, keySelector); } @@ -1047,7 +1051,7 @@ extension IterableX on Iterable { /// The first list contains elements for which [predicate] yielded true, /// while the second list contains elements for which [predicate] yielded /// false. - List> partition(bool predicate(E element)) { + List> partition(bool Function(E element) predicate) { var t = []; var f = []; for (var element in this) { @@ -1145,8 +1149,8 @@ extension IterableFutureX on Iterable> { /// Each future provides either a data event or an error event, /// depending on how the future completes. /// - /// If some futures have already completed when `Stream.fromFutures` is called, - /// their results will be emitted in some unspecified order. + /// If some futures have already completed when `Stream.fromFutures` is + /// called, their results will be emitted in some unspecified order. /// /// When all futures have completed, the stream is closed. Stream asStreamAwaited() => Stream.fromFutures(this); diff --git a/lib/src/list.dart b/lib/src/list.dart index 9c7112c..db6c76e 100644 --- a/lib/src/list.dart +++ b/lib/src/list.dart @@ -46,7 +46,7 @@ extension ListX on List { /// Returns a new list containing all elements except last elements that /// satisfy the given [predicate]. - List dropWhile(bool predicate(E element)) { + List dropWhile(bool Function(E element) predicate) { int startIndex; for (var i = 0; i < length; i++) { if (!predicate(this[i])) { @@ -73,7 +73,7 @@ extension ListX on List { /// Returns a new list containing all elements except last elements that /// satisfy the given [predicate]. - List dropLastWhile(bool predicate(E element)) { + List dropLastWhile(bool Function(E element) predicate) { int endIndex; for (var i = lastIndex; i >= 0; i--) { if (!predicate(this[i])) { @@ -96,7 +96,7 @@ extension ListX on List { /// [CastError]. /// /// Returns [length] if all the items in this list compare less than [value]. - int lowerBound(E value, {int compare(E a, E b)}) { + int lowerBound(E value, {int Function(E a, E b) compare}) { return _lowerBound(this, value, compare: compare); } @@ -110,7 +110,7 @@ extension ListX on List { /// [CastError]. /// /// Returns -1 if [value] is not in the list by default. - int binarySearch(E value, {int compare(E a, E b)}) { + int binarySearch(E value, {int Function(E a, E b) compare}) { return _binarySearch(this, value, compare: compare); } @@ -160,10 +160,10 @@ extension ListX on List { /// var list = [1, 2, 3, 4]; /// list.swap(0, 2); // [3, 2, 1, 4] /// ``` - void swap(int i, int j) { - final temp = this[i]; - this[i] = this[j]; - this[j] = temp; + void swap(int indexA, int indexB) { + final temp = this[indexA]; + this[indexA] = this[indexB]; + this[indexB] = temp; } } diff --git a/lib/src/sorted_list.dart b/lib/src/sorted_list.dart index 8318d31..e68c6da 100644 --- a/lib/src/sorted_list.dart +++ b/lib/src/sorted_list.dart @@ -1,6 +1,7 @@ part of dartx; -Comparator _getComparator(int order, Comparable selector(E element), +Comparator _getComparator( + int order, Comparable Function(E element) selector, {Comparator parent}) { final newComparator = (E a, E b) { return order * selector(a).compareTo(selector(b)); @@ -20,7 +21,7 @@ class _SortedList extends _DelegatingList { _SortedList._withSelector( this._source, - Comparable selector(E element), + Comparable Function(E element) selector, int order, Comparator parentComparator, ) : _comparator = _getComparator(order, selector, parent: parentComparator); @@ -40,7 +41,7 @@ class _SortedList extends _DelegatingList { /// /// **Note:** The actual sorting is performed when an element is accessed for /// the first time. - _SortedList thenBy(Comparable selector(E element)) { + _SortedList thenBy(Comparable Function(E element) selector) { return _SortedList._withSelector(this, selector, 1, _comparator); } @@ -50,7 +51,7 @@ class _SortedList extends _DelegatingList { /// /// **Note:** The actual sorting is performed when an element is accessed for /// the first time. - _SortedList thenByDescending(Comparable selector(E element)) { + _SortedList thenByDescending(Comparable Function(E element) selector) { return _SortedList._withSelector(this, selector, -1, _comparator); } @@ -129,7 +130,7 @@ abstract class _DelegatingList extends _DelegatingIterable int indexOf(E element, [int start = 0]) => delegate.indexOf(element, start); @override - int indexWhere(bool test(E element), [int start = 0]) => + int indexWhere(bool Function(E element) test, [int start = 0]) => delegate.indexWhere(test, start); @override @@ -150,7 +151,7 @@ abstract class _DelegatingList extends _DelegatingIterable delegate.lastIndexOf(element, start); @override - int lastIndexWhere(bool test(E element), [int start]) => + int lastIndexWhere(bool Function(E element) test, [int start]) => delegate.lastIndexWhere(test, start); @override @@ -171,14 +172,14 @@ abstract class _DelegatingList extends _DelegatingIterable void removeRange(int start, int end) => delegate.removeRange(start, end); @override - void removeWhere(bool test(E element)) => delegate.removeWhere(test); + void removeWhere(bool Function(E element) test) => delegate.removeWhere(test); @override void replaceRange(int start, int end, Iterable iterable) => delegate.replaceRange(start, end, iterable); @override - void retainWhere(bool test(E element)) => delegate.retainWhere(test); + void retainWhere(bool Function(E element) test) => delegate.retainWhere(test); @override Iterable get reversed => delegate.reversed; @@ -196,7 +197,7 @@ abstract class _DelegatingList extends _DelegatingIterable void shuffle([Random random]) => delegate.shuffle(random); @override - void sort([int compare(E a, E b)]) => delegate.sort(compare); + void sort([int Function(E a, E b) compare]) => delegate.sort(compare); @override List sublist(int start, [int end]) => delegate.sublist(start, end); @@ -220,7 +221,7 @@ abstract class _DelegatingIterable implements Iterable { Iterable get delegate; @override - bool any(bool test(E element)) => delegate.any(test); + bool any(bool Function(E element) test) => delegate.any(test); @override Iterable cast() => delegate.cast(); @@ -232,27 +233,28 @@ abstract class _DelegatingIterable implements Iterable { E elementAt(int index) => delegate.elementAt(index); @override - bool every(bool test(E element)) => delegate.every(test); + bool every(bool Function(E element) test) => delegate.every(test); @override - Iterable expand(Iterable f(E element)) => delegate.expand(f); + Iterable expand(Iterable Function(E element) f) => + delegate.expand(f); @override E get first => delegate.first; @override - E firstWhere(bool test(E element), {E orElse()}) => + E firstWhere(bool Function(E element) test, {E Function() orElse}) => delegate.firstWhere(test, orElse: orElse); @override - T fold(T initialValue, T combine(T previousValue, E element)) => + T fold(T initialValue, T Function(T previousValue, E element) combine) => delegate.fold(initialValue, combine); @override Iterable followedBy(Iterable other) => delegate.followedBy(other); @override - void forEach(void f(E element)) => delegate.forEach(f); + void forEach(void Function(E element) f) => delegate.forEach(f); @override bool get isEmpty => delegate.isEmpty; @@ -270,36 +272,38 @@ abstract class _DelegatingIterable implements Iterable { E get last => delegate.last; @override - E lastWhere(bool test(E element), {E orElse()}) => + E lastWhere(bool Function(E element) test, {E Function() orElse}) => delegate.lastWhere(test, orElse: orElse); @override int get length => delegate.length; @override - Iterable map(T f(E e)) => delegate.map(f); + Iterable map(T Function(E e) f) => delegate.map(f); @override - E reduce(E combine(E value, E element)) => delegate.reduce(combine); + E reduce(E Function(E value, E element) combine) => delegate.reduce(combine); @override E get single => delegate.single; @override - E singleWhere(bool test(E element), {E orElse()}) => + E singleWhere(bool Function(E element) test, {E Function() orElse}) => delegate.singleWhere(test, orElse: orElse); @override Iterable skip(int n) => delegate.skip(n); @override - Iterable skipWhile(bool test(E value)) => delegate.skipWhile(test); + Iterable skipWhile(bool Function(E value) test) => + delegate.skipWhile(test); @override Iterable take(int n) => delegate.take(n); @override - Iterable takeWhile(bool test(E value)) => delegate.takeWhile(test); + Iterable takeWhile(bool Function(E value) test) => + delegate.takeWhile(test); @override List toList({bool growable = true}) => delegate.toList(growable: growable); @@ -308,7 +312,7 @@ abstract class _DelegatingIterable implements Iterable { Set toSet() => delegate.toSet(); @override - Iterable where(bool test(E element)) => delegate.where(test); + Iterable where(bool Function(E element) test) => delegate.where(test); @override Iterable whereType() => delegate.whereType(); diff --git a/lib/src/string.dart b/lib/src/string.dart index d82db70..7424650 100644 --- a/lib/src/string.dart +++ b/lib/src/string.dart @@ -11,7 +11,7 @@ extension StringX on String { /// /// Please use [StringCharacters].characters /// https://github.com/dart-lang/characters/blob/10527437926f1b454edf9912fe700aa2506b1c3d/lib/src/extensions.dart#L9 - @Deprecated("Use .characters from the official characters package") + @Deprecated('Use .characters from the official characters package') Iterable get chars => Characters(this); /// Returns a copy of this string having its first letter uppercased, or the @@ -148,10 +148,10 @@ extension StringX on String { return true; } - return this.isEmpty; + return isEmpty; } - bool get isNotNullOrEmpty => !this.isNullOrEmpty; + bool get isNotNullOrEmpty => !isNullOrEmpty; /// If this [String] starts with the given [prefix], returns a copy of this /// string with the prefix removed. Otherwise, returns this [String]. @@ -176,7 +176,7 @@ extension StringX on String { /// Removes from a [String] both the given [prefix] and [suffix] if and only /// if it starts with the [prefix] and ends with the [suffix]. /// Otherwise returns this [String] unchanged. - String removeSurrounding({String prefix, String suffix}) { + String removeSurrounding({@required String prefix, @required String suffix}) { assert(prefix != null); assert(suffix != null); if (startsWith(prefix) && endsWith(suffix)) { diff --git a/pubspec.yaml b/pubspec.yaml index 8988936..c4f5f0e 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: dartx description: Superpowers for Dart. Collection of useful static extension methods. -version: 0.4.2+1 +version: 0.5.0 homepage: https://github.com/leisim/dartx environment: @@ -12,6 +12,7 @@ dependencies: crypto: ^2.1.0 characters: ">=0.4.0 <2.0.0" time: ^1.2.0 + meta: ^1.1.6 dev_dependencies: test: ^1.9.2 diff --git a/test/function_test.dart b/test/function_test.dart index 41e926d..7a714b6 100644 --- a/test/function_test.dart +++ b/test/function_test.dart @@ -7,6 +7,7 @@ void main() { final func = () => 5; test('.invoke', () { + // ignore: deprecated_member_use_from_same_package expect(func.invoke(), 5); }); }); diff --git a/test/iterable_test.dart b/test/iterable_test.dart index 03f85da..0c43642 100644 --- a/test/iterable_test.dart +++ b/test/iterable_test.dart @@ -889,6 +889,18 @@ void main() { ); }); + test("groupBy", () { + expect( + ['foo', 'bar', 'baz', 'bop', 'qux'] + .groupBy((dynamic string) => string[1]), + equals({ + 'o': ['foo', 'bop'], + 'a': ['bar', 'baz'], + 'u': ['qux'] + }), + ); + }); + test('.partition()', () { expect([].partition((it) => false), >[[], []]); expect([1, 2, 3, 4, 5, 6].partition((it) => it % 2 == 1), [ diff --git a/test/num_test.dart b/test/num_test.dart index 3f51969..4cca1c9 100644 --- a/test/num_test.dart +++ b/test/num_test.dart @@ -1,3 +1,5 @@ +import 'dart:typed_data'; + import 'package:test/test.dart'; import 'package:dartx/dartx.dart'; @@ -69,4 +71,22 @@ void main() { expect(11.0.between(10, 0), isFalse); }); }); + + group('IntX', () { + test('toBytes', () { + expect(123456789.toBytes(), + Uint8List.fromList([0, 0, 0, 0, 7, 91, 205, 21])); + expect(123456789.toBytes(Endian.little), + Uint8List.fromList([21, 205, 91, 7, 0, 0, 0, 0])); + }); + }); + + group('DoubleX', () { + test('toBytes', () { + expect(123.456.toBytes(), + Uint8List.fromList([64, 94, 221, 47, 26, 159, 190, 119])); + expect(123.455.toBytes(Endian.little), + Uint8List.fromList([133, 235, 81, 184, 30, 221, 94, 64])); + }); + }); } diff --git a/test/string_test.dart b/test/string_test.dart index 9ad96eb..1e79224 100644 --- a/test/string_test.dart +++ b/test/string_test.dart @@ -6,8 +6,10 @@ import 'package:test/test.dart'; void main() { group('StringX', () { test('.chars', () { + // ignore: deprecated_member_use_from_same_package expect('test12'.chars, ['t', 'e', 's', 't', '1', '2']); expect('test12'.characters, ['t', 'e', 's', 't', '1', '2']); + // ignore: deprecated_member_use_from_same_package expect('ഐ⌛酪Б👨‍👨‍👧‍👦'.chars, ['ഐ', '⌛', '酪', 'Б', '👨‍👨‍👧‍👦']); expect('ഐ⌛酪Б👨‍👨‍👧‍👦'.characters, ['ഐ', '⌛', '酪', 'Б', '👨‍👨‍👧‍👦']); });