Skip to content

Instantly share code, notes, and snippets.

@carlesba
Last active November 18, 2022 13:23
Show Gist options
  • Save carlesba/f2a5f666212d86c1fdc6 to your computer and use it in GitHub Desktop.
Save carlesba/f2a5f666212d86c1fdc6 to your computer and use it in GitHub Desktop.
Immutable builder

Purpose

As we know, javascript can't garantee that an object has been mutated so right now the only way to garantee this didn't happen is by using other libraries (like ImmutableJS).

The problem of these libraries is they force developers to use an specific interface so the prize to guarantee this immutability is to loose all the advantages that the language offers in terms of handling Objects/Arrays. Plus, we loose the compatibility with a big amount of libraries like lodash, etc (unless we set some mappers to translate this immutable data to a plain data).

For these reasons I think we need another immutable library. In my opinion the library should:

  • Should work for Objects and Arrays
  • Guarantee immutability in the object/array. No property can be modified.
  • Should keep same interface for reading its values. This is: dot notation or brackets notation.
  • Should offer a way to guess whether we're working with a plain object/array or an immutable one.
  • Could offer same interface as native objects/arrays.
  • In case the native method applies an mutation. This behaviour should be changed [open discussion]
  • Notify when somebody is trying to mutate a property (maybe throwing an error or just logging it)

PS: I'm currently working on a library to finish this properly. Stay tunned.

edit:

This is just an approach. Obviously, those classes need a bunch of methods to work properly.

About the Object.freeze references, the problem of Object.freeze is you don't realise when somebody tries to mutate the object and, in my opinion, that's bad.

Plus, you don't get a frozen object when you extend one. So, you have to freeze the new ones every time, what is too verbose.

edit2:

Still far from the goal but here's the repo: freezr

function ImmutableArray (arr) {
arr.forEach((key, index) => {
Object.defineProperty(this, index, {
enumberable: true,
get: () => key,
set: () => {
throw new Error('cannot be mutated')
}
})
})
}
function ImmutableObject (o) {
Object.keys(o).forEach((key) => {
Object.defineProperty(this, key, {
get: function () {
return o[key]
},
set: function (v) {
throw new Error('cannot be mutated')
},
enumerable : true
})
})
}
@carlesba
Copy link
Author

carlesba commented Mar 6, 2016

@coopy that sounds exactly what I was looking for. Thanks a lot!

@coopy
Copy link

coopy commented Mar 10, 2016

Glad I could help!

@carlesba
Copy link
Author

It's live. Check it out: https://github.com/carlesba/freezr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment