liveBook · Manning

Immutable.fromJS

As explained in Data-Oriented Programming: Reduce software complexity

Immutable.fromJS is an Immutable.js function that recursively converts JavaScript values into immutable collections and values.

Immutable.fromJS recursively converts native JavaScript objects and arrays into Immutable.js collections. Nested maps and lists become immutable too, giving persistent-data-structure code a consistent representation. section 9.3.2

Listing 9.9 Conversion to immutable data
var libraryData = Immutable.fromJS({
  "catalog": {
    "booksByIsbn": {
      "978-1779501127": {
        "isbn": "978-1779501127",
        "title": "Watchmen",
        "publicationYear": 1987,
        "authorIds": ["alan-moore",
          "dave-gibbons"]
      }
    },
    "authorsById": {
      "alan-moore": {
        "name": "Alan Moore",
        "bookIsbns": ["978-1779501127"]
      },
      "dave-gibbons": {
        "name": "Dave Gibbons",
        "bookIsbns": ["978-1779501127"]
      }
    }
  }
});

Convert a nested library catalog in one expression; catalog, books, authors, and identifier lists become immutable values. section 9.3.2

Use the converted value as the boundary between ordinary JavaScript data and the Immutable.js API:

  • Read fields with Immutable.get or nested paths with Immutable.getIn rather than dot or bracket notation. section 9.3.2
  • Prepare expected values and test fixtures with Immutable.fromJS, then compare collections with Immutable.isEqual. section 9.4.1 section 9.4.2

A practical boundary

A book-search test converts both catalog data and the expected result with Immutable.fromJS, then verifies a matching Watchmen result and an empty Batman result. section 9.4.1

How Data-Oriented Programming: Reduce software complexity covers it

How deep it goes
Defines recursive conversion and demonstrates nested fixtures, but defers conversion internals and collection implementation details.
Examples
Uses a nested library catalog, book-search fixtures, and member-addition mutation fixtures.
What it assumes
Assumes JavaScript objects, arrays, and nested data, while teaching the need for Immutable.js representations.
What you can do after
Apply it to convert nested JavaScript inputs and expected test values into Immutable.js data.
How it connects
Connects JavaScript objects and arrays with Immutable.js collections, persistent data structures, Immutable.js API functions, and Lodash-style queries.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data-Oriented Programming: Reduce software complexity. The section links point to the parts of the book it draws on.