rtfeldman / seamless-immutable

Immutable data structures for JavaScript which are backwards-compatible with normal JS Arrays and Objects.
BSD 3-Clause "New" or "Revised" License
5.37k stars 195 forks source link

Feature Request: array mutations within an object #127

Closed 0x4d6165 closed 8 years ago

0x4d6165 commented 8 years ago

I'm trying to use seamless-immutable as my data option in redux and I'm trying to add a demo "add-post" action which would push a new post into the post array which is within the state object (also an immutable object created with const initialState = Immutable({ posts: [], post: {} });. Using the following, which I would assume would work:

case ActionTypes.ADD_POST :
      return state.update('posts', list => list.push(
        {
          name: action.name,
          title: action.title,
          content: action.content,
          slug: action.slug,
          id: action.id,
          dateadded: action.dateadded,
          updateddate: action.updateddate,
        },
      ));

, causes an error. I could do something like this:

return Immutable({
        posts: [{
          name: action.name,
          title: action.title,
          content: action.content,
          slug: action.slug,
          id: action.id,
          dateadded: action.dateadded,
          updateddate: action.updateddate,
        }].concat(state.posts),
        post: state.post });

but I was hoping for a more elegant, functional programming-y solution 😝. However, I realize that a feature that would allow this sort of nested mutation might go against the philosophy of this module, but I thought I'd suggest it anyway :smile:.

rtfeldman commented 8 years ago

I realize that a feature that would allow this sort of nested mutation might go against the philosophy of this module, but I thought I'd suggest it anyway :smile:.

As you predicted, I'm not open to introducing mutations. 😉 Appreciate the thoughtful post though!