Unsafe
A lil library of mutating functions to use in reducers!
npm install unsafe
Just why?
- Declarative: It's pretty nice to have the mutation inside its own function that returns the new state.
- Performance: It feels good to win performance tests.
- Awkwardness: Mutating functions should probably come with a warning, and
unsafe
fits nicely.
When... why would I want this?
You're writing a reducing function and you're a little annoyed that it looks like this:
{ return list}
You'd rather use it like this:
{ return list}
But you don't want to lose the performance of the former, so...
{ return list}
API
append
Adds an item to the end of a list, using a push
.
unsafe //=> [1, 2, 3, 4]
assign
Merges two objects using Object.assign, mutating the first argument.
unsafe //=> { a: 1, b: 3 }
assoc
Sets a key and a value to an object.
unsafe //=> { a: 0, b: 2 }
dissoc
Removes a key from an object.
unsafe //=> { b: 2 }
prepend
Adds an item to the beginning of a list, using an unshift
.
unsafe //=> [1, 2, 3, 4]