Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use destructuring assignment with immutable.js?

With standard JS objects, one can use destructuring assignment such as:

let obj = {name: 'james', code: '007'}
let {name, code} = obj // creates new variables 'name' and 'code' (with the proper values)

As suggested by some Flux / Redux evangelist, I use immutable.js for my app; can I use destructuring also on immutable List / Map? Of course, one could do:

let obj = immutable.fromJS({name: 'james', code: '007'})
let {name, code} = obj.toJS()

but this seems to have quite inefficient as the objects grow bigger (because the object needs to be deeply jsified first).

like image 920
Tomas Kulich Avatar asked Jan 13 '16 09:01

Tomas Kulich


People also ask

Is immutable JS still used?

Update on 12 Aug 2021 Happily, the creator of Immutable JS resumed to maintaining his lib, and commits are regular now. Hope 4.0. 0 comes soon!

Can we Destructure array in JavaScript?

To destructure an array in JavaScript, we use the square brackets [] to store the variable name which will be assigned to the name of the array storing the element.

Should I use immutable JS?

Using ImmutableJS can improve dramatically the performance of your application. And, because the immutable data never changes, you can always expect new data to be passed from the above. To make sure you are correctly comparing the data and not updating the UI when there is no change, you should always use the .

Can you Destructure a function in JavaScript?

The object destructuring is a useful JavaScript feature to extract properties from objects and bind them to variables. What's better, object destructuring can extract multiple properties in one statement, can access properties from nested objects, and can set a default value if the property doesn't exist.


1 Answers

With immutable List, the destructuring works quite straightforwardly. This is because destructuring of Arrays works on every iterable (Checking whether something is iterable) and is not subjected just to js Arrays.

With Map, the situation is more complicated. Unlike with List, the destructuring of Map-like structures IS subjected just to plain JS objects and nothing more. Currently, it does not seem that ES community considers this a good idea (see https://esdiscuss.org/topic/extensible-destructuring-proposal)

However, there exist babel-plugin that enables this: https://github.com/vacuumlabs/babel-plugin-extensible-destructuring Having this plugin installed and enabled in .babelrc, you can simply patch immutable Map to have a .@@get method defined:

// main.js, first file loaded
import {Iterable} from 'immutable';
Iterable.prototype[Symbol.for('get')] = function(value) {return this.get(value); };

and everything works (also nested destructuring, or destructuring with default values)

import {fromJS} from 'immutable';
const map = fromJS({author: {name: {first: "John", last: "Doe"}, birthdate: "10-10-2010"}});
const {author: {name: {first, last}, birthdate}} = map;

Disclaimer: I'm one of the authors of the plugin mentioned above.

like image 117
Tomas Kulich Avatar answered Oct 09 '22 22:10

Tomas Kulich