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

use-list-transform

v0.2.3

Published

🤖 Filter, sort, manipulate your arrays in a react hook.

Downloads

6

Readme

🤖 use-list-transform

Build Status Coverage Status

A tiny, simple React hook that handles performing functions on an array. In other words, you provide the functions for sorting, filtering and manipulating the array and the hook will return the transformed array. Supports promise transforms.

  • TypeScript support
  • Promise-based transforms
  • Tiny API
  • Tiny size

Install

npm install use-list-transform --save

Usage

import useListTransform from "use-list-transform";

const searchTerm = ({ list, data }) => list.filter((item) => item.name.includes(data.searchTerm)); 

const SearchList = ({ list }) => {
    const { transformed, setData } = useListTransform({ 
        list, 
        transform: [searchTerm] 
    });

    return (
        <div>
            <input type="text" onChange={(evt) => setData({ searchTerm: evt.target.value })}/>
            <ul>
                {transformed.map((item, index) => (
                    <li key={index}>{item.name}</li>
                ))}
            </ul>
        </div>
    );
}

API

Options

  • list: An array of values to apply transforms on.
  • transformData: The data given to the transform functions to apply transformations.
  • transform: An array of transform functions or a single function to apply to the list. The function is given an object containing the list, data (the transform data) and the previousData (previous transform data). The function must return the transformed list.
  • throwOnError: (Default: false) If set to true, all transform errors will be thrown and not handled by the hook.
  • onLoading (loading: boolean) => void: Triggers prior to applying transformations and after it has completed.
  • onError (error: Error) => void: Triggers when an error occurs.
  • onListUpdate (list: TListItem[]) => void: Triggers when the transformed list has been updated.

Return values

  • transformed: The transformed array of values.
  • transformData: The data given to the transform functions.
  • setData (data: TTransformData) => void: Set the transform data.
  • updateData: (data: Partial<TTransformData>) => void: Update the existing transform data.
  • resetData: () => void: Reset the transform data to the original data passed by the options.
  • loading: Whether transforms are being applied.
  • error: Returns an error if an error has occurred during transform.

Examples

Multiple transforms

import useListTransform from "use-list-transform";

const byAge = ({ data }) => (item) => item.age == data.age;
const byTags = ({ data }) => (item) => item.tags.some((t) => data.tags.include(t));

const SearchList = ({ searchList }) => {
    const tags = ["ricky", "steve", "karl"];

    const { transformed, updateData, transformData } = useListTransform({ 
        list: searchList, 
        transform: [byAge, byTags] 
    });

    function onToggleTag(tag) {
        const tagIndex = transformData.tags.findIndex((t) => t === tag);

        updateData({
            tags: tagIndex === -1 
                ? [...transformData.tags, tag]
                : transformData.tags.slice(tagIndex)
        })
    }

    return (
        <div>
            <div>
                <ul>
                    {tags.map((tag, index) => (
                        <li key={tag} onClick={() => onToggleTag(tag)}>
                            {tag}
                        </li>
                    ))}
                </ul>
            </div>
            <ul>
                {transformed.map((item, index) => <li>{item.name}</li>)}
            </ul>
        </div>
    );
}

Promise-based transform requests (i.e. web workers)

// `greenlet` - moves async functions into its own threads
import greenlet from "greenlet";
import useListTransform from "use-list-transform";

const byWebWorker = greenlet(async ({ list, data }) => {
    // expensive operation on another thread..
    return list;
});

const SearchList = ({ searchList }) => {
    const { transformed, setData, loading } = useListTransform({ 
        list: searchList, 
        transform: [byWebWorker] 
    });

    return (
        <div>
            {loading && <p>Transforming...</p>}
            <input type="text" onChange={(evt) => setData({ searchTerm: evt.target.value })}/>
            <ul>
                {transformed.map((item, index) => <li>{item.name}</li>)}
            </ul>
        </div>
    );
}

Contributors

  • @ljbc1994