kt_dart 0.7.0-dev.3 kt_dart: ^0.7.0-dev.3 copied to clipboard
This project is a port of kotlin-stdlib for Dart/Flutter projects. It includes collections (KtList, KtMap, KtSet) with 150+ methods as well as other useful packages.
0.7.0-dev.3 #
- Rename
(List|Set|Map).immutable()
extension to.toImmutableList()
to match Dart SDK naming schema. - Remove
int.rangeTo(X)
extension. Please use thedartx
as replacement which offers the same extension - Remove
T.to(X)
extension to create aKtPair
. It's too general and should be offered by the dart SDK not a 3rd party package
0.7.0-dev.2 #
New .dart
extensions to convert KtDart collections back to dart collections.
// New: Converting dart collections to KtDart collections (mutable views)
final KtMutableList<String> ktList = ["hey"].kt;
final KtMutableSet<String> ktSet = {"hey"}.kt;
final KtMutableMap<String, int> ktMap = {"hey": 1}.kt;
// Converting KtDart collections to dart collections
final List<String> dartList = KtList.of("hey").dart;
final Set<String> dartSet = KtSet.of("hey").dart;
final Map<String, int> dartMap = KtMap.from({"hey": 1}).dart;
0.7.0-dev.1 #
KtDart makes full use of darts static extension methods, introduced with Dart 2.6.
The public API stays unchanged and is backwards compatible.
Improved interopt with dart collections #
It is now easier then ever to convert dart to ktdart collections and vice versa. Use the .kt
property to convert dart collections to KtDart collections. (Note: .kt
create a view, which allows you to mutate the original dart collection).
// New: Make dart collections immutable
final KtList<String> list = ["hey"].immutable();
final KtSet<String> set = {"hey"}.immutable();
final KtMap<String, int> map = {"hey": 1}.immutable();
// New: Converting dart collections to KtDart collections (mutable views)
final KtMutableList<String> ktList = ["hey"].kt;
final KtMutableSet<String> ktSet = {"hey"}.kt;
final KtMutableMap<String, int> ktMap = {"hey": 1}.kt;
// Converting KtDart collections to dart collections
final List<String> dartList = KtList.of("hey").asList();
final Set<String> dartSet = KtSet.of("hey").asSet();
final Map<String, int> dartMap = KtMap.from({"hey": 1}).asMap();
Tuple improvements #
KtPair
s can now created with the T0.to(T1)
extension.
final KtPair<String, int> pair = "foo".to(42);
Also, KtPair
and KtTriple
now have a new toList()
function to convert the values to a KtList
.
New Extensions #
KtList?.orEmpty()
returns an empty list when the list isnull
KtSet?.orEmpty()
returns an empty set when the set isnull
KtMap?.orEmpty()
returns an empty map when the map isnull
KtMap.ifEmpty(() -> defaultValue)
returns the default value when the map is emptyKtIterable<KtIterable<T>>.flatten()
flattens the nested collections toKtIterable<T>
KtIterable<KtPair<T, U>>.unzip(): KtPair<KtList<T>, KtList<U>>
unzips list of pairs to list of their first and second valuesKtIterable<Comparable<T>>.min()
returns the smallest element of any comparable iterableKtIterable<Comparable<T>>.max()
returns the largest element of any comparable iterable
0.6.2 #
0.6.0 #
This major update of kt.dart add 10+ extension methods for KtMap
and makes working with maps even easier.
Behavior Changes #
The properties KtList.list: List
,KtSet.set: Set
are now deprecated and KtMap.map: Map
was removed. Those properties where used to convert kt.dart collections to dart collections.
Instead use the new KtList.asList(): List
, KtSet.asSet(): Set
, KtMa.asMap(): Map
methods.
The old properties returned copies of the collections.
The new as
-methods return views of the original collections and reflect changes of the original data.
This breaking change was necessary because the property KtMap.map: Map<K, V>
was conflicting with KtMap.map(MapEntry<K, V> -> R) : KtList<R>
to map the entries to items of a KtList
.
Read about further details here.
If you have used properties to iterate over the collections using a for-loop you should now always use iter
which is available for all kt.dart collections.
for (final element in listOf("a", "b", "c").iter) {
print(element);
}
for (final element in setOf("a", "b", "c").iter) {
print(element);
}
for (final p in mapFrom({1: "Bulbasaur", 2: "Ivysaur"}).iter) {
print("${p.key} -> ${p.value}");
}
Additions #
- #86 New:
KtMap.map
Returns a list containing the results of applying the giventransform
function to each entry in the original map. - #86 New:
KtMap.iter
Access to aIterable
to be used in for-loops - #87 New:
KtMap.count
Returns the number of entries matching the given [predicate] or the number of entries whenpredicate = null
. - #89 New:
KtMap.minBy
Returns the first entry yielding the smallest value of the given function ornull
if there are no entries. - #89 New:
KtMap.minWith
Returns the first entry having the smallest value according to the providedcomparator
ornull
if there are no entries. - #89 New:
KtMap.maxBy
Returns the first entry yielding the largest value of the given function ornull
if there are no entries. - #89 New:
KtMap.maxWith
Returns the first entry having the largest value according to the providedcomparator
ornull
if there are no entries. - #90 New:
KtMap.toList
Returns aKtList
containing all key-value pairs. - #78 New:
KtMap.forEach
Performs givenaction
on each key/value pair from this map. thanks @acherkashyn - #80 New:
KtMap.none
Returnstrue
if there is no entries in the map that match the givenpredicate
. thanks @acherkashyn - #80 New:
KtMap.all
Returns true if all entries match the givenpredicate
. thanks @acherkashyn - #80 New:
KtMap.any
Returns true if there is at least one entry that matches the givenpredicate
. thanks @acherkashyn - #84 New:
KtMap.filterKeys
Returns a map containing all key-value pairs with keys matching the givenpredicate
. - #84 New:
KtMap.filterValues
Returns a map containing all key-value pairs with values matching the givenpredicate
. - #79 New:
KtMap.asMap
Returns a read-only dart:coreMap
- #79 New:
KtMutableMap.asMap
Creates aMap
instance that wraps the originalKtMap
. It acts as a view.
- #75 New:
KtIterable.takeWhile
Returns a list containing first elements satisfying the givenpredicate
. - #76 New:
KtIterable.takeLastWhile
Returns a list containing last elements satisfying the givenpredicate
.
- #73 New:
KtList.takeLast
, Returns a list containing lastn
elements. - #79 New:
KtList.asList
Returns a read-only dart:coreList
- #79 New:
KtMutableList.asList
Creates aList
instance that wraps the originalKtList
. It acts as a view.
- #79, #91 New:
KtSet.asSet
Returns a read-only dart:coreSet
- #79 New:
KtMutableSet.asSet
Creates aSet
instance that wraps the originalKtSet
. It acts as a view.
Bugfixes #
Documentation #
Misc. #
0.5.0 #
Project has been renamed to kt.dart
. If you're using a previous version upgrade like this:
pubspec.yaml
dependencies:
- dart_kollection: ^0.3.0
- kotlin_dart: ^0.4.0
+ kt_dart: ^0.5.0
your_source.dart
- import 'package:dart_kollection/dart_kollection.dart';
- import 'package:kotlin_dart/kotlin.dart';
+ import 'package:kt_dart/kt.dart';
0.4.3 #
Deprecate package kotlin_dart
and recommend users to migrate to kt_dart
.
0.4.2 #
Shorten pub project description to make pana happy.
0.4.1 #
Improve Readme which renders correctly on pub.
0.4.0 #
The kollection
project was migrated to kotlin.dart
where kollection
becomes the collection
module.
Upgrade #
pubspec.yaml
dependencies:
- dart_kollection: ^0.3.0
+ kotlin_dart: ^0.4.0
your_source.dart
- import 'package:dart_kollection/dart_kollection.dart';
+ import 'package:kotlin_dart/kotlin.dart';
Breaking Changes #
- #64 The class prefix of all collections has been changed from
K
toKt
(KList
->KtList
) - #60
listOf
now accepts up to 10 non-null arguments instead of anIterable
. UselistFrom
to createKtList
s from an dartIterable
s - #60 Collections can now be created with factory constructors i.e.
KtList.of(1, 2 ,3)
. Both APIs, factory constructor and function based one, are equally supported. It only depends on your personal taste.
Here is a list of all collection creation APIs.
Kotlin like, function based syntax
/// List
// Create immutable lists
emptyList<int>();
listOf(1, 2, 3, 4, 5);
listFrom([1, 2, 3, 4, 5]);
// Create mutable lists
mutableListOf(1, 2, 3, 4, 5);
mutableListFrom([1, 2, 3, 4, 5]);
/// Set
// Create immutable sets
emptySet<int>();
setOf(1, 2, 3, 4, 5);
setFrom([1, 2, 3, 4, 5]);
// Create a mutable set which keeps the order of the items
linkedSetOf(1, 2, 3, 4, 5);
linkedSetFrom([1, 2, 3, 4, 5]);
// Create mutable, unordered hash-table based set
hashSetOf(1, 2, 3, 4, 5);
hashSetFrom([1, 2, 3, 4, 5]);
/// Map
// Create immutable maps
emptyMap<int, String>();
mapFrom({1: "a", 2: "b"});
// Create mutable maps
mutableMapFrom({1: "a", 2: "b"});
// Create mutable maps without specified order when iterating over items
hashMapFrom({1: "a", 2: "b"});
// Create mutable maps which keep the order of the items
linkedMapFrom({1: "a", 2: "b"});
Dart like, constructor based syntax
/// List
// Create immutable lists
KList<int>.empty();
KList.of(1, 2, 3, 4, 5);
KList.from([1, 2, 3, 4, 5]);
// Create mutable lists
KMutableList<int>.empty();
KMutableList.of(1, 2, 3, 4, 5);
KMutableList.from([1, 2, 3, 4, 5]);
/// Set
// Create immutable sets
KSet<int>.empty();
KSet.of(1, 2, 3, 4, 5);
KSet.from([1, 2, 3, 4, 5]);
// Create a mutable set which keeps the order of the items
KMutableSet<int>.empty();
KMutableSet.of(1, 2, 3, 4, 5);
KMutableSet.from([1, 2, 3, 4, 5]);
// Create mutable, unordered hash-table based set
KHashSet<int>.empty();
KHashSet.of(1, 2, 3, 4, 5);
KHashSet.from([1, 2, 3, 4, 5]);
// Create a mutable set which keeps the order of the items
KLinkedSet<int>.empty();
KLinkedSet.of(1, 2, 3, 4, 5);
KLinkedSet.from([1, 2, 3, 4, 5]);
/// Map
// Create mutable maps
KMutableMap<int, String>.empty();
KMutableMap.from({1: "a", 2: "b"});
// Create mutable maps without specified order when iterating over items
KHashMap<int, String>.empty();
KHashMap.from({1: "a", 2: "b"});
// Create mutable maps which keep the order of the items
KLinkedMap<int, String>.empty();
KLinkedMap.from({1: "a", 2: "b"});
0.3.2 #
Deprecate package dart_kollection
in favour of kt_dart
0.3.1 #
Deprecate all APIs and advise users to upgrade to kotlin.dart
0.3.0 #
Summary #
This release of Kollection fully covers the project with unit tests, from 52% to 99% 🎉. By doing that bugs where discovered and fixed.
Because Dart doesn't support non-nullable types yet, this update manually checks all method arguments at runtime.
Passing null
in any method will throw ArgumentError
unless documented otherwise.
Behavior changes #
- #36 All method arguments are now validated for nullability. If a argument isn't documented as "nullable" the method will throw
ArgumentError
(when asserts are enabled) - #51, #46
KIterable<T>.associateWithTo
,Kiterable<T>.filterTo
,KIterable<T>.filterIndexedTo
,KIterable<T>.filterNotTo
,KIterable<T>.filterNotNullTo
,KIterable<T>.groupByTo
,KMap<T>.mapKeysTo
,KMap<T>.mapValuesTo
,KIterable.toCollection
did not compile when called directly due to dart-lang/sdk/issues/35518. The type ofdestination
of those methods has been changed to a dynamic type (i.e.KMutableList<T>
->KMutableList<dynamic>
). Those methods will now be checked at runtime. This has one advantage: It allows to pass in contravariant types.
final KIterable<int> iterable = listOf([4, 25, -12, 10]);
final result = mutableListOf<num>(); // covariant!
final filtered = iterable.filterIndexedTo(result, (i, it) => it < 10);
expect(identical(result, filtered), isTrue);
expect(result, listOf([4, -12]));
- #56
KMutableEntry.setValue
now throwsUnimplementedError
because of bug #55. It anyways never worked. - #58
KSet
doesn't allow mutation of its elements with viaset
getter. It is now really immutable.
API changes #
-
#38 Breaking: Removed
hashMapFrom(KIterable<KPair>)
because, unlike Kotlin, it feels unnatural in Dart. Instead usehashMapOf
to construct aKMutableMap
-
#17 Breaking:
KMap.associateBy
now takes only a single parameter (K Function(T) keySelector
). If you usedvalueTransform
useKMap.associateByTransform
as replacement -
#23 New
KMutableList.[]=
operator. Example:list[4] = "Hello"
-
#47 New
KMap
methodsfilter
,filterTo
,filterNot
,filterNotTo
, -
#37
KCollection.random
now optionally accepts aRandom
as argument which can be seeded -
#39
KMutableList.removeAt
now throwsIndexOutOfBoundsException
whenindex
exceeds length or is negative -
#18
KMutableCollection
:addAll
,removeAll
andretainAll
now receiveKIterable
as parameter, wasKCollection
Bug fixes #
- #18 Fixed
KList.first
stackoverflow - #44 Fixed
Klist.single
stackoverflow - #24 Fixed
KList.last
which returnedfirst
- #20 Fixed
KIterable.firstOrNull
which threwNoSuchElementException
for empty lists, now returnsnull
- #22 Fixed
KIterable.mapIndexedTo
,KIterable.mapIndexedNotNullTo
couldn't be called due to a generic compilation error - #26 Fixed
KList.containsAll
returned false when all elements ar in list - #28 Fixed
KListIterator.nextIndex
was off by one, now returns the index of the element which will be returned bynext()
- #30 Fixed
KMutableList.sortBy
andsortByDescending
not sorting theKMutableList
but a copy - #31 Fixed
KIterable.none
always returnedtrue
(Was always working forKCollection
) - #51 Fixed
KSet.==()
returns false forsetOf<int>([1, 2, 3]) == setOf<num>([1, 2, 3])
Documentation changes #
- #57 Document
hashSetOf
andlinkedSetOf
- #19
KIterable.any
document return value when called withoutpredicate
- #51 Document expected type of now dynamically typed
KIterable<T>.associateWithTo
,Kiterable<T>.filterTo
,KIterable<T>.filterIndexedTo
,KIterable<T>.filterNotTo
,KIterable<T>.filterNotNullTo
,KIterable<T>.groupByTo
,KMap<T>.mapKeysTo
,KMap<T>.mapValuesTo
,KIterable.toCollection
Other changes #
0.2.0 #
Behavior change #
API changes #
- #1 Add
Set<T> get set
returning the internal dart set - #1 Add
Map<K, V> get map
returning the intenral dart set - #7 Add
KMap.toMap
andKMap.toMutableMap
- #8 Add
KMap.isNotEmpty
- 3e3228e Add
KMap.toString()
- #9 Add
Map.plus
,Map.minus
andoperator +(KMap<K, V> map)
,operator -(K key)
- #12 Remove const constructors from collection interfaces
- #13 Remove default implementations from collection interfaces
Documentation changes #
- #15 Add documentation for
compareBy
andcompareByDescending
Other changes #
0.1.0 #
Initial release for
KList
/KMutableList
KSet
/KMutableSet
KMap
/KMutableMap
with tons of extensions waiting for you to use them!