@cheesebit/partition

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

Usage no npm install needed!

<script type="module">
  import cheesebitPartition from 'https://cdn.skypack.dev/@cheesebit/partition';
</script>

README

@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' }]