@cheesebit/partition
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

@cheesebit/partition

Coverage Status npm package

Creates an object splitting given collection into groups, based on the return of the given predicate when invoked with each collection's item.

Instalation

You can add this library as a dependency by running npm install @cheesebit/partition or yarn add @cheesebit/partition.

Usage guide

  import partition from '@cheesebit/partition';

  const { supplies, tools, toys } = partition([{
    name: 'pen',
    type: 'supply'
  }, {
    name: 'lamp',
    usage: 'maintenance'
  }, {
    name: 'Buzz lightyear',
    type: 'toy'
  }, {
    name: 'pliers',
    type: 'tool'
  }], item => {
    if (item.usage === 'maintenance' || item.type === 'tool') {
      return 'tools'
    }

    if (item.type === 'toy') {
      return 'toys'
    }

    return 'supplies'
  })

  console.log(supplies); // [{ name: 'pen', type: 'supply' }]
  console.log(tools); // [{ name: 'lamp', usage: 'maintenance' }, { name: 'pliers', type: 'tool' }]
  console.log(toys); // [{ name: 'Buzz lightyear', type: 'toy' }]

Package Sidebar

Install

npm i @cheesebit/partition

Weekly Downloads

1

Version

1.1.0

License

MIT

Unpacked Size

5.87 kB

Total Files

8

Last publish

Collaborators

  • welingtonms