search 1.2.3
Install from the command line:
Learn more about npm packages
$ npm install @SMAKSS/search@1.2.3
Install via package.json:
"@SMAKSS/search": "1.2.3"
About this version
Searching through arrays or objects might be easy these days with array helpers like filter or libraries like _lodash, but what if you want a lighter library and have a nested array of object and want to search into every key of your object with a specific keyword? This might be hard or frustrating sometimes, this package will help you to achieve search a keyword through each object key, array elements and/or nested arrays. Also, this package uses ES6+ syntax so if you using older standards for writing JS code you may need a transpiler for it.
To install it you can simply do the following command:
npm i @smakss/search
or
yarn add @smakss/search
to include it with common js module you should do this:
var Search = require('@smakss/search');
and to include it with ECMAscript module you can simply do this one:
import Search from '@smakss/search';
then to use it within your application you can do it just like below:
The search function will accept 4 input parameter:
-
searchText
(String
): The string that you want to look for in your element (It will match the whole string without regards to case sensitivity). -
searchItems
(Object|Array
): Element that you want to perform a search on it. -
keys
(Array
): Keys to include or exclude in your object items. If you exclude them it will exclude them from search and search won't perform on those specific keys. And if you include them search will only perform on those desired keys and it will ignore others. -
include
(Boolean
): A flag to determine whether thekeys
are included or excluded. It will beTrue
by default, which means the keys will be included. -
exact
(Boolean
): A flag to determine whether the you are exactly looking forsearchText
string or not. It will beFalse
by default, which means it will not looking for exactsearchText
. e. g. Let's saysearchText
is5
so it will match all numbers where they include5
(5
,15
,25
, ...) otherwise it will only match5
alone. This feature is only available inv1.0.6+
.
If the matching element was in object it will return the whole object:
const obj = { name: "John", lastName: "Doe" };
Search({ searchText: "john", searchItems: obj });
// Result: [{ lastName: "Doe", name: "John" }]
const arr = [
{ name: "John", lastName: "Doe" },
{ name: "Joe", lastName: "Doe" }
];
Search({ searchText: "john", searchItems: arr });
// Result: [{ lastName: "Doe", name: "John" }]
const arr = [
{ name: "John", lastName: "Doe" },
{ name: "Joe", lastName: "Doe" },
[{ name: "Jane", lastName: "Doe" }]
];
Search({ searchText: "jane", searchItems: arr });
// Result: [{ lastName: "Doe", name: "Jane" }]
const arr = [
{ name: "John", lastName: "Doe" },
{ name: "Joe", lastName: "Doe" },
[{ name: "Jane", lastName: "Doe" }]
];
Search({ searchText: "jane", searchItems: arr, keys: ['name'] });
// Result: [{ lastName: "Doe", name: "Jane" }]
const arr = [
{ name: "John", lastName: "Doe" },
{ name: "Joe", lastName: "Doe" },
[{ name: "Jane", lastName: "Doe" }]
];
Search({ searchText: "jane", searchItems: arr, keys: ['name'], include: false });
// Result: []
The result will be an empty array because we exclude the name
key from the search so nothing will be matched with the provided params.
(Only available in v1.0.6+
)
const arr = [
{ name: "John", lastName: "Doe" },
{ name: "Janet", lastName: "Doe" },
[{ name: "Jane", lastName: "Doe" }]
];
Search({ searchText: "jane", searchItems: arr, exact: true });
// Result: [{ name: "Jane", lastName: "Doe" }]
It will only match Jane and not Janet.
You can check the working demo in runkit.
Assets
- search-1.2.3-npm.tgz
Download activity
- Total downloads 0
- Last 30 days 0
- Last week 0
- Today 0