deepsearchjs
v1.7.15
Published
[![build](https://github.com/wise-introvert/deepsearchjs/actions/workflows/npm-publish.yml/badge.svg)](https://github.com/wise-introvert/deepsearchjs/workflows/npm-publish.yml) [![test](https://github.com/wise-introvert/deepsearchjs/actions/workflows/test
Downloads
97
Maintainers
Readme
deepsearchjs
Search for key in deeply-nested objects.
Installation
$ yarn add deepsearchjs
Usage
search<T>(object: T, any> | Array<any>, searchTerm: string): P;
| Name | Description | Required | Type |
| ------------ | -------------------------------------------------------------------------------------------------------------------------------- | -------- | ----------------------------------------------------------------------------------------------------------------- |
| object
| Object to be searched | True | T = Record<any, any> \| Array<any>
|
| searchTerm
| string to search for, a function that determines if an object key matches the query, or a regular expression to use for matching | True | string
or ((key: string, value: any) => boolean)
or RegExp
|
| output | Output object | --- | P = Record<key: string, any>
Where, key
is a valid object path(eg. a.b.c[0].d.e
, etc..) |
Parameters
obj
(T
): The object to search. It is also generic, meaning it can accept any type of object.query
(string
|((key: string, value: any) => boolean)
|RegExp
): The query to search for. This can be a string, a function, or a regular expression.- If it is a string, the function will search for keys that end with that string.
- If it is a function, it will determine if the key matches the query.
- If it is a regular expression, it will test the key against that regular
Example
index.ts
import { search } from "deepsearchjs";
import data from "./data.json";
// string as query
search(data, "city");
// call back function as query
search(data, (key: string, value: string): boolean => /city/gi.test(key));
// regex as query
search(data, /city/gi);
/*
* All three calls will have the same output:
* {
* "users[0].address.city": "Jenkinsboro",
* "users[0].address.previousAddresses[0].city": "Folsom",
* ...
* }
*/
TODO
- [ ] ci/cd: Automate versioning and releases
- [ ] feat: Optimize search algorithm to handle oversized datasets
- [ ] feat: Add a third parameter to the function to customize the search parameters ( leaf-node results only, etc... )
- [ ] feat: Search across values??
Benchmark
- text-based search x 22,188 ops/sec ±0.20% (94 runs sampled)
- regex-based search x 29,531 ops/sec ±0.11% (97 runs sampled)
- cb-based search x 9,243 ops/sec ±0.21% (96 runs sampled)
- Fastest method ( for the current release ): regex-based search :sparkles::tada:!!