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 🙏

© 2026 – Pkg Stats / Ryan Hefner

useadapter

v1.1.1

Published

A React hook to help abstract user input.

Readme

useAdapter

A React hook to help abstract user input.

Build status badge npm Mutation testing badge npm bundle size

Install

With npm.

npm install useadapter

With yarn.

yarn add useadapter

Usage

import useAdapter from 'useadapter'
function PhoneNumberField({ value, onChange }) {
    const [getInput, setInput] = useAdapter(
        // display, turn 8175553356 into 817-555-3356
        digits => {
            if (digits?.length !== 10) return ''
            return `${digits.substr(0,3)}-${digits.substr(3, 3)}-${digits.substr(6, 4)}`
        },
        // parser, turn any formatted phone number like 817-555-3356 into 8175553356
        input => {
            const digits = input.match(/\d+/g).join('')
            if (digits.length !== 10) return null
            return digits
        }
    )

    return (
        <input type="tel"
            // getInput runs the argument through the display function above
            value={getInput(value)}
            // setInput runs the argument through the parser function above
            onChange={e => onChange(setInput(e.target.value))}
        />
    )
}

The setInput function does three things. First, it stores e.target.value in React state. Next, it runs e.target.value through your custom parser function and stores the result in React state. And finally, it returns the result of the parse so that you may pass it directly to a callback or a conventional setState.

On the first render, getInput will simply run value through your custom display function and return the result. On subsequent renders, however, getInput will check if value is equal to the result of parsing the current input. If the value is equal to parsing the input (which was stored by setInput), getInput will simply return the stored input. Equality is checked by the fast-deep-equal npm package, such that objects and arrays with equivilant structures and equal values are considered equal.

This system allows PhoneNumberField to abstract the input value for any higher-level component without disallowing the user from typing a phone number in whatever format they see fit. Normally, if a parent component only stores the digits of a phone number, the user would be incapable of typing formatting characters like - or spaces, as React idioms dictate that the state be passed as the current input value,

<input type="tel" value={number} onChange={ev => setNumber(ev.target.value)} />

Conversely, useAdapter allows developers to parse values before hoisting them into React state while still retaining the users precise input as they type.

Of course, you could skip passing the value back to the input, but then your React app would not be able to programmatically update the phone number if necessary, as with a reset button.

<button onClick={() => onChange(previousNumber)}>Reset</button>

In the case of a reset, getInput would detect that the value has changed, which would trigger the use of the display function to generate the new input value.