Installation
npm i @supercharge/collections
Docs
Find all the details and available methods in the extensive Supercharge docs.
Usage
The package exports a function accepting an array as a parameter. From there, you can chain all collection methods.
The package is async/await-ready and supports async functions for most of the methods.
const User = require('models/user')
const Collect = require('@supercharge/collections')
const users = await Collect(
await User.all()
)
.filter(async user => {
return user.notSubscribedToNewsletter()
})
.map(async user => {
await user.subscribeToNewsletter()
return user
})
You can directly await the result for methods returning a definite value. The function returns a new instance of the collection without altering the original input array:
await Collect([ 1, 2, 3 ])
.map(item => item * 100)
.reduce((carry, item) => {
return carry + item
}, 0)
Contributing
Do you miss a collection function? We very much appreciate your contribution! Please send in a pull request 😊
- Create a fork
- Create your feature branch:
git checkout -b my-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request 🚀
License
MIT © Supercharge
superchargejs.com ·
GitHub @supercharge ·
Twitter @superchargejs