- Added `unique` extension for Lists

Signed-off-by: Hans Kokx <hans.kokx@hackberry.se>
This commit is contained in:
Hans Kokx
2024-11-04 17:00:50 +01:00
parent 9f8d85f543
commit 92133b2e28
5 changed files with 34 additions and 1 deletions
+1
View File
@@ -1,6 +1,7 @@
library arcane_helper_utils;
export "package:arcane_helper_utils/src/extensions/date_time.dart";
export "package:arcane_helper_utils/src/extensions/list.dart";
export "package:arcane_helper_utils/src/extensions/string.dart";
export "package:arcane_helper_utils/src/extensions/string_jwt.dart";
export "package:arcane_helper_utils/src/utils/json_converter.dart";
+8
View File
@@ -0,0 +1,8 @@
extension Unique<E, Id> on List<E> {
List<E> unique([Id Function(E element)? id, bool inplace = true]) {
final Set ids = {};
final List<E> list = inplace ? this : List<E>.from(this);
list.retainWhere((x) => ids.add(id != null ? id(x) : x as Id));
return list;
}
}