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

Lazy mapping


The mapping that we've done so far has been called directly on collections. Mapping operations can be done lazily as part of a sequence of transformations that feed into a side-effect. Being able to filter and map lazily is part of a pattern that you'll use frequently with Immutable.js.

Multiple map() calls

Let's revisit our earlier example where we transformed two map values into a single capitalized name string:

const capitalize = s =>
  `${s.charAt(0).toUpperCase()}${s.slice(1)}`;
const myList = List.of(
  Map.of('first', 'joe', 'last', 'brown', 'age', 45),
  Map.of('first', 'john', 'last', 'smith', 'age', 32),
  Map.of('first', 'mary', 'last', 'wise', 'age', 56)
);

myList
  .toSeq()
  .map(v => v.update('first', capitalize))
  .map(v => v.update('last', capitalize))
  .map(v => [v.get('first'), v.get('last')].join(' '))
  .forEach(v => console.log('name', v));
  // -> name Joe Brown
  // -> name John Smith
  // -> name Mary Wise

In this version of the...