Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/supercharge/collections
async/await-ready, chainable Array & Collection utilities
https://github.com/supercharge/collections
array async-await async-functions asynchronous chainable-methods collections filter hacktoberfest iteration map promise promises reduce supercharge
Last synced: 2 months ago
JSON representation
async/await-ready, chainable Array & Collection utilities
- Host: GitHub
- URL: https://github.com/supercharge/collections
- Owner: supercharge
- License: mit
- Created: 2019-07-03T19:08:47.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2023-11-06T04:22:36.000Z (about 1 year ago)
- Last Synced: 2024-10-30T03:02:09.694Z (3 months ago)
- Topics: array, async-await, async-functions, asynchronous, chainable-methods, collections, filter, hacktoberfest, iteration, map, promise, promises, reduce, supercharge
- Language: TypeScript
- Homepage: https://superchargejs.com/docs/collections
- Size: 521 KB
- Stars: 34
- Watchers: 3
- Forks: 24
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
Collections
async/await-ready
array methods for Node.js
Installation ·
Docs ·
Usage
Follow @marcuspoehls and @superchargejs for updates!
---
## Notice
This package is ESM-only.## Introduction
The `@supercharge/collections` package provides a convenient, fully-async wrapper to work with arrays.## Installation
```
npm i @supercharge/collections
```## Docs
Find all the [details and available methods in the extensive Supercharge docs](https://superchargejs.com/docs/collections).## Usage
The package exports a function accepting an array as a parameter. From there, you can chain all collection methods.### SyncCollections by default
### Collections are Asynchronous
Collections are asynchronous by default. You can use `async` callback functions in all methods and need to `await` the result.In contrast to JavaScript’s array methods, collections have a lot more methods available making your code a lot simpler. Here are basic examples using a collection:
```js
import User from '../models/user'
import Collect from '@supercharge/collections'const users = await User.findAll()
const notSubscribedUsers = await Collect(users).filter(user => {
return user.notSubscribedToNewsletter
})// notSubscribedUsers = [ ]
// you can also chain further methods to the collection
const users = await User.findAll()const subscribedUsers = await Collect(users)
.filter(user => {
return user.notSubscribedToNewsletter
})
.map(async user => { // <-- providing an async callback creates an async collection that you need to `await`
await user.subscribeToNewsletter()return user
})// subscribedUsers = [ ]
```Here’s another example outlining how to determine whether the array “has” an item:
```js
// “has” in JS Arrays
const hasNotSubscribedUsers = !![].concat(users).find(user => {
return user.notSubscribedToNewsletter
})// “has” in Collections
const hasNotSubscribedUsers = Collect(users).has(async user => {
return await User.isSubcribedToNewsletter(user)
})
```All available methods are outlined in the [docs](https://superchargejs.com/docs/collections).
## Contributing
Do you miss a collection function? We very much appreciate your contribution! Please send in a pull request 😊1. Create a fork
2. Create your feature branch: `git checkout -b my-feature`
3. Commit your changes: `git commit -am 'Add some feature'`
4. Push to the branch: `git push origin my-new-feature`
5. Submit a pull request 🚀## License
MIT © [Supercharge](https://superchargejs.com)---
> [superchargejs.com](https://superchargejs.com) ·
> GitHub [@supercharge](https://github.com/supercharge) ·
> Twitter [@superchargejs](https://twitter.com/superchargejs)