immutable-extras

1.6.3 • Public • Published

immutable-extras

Simple extensions for the Immutable.js library

Install

npm install --save immutable-extras
yarn add immutable-extras

Usage

import Immutable from 'immutable';
import 'immutable-extras';
 
const someList = Immutable.List([
  Immutable.Map({
    id: 5,
    name: 'a'
  }),
  Immutable.Map({
    id: 15,
    name: 'b'
  }),
  Immutable.Map({
    id: 5,
    name: 'c'
  }),
  Immutable.Map({
    id: 5,
    name: 'a'
  }),
]);
 
const uniqueList = someList.unique('id');
// uniqueList ->
//    [ { id: 5, name: 'a' }, { id: 15, name: 'b' } ]
 
const mapped = someList.mapOn(v => v.name);
// mapped ->
//    {
//      a: { id: 5, name: 'a' },
//      b: { id: 15, name: 'b' },
//      c: { id: 5, name: 'c' },
//    }
 
const sortedList = Immutable.List([ 'bd', 'ab', 'c', 'bb']).multiSort(
  (a, b) => b.length - a.length,
  (a, b) => a.localeCompare(b),
)
// sortedList ->
//    ["ab", "bb", "bd", "c"]
 
const groupedList = Immutable.List([ '1', '2', '3', '4' ]).group(2)
// groupedList ->
//    [ [ '1', '2' ], [ '3', '4' ]]
 
const safeSetMap = Immutable.fromJS({ a: undefined }).safeSetIn({ a: { b: 'c' }})
// safeSetMap ->
//   {
//     a: {
//       b: 'c'
//     }
//   }
 
class TestClass {}
const classList1 = Immutable.List([ { a: 1 } ]).class(TestClass);
const classList2 = Immutable.List.of({ a: 1 }).class(TestClass);
// classList1 ->
//   Immutable.List([ TestClass(...) ])
// classList2 ->
//   Immutable.List([ TestClass(...) ])

Package Sidebar

Install

npm i immutable-extras

Weekly Downloads

1

Version

1.6.3

License

MIT

Unpacked Size

14.8 kB

Total Files

18

Last publish

Collaborators

  • alexrass