12

I'm using immutable.js with my flux application. It is very useful and gives performance boost. But what actually makes me sad is the fact that I can't use lodash together with it. Lodash provides great API with ton of useful functions, so I wonder maybe there is a way to get them together to work for me?

Boris Zagoruiko
  • 12,705
  • 15
  • 47
  • 79
  • 2
    Maybe provide some code you're trying to use that illustrates _how_ the two libraries don't work together? – Adam Boduch May 07 '15 at 20:51
  • I don't see how they could work together, as lodash handles javascript arrays and objects - immutable.js replaces them with its own Lists and Maps. – OlliM May 12 '15 at 12:06
  • You can also check out https://github.com/engineforce/ImmutableAssign, which supports immutability and allows you to continue working with POJO (Plain Old JavaScript Object). – engineforce Jun 21 '16 at 14:58

6 Answers6

11

You could use Ramda if you want immutability and side-effect free functions.

The library provides useful functions similar to lodash but in a functional programming style that never mutates user data.

Consider this React example using the flatten function in Ramda using ES6 syntax.

import { flatten } from 'ramda';  

const users = ['Steve Jobs', ['Steve Wozniak']];
const flatUsers = flatten(users);

// returns ['Steve Jobs', 'Steve Wozniak']
Brett Hayes
  • 253
  • 3
  • 7
6

I'm assuming you're trying to do something like do a lodash map on an immutable set. Lodash doesn't do helpful things when you try to do that directly.

Note that immutable.js already has a lot of its own manipulation functions (like map) as well as its own lazy chaining (via Seq) so you should look into those.

If you need to do something that lodash provides and immutable.js does not, one thing you can do is take your immutable object and convert it to a vanilla JS object for consumption by lodash. For instance:

// Do all of your fancy immutable.js stuff...
my_set = immutable.Set([1,2,3]).union(immutable.Set([2,3,4]))
// ...and then convert to JS before you do all of your fancy lodash stuff
mapped_set = _(my_set.toArray()).map(whatever)

You'll of course have to take performance into account here, since you may end up with the worst of both worlds if you convert from one to the other by copying your data into a vanilla data structure. In the toy case above, for instance, you'd probably be better off using an immutable.js map() directly.

waterproof
  • 4,943
  • 5
  • 30
  • 28
  • 6
    Converting stuff from JS and to JS many times is not acceptable for my particular case. Anyway I found out that immutable.js has large API by itself and in most cases replaces lodash. Anyway thank you for answering – Boris Zagoruiko May 28 '15 at 06:38
  • I can attest to the constant seesawing use of .fromJS() and .toJS() to be very costly. I would suggest either assuming your entire state tree is encapsulated within Immutable.js' API or be very careful with how you weave it in. – Con Antonakos Aug 01 '16 at 19:02
5

I've recently written a Lodash wrapper providing Immutable.JS support called mudash. Most of the major Lodash functions are supported with more being added regularly.

Brian Neisler
  • 843
  • 9
  • 12
1

seamless-immutable aims to provide an API that is backwards compatible with vanilla JS data structures.

This allows you to use lodash methods. However, since many of lodash's methods are written with mutability in mind, they are probably far from optimal.

bryanph
  • 993
  • 7
  • 18
1

You may want to check out https://github.com/engineforce/ImmutableAssign created by me, which is a light weigh immutable helper, which supports immutability and allows you to continue working with POJO (Plain Old JavaScript Object). Therefore you can use any lodash functions.

E.g.,

var iassign = require("immutable-assign");
var _ = require("lodash");

var o1 = { a: { c: 1 }, b: [1, 2, 3] };
var o2 = iassign(
    o1, 
    function(o) { return o.b; },   // get property to be updated 
    function(b) {                  // update select property
        return _.map(b, function(i) { return i + 1; }); 
    }
);


// o2 =  { a: { c: 1 }, b: [2, 3, 4] }
// o1 is not modified

// o2 !== o1
// o2.b !== o1.b

// o2.a === o1.a
engineforce
  • 2,840
  • 1
  • 23
  • 17
1

How about to use withMutations in ImmutableJS?

Search for Batching Mutations for brief explanation here.

Artem Gavrysh
  • 124
  • 1
  • 3