Package Exports
- @httpie/collections
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@httpie/collections) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Collections
async/await-ready array methods for Node.js
Installation · Docs · Usage
Follow @marcuspoehls and @httpiejs for updates!
Introduction
The @httpie/collections package provides a convenient wrapper to work with arrays.
Installation
npm i @httpie/collectionsDocs
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.
Sync Collections by default
A created collection (Collect([1, 2, 3])) is synchronous by default. That means it behaves like JavaScript’s array methods.
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:
const User = require('../models/user')
const Collect = require('@httpie/collections')
const users = await User.findAll()
const notSubscribedUsers = Collect(users)
.filter(user => {
return user.notSubscribedToNewsletter
})
.all()
// notSubscribedUsers = [ <list of not-yet-subscribed users> ]Here’s another example outlining how to determine whether the array “has” an item:
// “has” in JS Arrays
const hasNotSubscribedUsers = !![].concat(users).find(user => {
return user.notSubscribedToNewsletter
})
// “has” in Collections
const hasNotSubscribedUsers = Collect(users).has(user => {
return user.notSubscribedToNewsletter
})All available methods are outlined in the docs.
Async Collections
The package is async/await-ready and supports async callback functions. A collection becomes async (returns a promise) as soon as you provide an async callback method to methods like map, filter, find, and so on. You then need to await the collection pipeline:
const User = require('../models/user')
const Collect = require('@httpie/collections')
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 = [ <list of newly-subscribed users> ]You can directly await async collections without ending the call chain with .all(). You can still call .all() though, it works as well.
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
httpiejs.com · GitHub @httpie · Twitter @httpiejs