Socket
Socket
Sign inDemoInstall

@supercharge/collections

Package Overview
Dependencies
2
Maintainers
3
Versions
41
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @supercharge/collections

Supercharge collections


Version published
Weekly downloads
418
increased by3.72%
Maintainers
3
Created
Weekly downloads
 

Changelog

Source

3.0.0 - 2020-09-03

Added

  • synchronous collections by default
    • all collections were async by default until versions 2.x, you had to await every collection pipeline
    • this changes with the release of version 3.0: all collections are sync by default
    • synchronous collections work like JavaScript’s array methods
    • you must explicitly call collection.all() to retrieve the resulting array from a synchronous collection
  • a collection becomes async as soon as you provide an async callback method to methods like map, filter, find, and so on:

Updated

  • bump dependencies
  • change main entrypoint in package.json to dist folder

Removed

  • remove index.js file which acted as a middleman to export from dist folder

Breaking Changes

  • collections are now synchronous by default, meaning you don’t need to await a collection if you’re not using an async callback
Sync Collections

Synchronous collections work like JavaScript’s Array methods. For example, the following pipeline stays a synchronous pipeline:

const Collect = require('@supercharge/collections')

const collection = Collect([1, 2, 3, 4, 5])
  .map(item => item * 2)
  .filter(item => item > 5)
  .sort((a, b) => b -a)

// on sync collections, you must explicitly call `.all()` to retrieve the result

const result = collection.all()
//  [10, 8, 6]
Async Collections

In comparison, a collection becomes async as soon as you provide an async callback method to methods like map, filter, find, and so on:

const Collect = require('@supercharge/collections')

const collection = Collect([1, 2, 3, 4, 5])
  .map(async item => item * 2) // the collection is async from here on
  .filter(item => item > 5)
  .sort((a, b) => b -a)

// async collections must be awaited

const result = await collection
//  [10, 8, 6]

Readme

Source


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

Keywords

FAQs

Last updated on 03 Sep 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc