Book Image

Mastering Immutable.js

By : Adam Boduch
Book Image

Mastering Immutable.js

By: Adam Boduch

Overview of this book

Immutable.js is a JavaScript library that will improve the robustness and dependability of your larger JavaScript projects. All aspects of the Immutable.js framework are covered in this book, and common JavaScript situations are examined in a hands-on way so that you gain practical experience using Immutable.js that you can apply across your own JavaScript projects. The key to building robust JavaScript applications using immutability is to control how data flows through your application, and how the side-effects of these flows are managed. Many problems that are difficult to pinpoint in large codebases stem from data that’s been mutated where it shouldn’t have been. With immutable data, you rule out an entire class of bugs. Mastering Immutable.js takes a practical, hands-on approach throughout, and shows you the ins and outs of the Immutable.js framework so that you can confidently build successful and dependable JavaScript projects.
Table of Contents (23 chapters)
Title Page
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

Parsing data using the fromJS() function


Immutable.js has one more tool for creating immutable data in addition to everything we've looked at so far in this chapter. The fromJS() function is similar to a JSON parser, because it parses the same types of JavaScript objects that you'll see when you parse JSON strings: objects and arrays.

Parsing JavaScript arrays

You can parse a regular JavaScript array, resulting in a list, as shown here:

import { fromJS } from 'immutable';
const myList = fromJS([1, 2, 3]);
console.log('myList', myList.get(0));
// -> myList 1

This is just like passing the array to the List constructor.

Parsing JavaScript objects

You can parse a regular JavaScript object, resulting in a map:

const myMap = fromJS({
  one: 1,
  two: 2,
  three: 3
});
console.log('myMap', myMap.get('one'));
// -> myMap 1

Once again, this is just like passing the object literal to the Map constructor.

Parsing complex structures

The real power of the fromJS() function is its ability to turn complex...