.. | ||
example | ||
lib | ||
test | ||
.gitignore | ||
analysis_options.yaml | ||
CHANGELOG.md | ||
LICENSE.md | ||
pubspec.yaml | ||
README.md |
Platform Collections
A Dart implementation of Laravel-inspired collections, providing a fluent, convenient wrapper for working with arrays of data.
Features
- Chainable methods for manipulating collections of data
- Type-safe operations
- Null-safe implementation
- Inspired by Laravel's collection methods
Getting started
Add this package to your pubspec.yaml
:
dependencies:
platform_collections: ^1.0.0
Then run dart pub get
or flutter pub get
to install the package.
Usage
Here's a simple example of how to use the Collection
class:
import 'package:platform_collections/platform_collections.dart';
void main() {
final numbers = Collection([1, 2, 3, 4, 5]);
// Using various collection methods
final result = numbers
.whereCustom((n) => n % 2 == 0)
.mapCustom((n) => n * 2)
.toList();
print(result); // [4, 8]
// Chaining methods
final sum = numbers
.whereCustom((n) => n > 2)
.fold(0, (prev, curr) => prev + curr);
print(sum); // 12
}
Available Methods
all()
: Returns all items in the collectionavg()
: Calculates the average of the collectionchunk()
: Chunks the collection into smaller collectionscollapse()
: Collapses a collection of arrays into a single collectionconcat()
: Concatenates the given array or collectioncontains()
: Determines if the collection contains a given itemcount()
: Returns the total number of items in the collectioneach()
: Iterates over the items in the collectioneveryNth()
: Creates a new collection consisting of every n-th elementexcept()
: Returns all items except for those with the specified keysfilter()
/whereCustom()
: Filters the collection using a callbackfirst()
/firstWhere()
: Returns the first element that passes the given truth testflatten()
: Flattens a multi-dimensional collectionflip()
: Flips the items in the collectionfold()
: Reduces the collection to a single valuegroupBy()
: Groups the collection's items by a given keyjoin()
: Joins the items in a collectionlast()
/lastOrNull()
: Returns the last element in the collectionmap()
/mapCustom()
: Runs a map over each of the itemsmapSpread()
: Runs a map over each nested chunk of itemsmax()
: Returns the maximum value in the collectionmerge()
: Merges the given array into the collectionmin()
: Returns the minimum value in the collectiononly()
: Returns only the items from the collection with the specified keyspluck()
: Retrieves all of the collection values for a given keyrandom()
: Returns a random item from the collectionreverse()
: Reverses the order of the collection's itemssearch()
: Searches the collection for a given valueshuffle()
: Shuffles the items in the collectionslice()
: Returns a slice of the collectionsort()
/sortCustom()
: Sorts the collectiontake()
: Takes the first or last {n} items
Additional Information
For more detailed examples, please refer to the example/collections_example.dart
file in the package.
If you encounter any issues or have feature requests, please file them on the issue tracker.
Contributions are welcome! Please read our contributing guidelines before submitting a pull request.