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

@kj455/react-use-search

v1.0.2

Published

React hook for filtering and sorting

Readme

🪝 react-use-search

This hook allows you to implement complex filtering and sorting operations on arrays with ease.

🌟 features

  • type-safe filter and sort operations
  • multiple filter conditions supporting both AND and OR
  • multiple sorting conditions

🚀 Install

npm i @kj455/react-use-search
yarn add @kj455/react-use-search

📚 Usage

📝 Summary

  1. Pass the following arguments to this hook. ('?' means optional)

    • list : the array to filter or sort
    • opsions : an object containing the following properties. details below.
      • filterOptions?
      • sortOptions?
    • initKeys? : an object containing the keys for the initial filter/sort conditions.
  2. Get returned values. results is an array of filtered and sorted items.

  3. Call addFilter(KEY_NAME) or addSort(KEY_NAME) to add a new filter/sort condition.

  4. other methods are available for advanced usage. details below.

const { results, addFilter } = useSearch(list, { filterOptions: {...}, ... });

📖 Details

↪️ filterOptions

filterOptions is an object which key is the name of the filter condition and value is an object containing the following properties.

  • condition : a function which takes an item in list and returns a boolean, which is used for Array.filter() method.
  • groupKey? : if specified, conditions with the same groupKey will be combined with OR condition.

NOTE: By default, AND condition is used for filtering.

:twisted_rightwards_arrows: sortOptions

sortOptions is an object which key is the name of the sort condition and value is an object containing the following properties.

  • rule : a function which takes an item in list and returns a number, which is used for Array.sort() method.

if multiple sort keys are specified, the items will be sorted by the first sort condition, then by the second sort condition, and so on.

🐶 Basic example

Basic filtering. Demo here.

export default function App() {
  const list = [...];
  const { results, addFilter, removeFilter } = useSearch(list, {
    filterOptions: {
      VISIBLE: {
        condition: (item) => item.isVisible,
      },
      WITH_IMAGE: {
        condition: (item) => !!item.imageUrl,
      },
    },
    sortOptions: {
      ID_DESC: {
        rule: (a, b) => a.id - b.id
      },
      ID_ASC: {
        rule: (a, b) => b.id - a.id
      }
    }
  });

  return (...);
}

🐱 Advanced example

  • Combination of AND and OR conditions
  • Specification of initial filter key

Demo here.

const list = [
  { id: 1, name: "piccachu", category: "POKEMON", isHuman: false },
  { id: 2, name: "mew", category: "POKEMON", isHuman: false },
  { id: 3, name: "mario", category: "SUPER_MARIO", isHuman: true },
  { id: 4, name: "pinokio", category: "SUPER_MARIO", isHuman: false }
];

export default function App() {
  const { results, filterKeyList, addFilter, removeFilter } = useSearch(list, {
    filterOptions: {
      HUMAN: {
        condition: (item) => item.isHuman
      },
      POKEMON: {
        condition: (item) => item.category === "POKEMON",
        groupKey: "anime"
      },
      SUPER_MARIO: {
        condition: (item) => item.category === "SUPER_MARIO",
        groupKey: "anime"
      }
    },
  }, { filter: 'POKEMON' });

  return (...)
}

:memo: All Returned Values

| name | description | | ------------- | --------------------------------------------------------- | | results | Array after filtered and sorted | | filterKeyList | Array of keys for the current filter conditions | | filterOptions | Object same as filterOption received as argument | | setFilter | Function to set filter KEY after emptying filterKeyList | | addFilter | Function to add filter KEY to filterKeyList | | removeFilter | Function to remove filter KEY from filterKeyList | | resetFilter | Function to empty filterKeyList | | sortKeyList | Array of keys for the current sort rules | | sortOptions | Object same as sortOption received as argument | | setSort | Function to set sort KEY after emptying sortKeyList | | addSort | Function to add sort KEY to sortKeyList | | removeSort | Function to remove sort KEY from sortKeyList | | resetSort | Function to empty sortKeyList |