@supercharge/collections
TypeScript icon, indicating that this package has built-in type declarations

5.0.1 • Public • Published


Collections

async/await-ready array methods for Node.js


Installation · Docs · Usage



Latest Version Monthly downloads

Follow @marcuspoehls and @superchargejs for updates!


Introduction

The @supercharge/collections package provides a convenient wrapper to work with arrays.

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.

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('@supercharge/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('@supercharge/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 😊

  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


superchargejs.com  ·  GitHub @supercharge  ·  Twitter @superchargejs

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
5.0.1234latest

Version History

VersionDownloads (Last 7 Days)Published
5.0.1234
5.0.00
4.3.122
4.3.00
4.2.01
4.1.00
4.0.10
4.0.00
3.2.11
3.2.00
3.1.40
3.1.30
3.1.20
3.1.10
3.1.00
3.0.00
2.4.00
2.3.00
2.2.00
2.1.00
2.0.00
1.13.00
1.12.20
1.12.10
1.12.00
1.11.00
1.10.00
1.9.183
1.9.00
1.8.00
1.7.00
1.6.00
1.5.10
1.5.01
1.4.11
1.4.00
1.3.00
1.2.10
1.2.00
1.1.00
1.0.00

Package Sidebar

Install

npm i @supercharge/collections

Weekly Downloads

343

Version

5.0.1

License

MIT

Unpacked Size

104 kB

Total Files

15

Last publish

Collaborators

  • marcuspoehls
  • peitek
  • celsiusf