DEV Community

Discussion on: Immutability in JavaScript

Collapse
 
nlepage profile image
Nicolas Lepage

Hi Steven!

Actually our API has a way to do this:

const { flow, set, push } = require('immutadot')

const person = {
  name: 'Steven',
  cities: ['Berlin', 'Lisbon'],
  currentCity: 'Berlin'
}

const movedIntoNewCity = flow(
  push('cities', 'Amsterdam'),
  set('currentCity', 'Amsterdam')
)(person);

console.log(movedIntoNewCity)

Here's your working example on runkit: runkit.com/embed/kr9e9tins87j

Anyway thank you for your comment, we're waiting for other constructive feedback from you.

Collapse
 
stevenlangbroek profile image
Steven Langbroek • Edited

That's great! Took a cursory look at the code, and if you'd reversed the argument order, you could've delegated to pipe (maybe a more familiar name for people?) and compose (right-associative version of pipe) from lodash, ramda or whatever. The problem now is, what if I'm already piping a bunch of unary functions using any of the bigger FP toolbelts, and add a immutadot function? It won't work:

const newState = pipe(
  over('items', filter(has('id')),
  // won't work
  push('items', newItem)
)(oldState);