immutable-extras
v1.6.3
Published
Some extensions on top of immutable.js
Downloads
19
Maintainers
Readme
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(...) ])