1.2.9 • Published 2 years ago

@lindeneg/search v1.2.9

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

@lindeneg/search

typescript bundle-size license

Sandbox


React hook for filtering and optionally sorting objects by nested values against a query in a type-safe way.

Installation

yarn add @lindeneg/search


Arguments

NameRequiredRefTypeDescription
dataYTArray<Record<string \| number \| symbol, unknown>>array of objects to filter
predicateY-string[] \| (query: string, item: T[number], index: number) => booleanan array of property names to target or a predicate function
optsN-UseSearchOptionsoptions, see below

UseSearchOptions

NameRequiredDefaultTypeDescription
modeN"lenient""strict" \| "lenient"lenient escapes invalid symbols, strict ignores them completely
sortN-(a: T[number], b: T[number]) => numberoptionally provide a sort function to be called post-filtering

Return

Object with properties:

NameTypeDescription
filteredTarray of filtered, optionally sorted, objects
querystringthe current query
onQueryChange(target: string \| React.FormEvent<HTMLInputElement>) => void;function to change query
onPredicateChange(predicate: string[] \| (query: string, item: T[number], index: number) => boolean) => void;function to change predicate

Usage

import useSearch from '@lindeneg/search';

function SomeComponent() {
  const { filtered, query, onQueryChange, onPredicateChange } = useSearch(
    data,
    predicate,
    useSearchOptions
  );

  console.log(filtered);

  return (
    <div>
      <input value={query} onChange={onQueryChange} />
    </div>
  );
}

Suppose the following object:

interface User {
  id: number;
  name: string;
  email?: string;
  activity: { events: { context: string }[] }[];
  interest: {
    name: string;
    info: {
      special: string;
    };
  };
  arr: string[];
}

Say the interesting keys when filtering are name and email:

useSearch(users, ['name', 'email']);

Then the data will be filtered using the values of the name and email keys against a query.

Values found inside nested objects or arrays can also be used. Suppose the desired key to include is context found inside the events array that is itself found inside the activity array.

useSearch(users, ['activity.n.events.n.context']);

Then the data will be filtered using the values of the context key against a query. n is used to describe that the current key is an index value, all items in the array will be considered.

Decent type-safety is also achieved, as can be seen in this example:

the search depth is 5 layers, so x.y.z.i.j will be inferred while x.y.z.i.j.k will not

use-search-type-safety

1.2.9

2 years ago

1.2.8

2 years ago

1.2.7

2 years ago

1.2.6

2 years ago

1.2.5

2 years ago

1.2.4

2 years ago

1.2.3

2 years ago

1.2.2

2 years ago

1.2.1

2 years ago

1.2.0

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago