0.2.1 • Published 6 years ago

immutator v0.2.1

Weekly downloads
-
License
MIT
Repository
-
Last release
6 years ago

immutator

a redux-inspired state mutator.

npm install immutator

be aware: immutator uses Proxy. see node support and browser support.

example

const immutator = require('immutator')

const state = immutator({ count: 0 })

try {
  state.count++
} catch (err) {
  console.log('Error: state cannot be mutated directly. please define a mutator.')
}

console.log(state) // { count: 0 }

// define mutator `increment`
state.increment = state => state.count++

// call mutator
state.increment() 

console.log(state) // { count: 1 }

state is a proxy whose target can only be mutated by defining and calling a mutator.

functions defined on the immutator are mutators. mutators are passed a mutable copy of state as their first argument when they are called. while they share the same namespace, defining a mutator does not affect state.

api

const immutable_state = immutator(initial_state={})

immutable_state[mutation_type] = mutator(mutable_state, ...mutation_data)

immutable_state[mutation_type](...mutation_data)

0.2.1

6 years ago

0.2.0

6 years ago

0.1.0

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago