ecmascript-immutable-data-structures icon indicating copy to clipboard operation
ecmascript-immutable-data-structures copied to clipboard

Easy conversion between mutable and immutable with prototype methods.

Open nmn opened this issue 9 years ago • 0 comments

There are lots of methods already defined on the prototypes of the types.

I propose two more methods. The idea is to have the same capability as 'fromJS' and 'toJS' as seen on 'immutable-js' On all the immutable types:

.toMutable

This would return a new mutable object/array/Map/Set with the same data as the Immutable value. It will not mutate the Immutable Type.

On the other hand, the Object.prototype, Array.prototype, Map.prototype and Set.prototype, should get one extra method:

.toImmutable

This would return a new Immutable type with the same values as the ones in the mutable types.

  • In case of Objects, only the keys that are on the object themselves (and not prototypes) will be copied over.
  • In case of arrays, and 'holes' will be converted to undefined
  • There doesn't seem to be any edge cases with Maps of Sets.

These methods will also be recursive, as in all sub values, will be made mutable/immutable as well.

Polyfills for these methods are fairly trivial, and I'll be posting them here in more comments soon.

nmn avatar Apr 08 '15 16:04 nmn