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

@usagihana/ducktools

v2.5.0

Published

namespaced duck (reducer package) factories for redux to reduce boilerplate and increase fun

Downloads

10

Readme

Ducktools Logo

ducktools - namespaced reducer package (duck) factories

reduce boilerplate and increase fun with redux

Why?

Boilerplate makes redux tedious. Most reducers are either just a value, async value or a list of values. These reusable, namespaced, state slice reducer packages help you save time

Usage:

createValueDuck

import { createValueDuck } from "@usagihana/ducktools"
/* Ducks bundle their namespaced reducer, action creators and action types
    createValueDuck returns:
    const duck = {
        namespace,
        reducer: namespace ? createReducerNamespace(valueReducer, namespace) : valueReducer,
        //types
        VALUE_CREATED, 
        VALUE_REMOVED,
        // action creators
        create, 
        remove,
    }
*/

// choose a namespace, onlyActions with correct namespaces will be send to these reducers
export const myValueDuck = createValueDuck('app_myValue');
export const reducer = myValueDuck.reducer





store.dispatch( myValueDuck.create('value') )
// store.dispatch({type:myValueDuck.VALUE_CREATE, namespace: 'myNamespace', payload:'value' })

store.dispatch( myValueDuck.remove() )
// store.dispatch({type:myValueDuck.VALUE_REMOVE, namespace: 'myNamespace' })

createAsyncDuck

import { createAsyncDuck } from "@usagihana/ducktools"
/*  createAsyncDuck returns:
    const duck = {
        namespace,
        reducer: namespace ? createReducerNamespace(asyncReducer, namespace) : asyncReducer,
        // types
        ASYNC_REQUESTED, 
        ASYNC_REJECTED,
        ASYNC_FULFILLED,
        // action creators
        request, 
        reject, 
        fulfill,
        // thunks
        asyncFetchData
    }
*/


export const myAsyncDuck = createAsyncDuck('app_myAsyncValue');
export const reducer = myAsyncDuck.reducer


store.dispatch( myAsyncDuck.request() )

setTimeout( () => {
  const data = 'value'
  store.dispatch( myAsyncDuck.fulfill(data) )
}, 1000)

// store.dispatch( myAsyncDuck.reject() )
// store.dispatch({type:myAsyncDuck.ASYNC_REJECTED, namespace: 'myNamespace'})

createListDuck

import { createListDuck } from "@usagihana/ducktools"
/*  createListDuck returns: 
    const duck = {
        namespace,
        reducer: namespace ? createReducerNamespace(listReducer, namespace) : listReducer,
        // type
        LIST_CREATED, 
        LIST_REMOVED,
        LIST_RESET,
        LIST_CREATEDMANY,
        LIST_SORT,
        // creators
        create, 
        remove,
        reset,
        createMany,
        sort
    }
*/


export const myListDuck = createListDuck('app_myList');
export const rootReducer = myListDuck.reducer


store.dispatch( myListDuck.create({ id: 'myId',  name: "bla" }) )
store.dispatch( myListDuck.create({ id: 'myId2', name: "bla" }) )
// store.dispatch({type:myListDuck.LIST_CREATE, namespace: 'myNamespace', payload:{ id: 'myId',  name: "bla" }})

/* Lists have a object with IDs as keys and a order array
store = {
    list: { 
        "myID": { id: "myID" },
        "myID2": { id: "myID2" }
    },
    listOrder: [
        "myID", 
        "myID2"
    ]
}
*/

Full Usage Example:

import { createValueDuck, createAsyncDuck, createListDuck } from "@usagihana/ducktools"


// choose a namespace, onlyActions with correct namespaces will be send to these reducers
//
const namespace = "myNamespace";

const myListDuck = createListDuck(namespace+'myList');
const myValueDuck = createValueDuck(namespace+'myValue');
const myAsyncDuck = createAsyncDuck(namespace+'myAsync');

const rootReducer = combineReducers({
    myList: myListDuck.reducer,
    myValue: myValueDuck.reducer,
    myAsync: myAsyncDuck.reducer
})


// create store 
const store = createStore(rootReducer, undefined, applyMiddleware(logger,thunk))


store.dispatch( myValueDuck.create('value') )


store.dispatch( myListDuck.create({ id: 'myId',  name: "bla" }) )
store.dispatch( myListDuck.create({ id: 'myId2', name: "bla" }) )


store.dispatch( myAsyncDuck.request() )

setTimeout( () => {
  const data = 'asyncvalue'
  store.dispatch( myAsyncDuck.fulfill(data) )
}, 1000)

/*
store = {
    myList: {
        list: { 
            "myID": { id: "myID" },
            "myID2": { id: "myID2" }
        },
        listOrder: [
            "myID", 
            "myID2"
        ]
    },
    myValue: 'value',
    myAsync: 'asyncvalue'
}
*/