hughfdjackson / immutable

neatly packages immutable equivalents to JavaScript's Objects and Arrays.
101 stars 7 forks source link

deep add/get/has/dissoc #28

Closed hughfdjackson closed 11 years ago

hughfdjackson commented 11 years ago

Proposed extensions:

var user = im.object.deep({
  followers: [232, 4343, 54, 4],
  latestPosts: [{
    msg: "happy times"
  }],
 settings: { 
   hideEmailAddress: true
 }
})

user.get('followers') //= im.array( [232, 4343, 54, 4] )
user.get.deep('followers.0') //= 232
user.has.deep('followers.0') //= 232
user.get.deep('settings.hideEmailAddress') //= true

var updatedUser = user.assoc.deep('settings.hideEmailAddress', false)
hughfdjackson commented 11 years ago

This is possibly the role of a supporting library - either as an extension of these methods, or a stand-alone:

var deep = require('immutable-deep')

var updatedUser = deep.assoc(user, 'settings.hideEmailAddress', false)
Raynos commented 11 years ago

My patch function supports deep by default ( https://github.com/Raynos/immutable-hash#hashpatchpath-value ). So does get ( https://github.com/Raynos/immutable-hash#hashgetkey )

hughfdjackson commented 11 years ago

It's very pretty - but what do you do when you want to store keys with . in them? escape?

I guess you could, but I feel like it puts 'gotchas' into the landscape.

Raynos commented 11 years ago

@hughfdjackson I can trivially make all apis support get("foo.bar") and get(["foo", "bar"])

hughfdjackson commented 11 years ago

I think it'd be fairly easy to implement for immutable too - the question is, should it be implemented that way for something that's aiming to be the bread-and-butter immutable object implementation?

Sometimes I think that 'uncomplex' is the best for those types of implementations - to prevent 'wut' moments, and give the impression of solidity.

hughfdjackson commented 11 years ago

For the first draft, going to move this into a util library. Once in, removal from a library is verboten after 1.0.0.