Noncollinear Perpendicular Microcrystalline
Need private packages and team management tools?Check out npm Teams »

@supercharge/collections

1.13.0 • Public • Published


Collections

async/await-ready array & collection utilities for Node.js


Installation · Docs · Usage



Latest Version Monthly downloads

Follow @marcuspoehls and @superchargejs for updates!


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
  })
  .all()
 
// users = [ <list of newly-subscribed users> ]

Notice: when chaining methods like map or filter, you'll receive a collection instance in return. You must actively end the call chain using the .all() method to process the collection pipeline and retrieve the final result.

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)
 
// result: 600

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

Install

npm i @supercharge/collections

DownloadsWeekly Downloads

181

Version

1.13.0

License

MIT

Unpacked Size

45.8 kB

Total Files

7

Last publish

Collaborators

  • avatar
  • avatar
  • avatar