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

update-function-types

v2.1.2

Published

Provides a specification and basic type classes for update function. Essentially there are two kinds of update functions —

Downloads

29

Readme

Update Function Types Build Status

Provides a specification and basic type classes for update function. Essentially there are two kinds of update functions —

  1. ReducerFunction: takes in action and state and returns a new state.
  2. CommandFunction: takes in action and state and return a new action.

Index

Types

Reducer Function

  1. Takes two arguments viz. Value and State.
  2. Always return a State.
  3. Does not mutate the original objects provided.
export type ReducerFunction<Value, State> = {
  (value: Value, state: State): State
}

Command Function

  1. Takes two arguments only viz. Value and State.
  2. Always returns an Action. It can consider returning Nil if no meaningful value is intended.
  3. Does not mutate the original objects provided.
export type CommandFunction<Value, State, Output> = {
  (value: Value, state: State): Action<Output>
}

Library Functions

concatR

Takes in multiple ReducerFunction(s) as arguments and returns a new ReducerFunction as a result.

  1. Consider the ReducerFunction(s) R0 R1 then two statements will be logically equivalent —
    1. concatR(R0, R1)(action, state)
    2. R1(action, R0(action, state))
  2. Its associative — concatR(concatR(R0, R1), R2) === concatR(R0, concatR(R1, R2))
  3. Its additive — concatR(R0, zeroR) === R0.

Usage

import {concatR} from 'update-function-type'

const R0 = (a, b) => a + b
const R1 = (a, b) => a * b

concatR(R0, R1)(10, 20) // 10 * (10 + 20) === 300

concatC

Takes in multiple CommandFunction(s) as arguments and returns a new CommandFunction as a result.

  1. Consider the CommandFunction(s) C0 C1 then two statements will be logically equivalent —
    1. concatC(C0, C1)(action, state)
    2. List(C0(action, state), C1(action, state))
  2. Its associative — concatC(concatC(C0, C1), C2) === concatC(C0, concatC(C1, C2))
  3. Its additive — concatC(C0, zeroC) === C0

Usage

import {concatC} from 'update-function-type'

const C0 = (a, b) => action('+', a + b)
const C1 = (a, b) => action('*', a * b)

concatC(C0, C1)(10, 500) // List(action('+', 510), action('B', 5000))

matchR

  1. Takes in specification object with keys as action types and values as ReducerFunction(s) and returns another ReducerFunction.
  2. The returned function when called with an Action and State internally calls the function matching the action's type.
  3. The matching reducer function is called with value of the original action and the state is passed as is.
  4. In case no action matches the given specification the original state is returned.

Usage

import {matchR} from 'update-function-type'

const reducer = matchR({
  add: (a, b) => a + b,
  mul: (a, b) => a * b
})

reducer(action('add', 10), 1000) // returns 1010

matchC

  1. Takes in specification object with keys as action types and values as CommandFunction(s) and returns another CommandFunction.
  2. The returned function when called with an Action and State internally calls the function matching the action's type.
  3. The matching reducer function is called with value of the original action and the state is passed as is.
  4. In case no action matches the given specification Nil is returned.

Usage

import {matchC} from 'update-function-type'

const reducer = matchR({
  add: (a, b) => action('added', a + b),
  mul: (a, b) => action('multiplied', a * b)
})

reducer(action('add', 10), 1000) // returns action('added', 1010)

zeroR

  1. Is a ReducerFunction.
  2. Takes in an action and a state and returns the same state.

zeroC

  1. Is a CommandFunction.
  2. Takes in an action and a state and returns the same action.