ImmutableJS overview
Immutable library can be useful but documentation is quite tricky to understand. I have just played a little with this library and wanted to share with you some examples.
Written with TypeScript I guess it is why documentation is quite difficult to read first.
API
Documentation use a very generic template syntax.
Generic constructors
Ok, it is quite simple, T (for Type) K (for Key) V (for Value) are generic type declaration which will be used by immutable implementation.
Following commonly notation, keep it mind the following type parameter names:
- K - Key
- V - Value
- T - Type
- number - Number
- … maybe more
Generic methods
Function signature follow this convention:
What I just want to say is that you work on immutable data, so always use your returned value, otherwise…
parameters in documentation are often named for more readability
ES6
Then Immutable embrace ES6 with Classes, Arrow Functions and Modules so it won’t help you to read documentation if you do not know anything about it.
But I am nice, I give you some details :)
Arrow Function
Arrow Function sample extracted from Immutable documentation:
Ok, but let’s see an exemple on an Immutable Map.update() object method :
OMG, do not worry, difficult part to read is the parameter in parenthesis.
Reminder
is equivalent to
As explained before key
,updater
keywords are just there for readability, you do not write it when coding.
Firt param is your map key, second one use an Arrow Function, see it in action:
Iterable
Iterating over your collection of data, you might all know what Iterable and Iterator mean.
All collections in Immutable are extending Iterable following common (key, value) pattern.
Example with native Immutable Map
Example with native javascript Array []
Purpose of this example is to get an Iterable obj from a native javascript array.
Then play with all methods available on it
in practice
Behind it looks like Seq is used.
KeyPath
What is it ?
A keypath seach for you data according to a path represented by a sum of keys.
“Returns the value found by following a path of keys or indices through nested Iterables.”
Signature
Example
Many functions can be used as setIn(), deleteIn(), updateIn(), mergeIn()….
You can also edit this article by pressing edit button.
Tags: ImmutableJS Javascript