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

usemutator

v1.0.5

Published

useReducer, but with minimal boilerplate ⚡

Downloads

13

Readme

🧬 useMutator 🧬

So you want to reduce time spent writing reducer code, eh? Say hello to your friendly neighbourhood useReducer wrapper - useMutator 🦸 - that lets you do just that and more:

  • Lends itself to auto-completion 🤖
  • Automatically merges your state changes, but can just as easily overwrite the existing state, with the flip of a switch 💡
  • Works with simple mutator functions instead of ugly strings of actions and cumbersome switch statements 😵
  • Mutator functions can return objects OR functions that get enriched with state, that then return objects - YOU decide what you need! 🎯

Now you too, can reduce with minimal boilerplate and with minimal dependencies - thanks to just a few extra lines of code ⚡🎉

Simply yarn add usemutator or npm i usemutator to get started 😎

Easy as 3, 2, 1! 🏎️💨

import useMutator from 'usemutator'
...
const MyShinyForm = () => {
    const { state, mutate } = useMutator(MyShinyForm.initialState)
    const onFormFieldChange = (e) => mutate(MyShinyForm.mutators.FORM_INPUT(e.target.name, e.target.value))
    return <input type="text" name="full_name" value={state.full_name} onChange={onFormFieldChange} />
}
...
MyShinyForm.mutators = {
    FORM_INPUT: (name, value) => ({ [name]: value }),
}
MyShinyForm.initialState = {}

How it works

First you run the useMutator hook with an initial state, which gives you an object with state and mutate. Unless you've been living under a rock you already know state, but mutate is the magic sauce:

mutate(mutation: Object|function(any) => Object, replace=false)

mutate is by itself just a function, which takes two arguments: first argument is either an object or a function that returns an object. This is the mutation to apply. The second argument is replace - this is simply a boolean which by default is off. If you manually switch it to true, you force mutate to swap out the current state with your mutation.

Advanced usage 🧠✨

import useMutator from 'usemutator'

const MyTodo = ({ children, done, ...props }) => <li {...props} style={{ textDecoration: done ? 'line-through' : null }}>{children}</li>

const Todos = () => {
    const { state, mutate } = useMutator(Todos.initialState)
    const onFormInputChange = (e) => mutate(Todos.mutators.FORM_INPUT(e.target.name, e.target.value))
    return (
        <>
            <h1>ToDo:</h1>
            <ul>
                {state.todos.map((todo, index) => (
                    <MyTodo key={index} done={todo.done}>
                        <input type="checkbox" checked={todo.done} onChange={() => mutate((state) => Todos.mutators.TOGGLE_TODO_DONE(state, index))} />&nbsp;
                        {todo.todoText}&nbsp;
                        <button type="button" onClick={() => mutate((state) => Todos.mutators.REMOVE_TODO(state, index))}>Remove</button>
                    </MyTodo>
                ))}
            </ul>
            <form onSubmit={(e) => {e.preventDefault(); mutate((state) => Todos.mutators.ADD_TODO(state, state.new_todo_text))}}>
                <label>
                    Add todo: <input type="text" name="new_todo_text" value={state.new_todo_text} onChange={onFormInputChange} />
                    <button type="button" onClick={() => mutate((state) => Todos.mutators.ADD_TODO(state, state.new_todo_text))}>Add</button>
                </label>
            </form>
        </>
    )
}
Todos.initialState = {
    todos: [],
}
Todos.mutators = {
    ADD_TODO: (state, todoText) => {
        const todos = todoText ? [ ...state.todos, { todoText } ] : state.todos
        return { todos, new_todo_text: '' }
    },
    REMOVE_TODO: (state, index) => {
        state.todos.splice(index, 1)
        return state
    },
    TOGGLE_TODO_DONE: (state, index) => {
        state.todos[index].done = !state.todos[index].done
        return state
    },
    FORM_INPUT: (name, value) => ({ [name]: value }),
}