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

Flattening collections


Collections in Immutable.js can have simple values, or they can have complex values such as other collections. These collections can in turn contain other collections, and so on. These deep structures are necessary to reflect the model of your application data. However, traversing nested hierarchies is error prone.

Avoiding recursion

When you're dealing with hierarchical data, recursion is inevitable. We write a function that calls itself when a new level in the hierarchy is discovered. This is difficult to do because these types of functions often end up being highly specialized, applying to only one situation. With Immutable.js and its persistent-change/transformation/side-effect pattern, writing recursive functions is often a dead end.

When traversing nested collections, you don't often care where you are in the hierarchy. So, if all you need is to locate a particular value, it's much simpler to traverse a flat collection.

Note

The downside to flattening collections...