unsafe

1.0.2 • Public • Published

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:

function map (f, list) {
  return list.reduce((acc, item) => {
    acc.push(f(item))
    return acc
  }, [])
}

You'd rather use it like this:

function map (f, list) {
  return list.reduce((acc, item) => {
    return [...acc, f(item)]
  }, [])
}

But you don't want to lose the performance of the former, so...

function map (f, list) {
  return list.reduce((acc, item) => {
    return unsafe.append(acc, f(item))
  }, [])
}

API

append

Adds an item to the end of a list, using a push.

unsafe.append([1, 2, 3], 4) //=> [1, 2, 3, 4]

assign

Merges two objects using Object.assign, mutating the first argument.

unsafe.assign({ a: 1, b: 2 }, { b: 3 }) //=> { a: 1, b: 3 }

assoc

Sets a key and a value to an object.

unsafe.assoc({ a: 1, b: 2 }, 'a', 0) //=> { a: 0, b: 2 }

dissoc

Removes a key from an object.

unsafe.dissoc({ a: 1, b: 2 }, 'a') //=> { b: 2 }

prepend

Adds an item to the beginning of a list, using an unshift.

unsafe.prepend([2, 3, 4], 1) //=> [1, 2, 3, 4]

Package Sidebar

Install

npm i unsafe

Weekly Downloads

10

Version

1.0.2

License

ISC

Unpacked Size

2.6 kB

Total Files

3

Last publish

Collaborators

  • brandonchartier