npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@sterensoftware/rdx

v0.4.0

Published

RDX is a small Redux library of "higher-order reducers" (and a couple utilities). It began life as a single helper function to enable writing more declarative code.

Downloads

17

Readme

RDX

RDX is a small Redux library of "higher-order reducers" (and a couple utilities). It began life as a single helper function to enable writing more declarative code.

RDX also embraces separating state from the "functional core" of application business logic.


Installation

npm install --save @sterensoftware/rdx

Available named imports

reduceConditionally

reduceConditionally({
	when: (state, action) => boolean,
	with: <reducer>,
	[otherwise: <default reducer>]
})

reduceConditionally, which simply returns existing state by default, supports Redux's fundamental paradigm of well-defined state transitions by allowing state to change only under explicit conditions. Overriding the default behavior is also possible (by providing an otherwise reducer), when necessary.

reduceOnActionType

reduceOnActionType({
	[actionType]: <reducer>,
	...
})

or

reduceOnActionType({
	[actionType]: <reducer>,
	...
}, <default reducer>)

reduceOnActionType builds reducer functionality for a single piece of state by declarative composition of simple reducers.

In other words, it reduces cognitive load (and supports Separation of Concerns) by allowing each action to be reduced independently, enhances readability by "labeling" each reducer with an action.type, and encourages declarative programming by obsoleting the imperative Big Giant Switch.

reduceObjectProperties

reduceObjectProperties(
	{
		reducePropertyName: (state, action) => string,
		reducePropertyValue: <reducer>
	},
	...
)

reduceObjectProperties will run all reducers passed to it (in order) as long as the prior state is not undefined or null. It will reduce each property of the exisiting state as specified: reducePropertyName indicates which property to set with the value returned by reducePropertyValue. Any properties not specified will remain unchanged. If reducePropertyValue returns undefined the property will be deleted from the object.

(Please note, returning a static string value from each reducePropertyName method approximates Redux's combineReducers functionality.)

mapObjectPropertiesReducerParameters

mapObjectPropertiesReducerParameters({
	reducePropertyName: string?,
	reducePropertyValue: string?
})

mapObjectPropertiesReducerParameters will produce a function analogous to reduceObjectProperties while allowing all respective reducers to be named by the developer. For example:

const reduceObjectProperties = mapObjectPropertiesReducerParameters({
	reducePropertyName: 'which',
	reducePropertyValue: 'with'
})

...

reduceObjectProperties(
	{
		which: (state, action) => string,
		with: <reducer>
	},
	...
)

(Since 0.4.0, mapObjectPropertiesReducerParameters is used internally to provide reduceObjectProperties.)

reducePropertyFromAction

reducePropertyFromAction(<action property name>)

or

reducePropertyFromAction(action => <new state>)

reducePropertyFromAction is a small helper function for building a reducer that simply returns the given property directly from the action. It is included for convenience.

Since 0.3.1:

To support better readability (via consistency), reducePropertyFromAction will also accept a function that returns new state.

Please note: The function provided here is not a reducer; it will be invoked with the action as its first and only parameter.

reduceArrayElements

reduceArrayElements({
	when: (state, action) => boolean,
	with: <reducer>,
})

reduceArrayElements enables updating only the specified element(s) of a state collection (an array). To add or remove item(s) see respectively appendToArray or removeFromArray, below.

(Please note, this function is only supported for array state.)

appendToArray

appendToArray({ with: (state, action) => <value(s) to append> })

appendToArray enables growing a state collection by one or more items. If your reducer returns an array of values then the state returned will be a concatenation of the existing state and those values. To append an array to state, simply wrap it with an outer array.

(Please note, this function is only supported for array state.)

removeFromArray

removeFromArray({ when: (element, action, index) => boolean })

removeFromArray will drop any elements from state for which your function returns true.

(Please note, this function is only supported for array state.)

withDefaultInitialState

withDefaultInitialState(<initial state>, <reducer>)

withDefaultInitialState allows setting default initial state for an existing reducer (similar to an argument default when defining an new reducer, e.g. (state = defaultInitalState, action) => { ... }).

replace

replace({ with: <replacement> })

or

replace({ with: (state, action) => <replacement> })

replace returns a reducer that always returns the replacement.

If with is a function, it will simply be returned. This is to support programatic replacement while preserving readability.

keepExistingState

(state, action) => state

keepExistingState is a reducer that simply returns existing state. It is included to aid readability.

remove

remove is a do-nothing reducer that simply returns undefined. It is included to aid readability when using RDX, but be warned it is not compatible with out-of-the-box Redux, which intentionally doesn't support reducers returning undefined (and if you're a Redux maintainer reading this and thinking "maybe?", please don't break your existing undefined paradigm to accomplish something like this silly remove).