1.0.6 • Published 5 years ago

re-operators v1.0.6

Weekly downloads
1
License
ISC
Repository
github
Last release
5 years ago

re-operators

Libary for easier reducer definition without ugly switch cases with few extra features, also with type

import { reducer, set } from 're-operators'

const defaults = {
	search: '',
	list: [
		{id: 1, name: 'Walk with koala' },
		{id: 2, name: 'Drink tea' },
		{id: 3, name: 'Feed the sofa' },
	]
}

// state type will be same as typeof provided defaults or as reducer<S> generic argument
// stopOnFirstMatch - set as true if you want to prevent handling by child reducers, or another action handlers if some handler already matched
// note reducer will go in the same order that you define take, for-use, use.
const todos = reducer({ defaults, stopOnFirstMatch })
	.take('search change')
	.merge((action, state) => ({ search: action.payload })) // will return state with changed search field
	.take('todos get success')
	.map((state, action) => ({...state, list: action.payload})) // will return new state
	.catch((error, state, action) => ({...state, error: 'OOops error'})) // return error state if error occured in map
	// connect another reducer
	.use((state, action) => state)
	// define for which part of state you want provide another reducer
	// for selector is typesafe so you will get actual type here when you will use for
	// but under the hood that use proxy to get property path
	// if you will return null here, it will be same as just .use without for
	.for(state => state.search)
	// state will be search field
	// you can provide compare function as second arg to define is sub state changed
	.use<YourTypeHere>((state, action) => state, (a, b) => bool)
	.take('reset')
	// map to provided payload when action matched
	.mapTo(defaults)
	// get resulted reducer function
	.complete();

	// wrapped with typed path selectot set from object-path-immutable just for simple typesafe util
	set(target, target => target.some.typed.field, newValue) // new target
1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago