From 92133b2e285c4ec5c5fb7e71a1b569e784183efc Mon Sep 17 00:00:00 2001 From: Hans Kokx Date: Mon, 4 Nov 2024 17:00:50 +0100 Subject: [PATCH] v1.1.4 - Added `unique` extension for Lists Signed-off-by: Hans Kokx --- CHANGELOG.md | 4 ++++ README.md | 20 ++++++++++++++++++++ lib/arcane_helper_utils.dart | 1 + lib/src/extensions/list.dart | 8 ++++++++ pubspec.yaml | 2 +- 5 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 lib/src/extensions/list.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index 74da327..68f93df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 1.1.4 + +- Added the `unique` extension for `List` objects. + ## 1.1.3 - Removes `Color` extension due to an incompatibility with Flutter. Sorry, @rania-run! diff --git a/README.md b/README.md index 8461253..336b25f 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ providing utility functions and extensions that simplify common tasks. class, making it easier to format dates and calculate differences. - **String Extensions**: Enhances the `String` class by adding new methods for common transformations and checks, including JWT parsing. +- **List Extensions**: Adds a new `unique` operator for filtering `List` items. ## Getting Started @@ -330,6 +331,25 @@ objects: Additionally, the `CommonString` class provides a quick shortcut to common strings, such as punctuation marks that are otherwise cumbersome to find or type. +### List Extensions + +The following extensions have been added to the `List` object: + +- `unique([Id Function(E element)? id, bool inplace = true])`: Filters a list + by a given element, returning only non-duplicate values. Can return either a + new `List` or filter the existing list by specifying the `inplace` option. + + ```dart + final List myList = [ + Item(value: "Hello"), + Item(value: "Hello"), + Item(value: "World"), + Item(value: "World"), + ]; + + myList.unique((item) => item.value); + // [Item(value: "Hello"), Item(value: "World")] + ## Contributing Contributions are welcome! Feel free to fork the repository and submit pull diff --git a/lib/arcane_helper_utils.dart b/lib/arcane_helper_utils.dart index 68f8ffe..19110ad 100644 --- a/lib/arcane_helper_utils.dart +++ b/lib/arcane_helper_utils.dart @@ -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"; diff --git a/lib/src/extensions/list.dart b/lib/src/extensions/list.dart new file mode 100644 index 0000000..7b2b0a1 --- /dev/null +++ b/lib/src/extensions/list.dart @@ -0,0 +1,8 @@ +extension Unique on List { + List unique([Id Function(E element)? id, bool inplace = true]) { + final Set ids = {}; + final List list = inplace ? this : List.from(this); + list.retainWhere((x) => ids.add(id != null ? id(x) : x as Id)); + return list; + } +} diff --git a/pubspec.yaml b/pubspec.yaml index dcfb42c..369bedd 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,7 +1,7 @@ name: arcane_helper_utils description: Provides a variety of helpful utilities and extensions for Flutter and Dart. -version: 1.1.3 +version: 1.2.0 repository: https://github.com/hanskokx/arcane_helper_utils issue_tracker: https://github.com/hanskokx/arcane_helper_utils/issues