Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@smakss/search

Package Overview
Dependencies
Maintainers
1
Versions
44
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@smakss/search - npm Package Compare versions

Comparing version 1.2.4 to 2.0.0-beta.0

dist/cjs/index.d.ts

57

package.json

@@ -7,23 +7,31 @@ {

"description": "Search through array and/or objects for specific keyword.",
"devDependencies": {
"@commitlint/cli": "^18.2.0",
"@commitlint/config-conventional": "^18.1.0",
"@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-typescript": "^11.1.5",
"@typescript-eslint/eslint-plugin": "^6.9.1",
"@typescript-eslint/parser": "^6.9.1",
"eslint": "^8.53.0",
"husky": "^8.0.3",
"lint-staged": "^15.0.2",
"prettier": "^3.0.3",
"rollup": "^4.3.0",
"tslib": "^2.6.2",
"typescript": "^5.2.2"
},
"engines": {
"node": ">=18.0.0"
},
"exports": {
"import": "./index.mjs",
"require": "./index.cjs"
"import": "./dist/esm/index.js",
"require": "./dist/cjs/index.js"
},
"files": ["dist"],
"homepage": "https://github.com/SMAKSS/search#readme",
"keywords": [
"npm",
"yarn",
"search",
"Array",
"Object",
"SMAKSS",
"CommonJS",
"EcmaScript",
"Nested array",
"Nested object",
"Search in arrays",
"Search in objects"
],
"keywords": ["npm", "yarn", "search", "Array", "Object", "SMAKSS", "CommonJS", "EcmaScript", "Typescript", "Nested array", "Nested object", "Search in arrays", "Search in objects"],
"license": "MIT",
"main": "./index.cjs",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"name": "@smakss/search",

@@ -34,4 +42,15 @@ "repository": {

},
"scripts": {
"format": "prettier --write \"**/*.+(js|json|yml|yaml|ts|md)\"",
"format:check": "prettier -l \"**/*.+(js|json|yml|yaml|ts|md)\"",
"generate": "rollup -c",
"generate:check": "yarn format && yarn lint && yarn typecheck",
"lint": "eslint --cache --cache-location ./node_modules/.cache/.eslintcache --ext js,ts --max-warnings=0 .",
"lint:fix": "eslint src/**/*.ts --fix",
"setup": "yarn && husky install",
"typecheck": "tsc -b ."
},
"type": "module",
"version": "1.2.4"
}
"types": "lib",
"version": "2.0.0-beta.0"
}
# Search
![npm](https://img.shields.io/npm/v/@smakss/search) ![Snyk Vulnerabilities for npm package](https://img.shields.io/snyk/vulnerabilities/npm/@smakss/search) ![NPM](https://img.shields.io/npm/l/@smakss/search) ![npm](https://img.shields.io/npm/dt/@smakss/search) ![npm bundle size (scoped)](https://img.shields.io/bundlephobia/min/@smakss/search)
![npm](https://img.shields.io/npm/v/@smakss/search) ![NPM](https://img.shields.io/npm/l/@smakss/search) ![npm](https://img.shields.io/npm/dt/@smakss/search) ![npm bundle size (scoped)](https://img.shields.io/bundlephobia/min/@smakss/search)
Searching through arrays or objects might be easy these days with array helpers like [filter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) or libraries like [\_lodash](https://www.npmjs.com/package/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.
Searching through arrays, nested arrays, or objects for specific keywords can often be cumbersome, especially when dealing with deeply nested structures. This package simplifies that process by offering a lightweight, flexible search utility that delves into every key of an object or array elements to find matches. It utilizes ES6+ syntax, so if your codebase uses older standards, a transpiler might be required.
## Demo
Check out the [working demo](https://runkit.com/smakss/5f738b7f464579001bfda2d0) on RunKit.
or
[![View @smakss/search](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/smakss-search-zlqtu3?fontsize=14&hidenavigation=1&theme=dark)
## How it works?
To install it you can simply do the following command:
To install the package:
```bash
npm i @smakss/search
or
# or
yarn add @smakss/search
```
to include it with common js module you should do this:
For CommonJS modules:
```js
var Search = require("@smakss/search");
const Search = require('@smakss/search');
```
and to include it with ECMAscript module you can simply do this one:
For ECMAScript modules:
```js
import Search from "@smakss/search";
import Search from '@smakss/search';
```
then to use it within your application you can do it just like below:
Then, to utilize the search function within your application:
The search function will accept 4 input parameter:
The `Search` function accepts an options object with the following properties:
- `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 the `keys` are included or excluded. It will be `True` by default, which means the keys will be included.
- `exact` (`Boolean`): A flag to determine whether the you are exactly looking for `searchText` string or not. It will be `False` by default, which means it will not looking for exact `searchText`. e. g. Let's say `searchText` is `5` so it will match all numbers where they include `5` _(`5`, `15`, `25`, ...)_ otherwise it will only match `5` alone. This feature is only available in `v1.0.6+`.
- `searchText` (`String`): The string to search for.
- `searchItems` (`Array|Object`): The items or structure to search within.
- `keys` (`Array`): An array of keys to include or exclude from the search.
- `include` (`Boolean`): Determines whether to include (`true`) or exclude (`false`) the keys specified. Defaults to `true`.
- `exact` (`Boolean`): Determines whether to perform an exact match search. Defaults to `false`.
## Examples of usage
## Examples of Usage
### Passing an object
### Searching Within an Object
If the matching element was in object it will return the whole object:
When the match is found in an object, the entire object is returned:
```js
const obj = { name: "John", lastName: "Doe" };
const obj = { name: 'John', lastName: 'Doe' };
Search({ searchText: "john", searchItems: obj });
// Result: [{ lastName: "Doe", name: "John" }]
const results = Search({ searchText: 'john', searchItems: [obj] });
// Results: [{ name: 'John', lastName: 'Doe' }]
```
### Passing an array
### Searching Within an Array
```js
const arr = [
{ name: "John", lastName: "Doe" },
{ name: "Joe", lastName: "Doe" },
{ name: 'John', lastName: 'Doe' },
{ name: 'Joe', lastName: 'Doe' }
];
Search({ searchText: "john", searchItems: arr });
// Result: [{ lastName: "Doe", name: "John" }]
const results = Search({ searchText: 'john', searchItems: arr });
// Results: [{ name: 'John', lastName: 'Doe' }]
```
### Passing a nested array
### Searching Within a Nested Array
```js
const arr = [
{ name: "John", lastName: "Doe" },
{ name: "Joe", lastName: "Doe" },
[{ name: "Jane", lastName: "Doe" }],
const nestedArr = [
{ name: 'John', lastName: 'Doe' },
{ name: 'Joe', lastName: 'Doe' },
[{ name: 'Jane', lastName: 'Doe' }]
];
Search({ searchText: "jane", searchItems: arr });
// Result: [{ lastName: "Doe", name: "Jane" }]
const results = Search({ searchText: 'jane', searchItems: nestedArr });
// Results: [{ name: 'Jane', lastName: 'Doe' }]
```
### Passing a nested array with including keys
### Including Specific Keys
```js
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 results = Search({
searchText: 'jane',
searchItems: nestedArr,
keys: ['name']
});
// Results: [{ name: 'Jane', lastName: 'Doe' }]
```
### Passing a nested array with excluding keys
### Excluding Specific Keys
```js
const arr = [
{ name: "John", lastName: "Doe" },
{ name: "Joe", lastName: "Doe" },
[{ name: "Jane", lastName: "Doe" }],
];
Search({
searchText: "jane",
searchItems: arr,
keys: ["name"],
include: false,
const results = Search({
searchText: 'jane',
searchItems: nestedArr,
keys: ['lastName'],
include: false
});
// Result: []
// Results: []
```
<sub>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.</sub>
_Note: The result is an empty array because 'lastName' is excluded from the search._
### Passing a nested array with exact search
### Performing an Exact Match Search
<sub>(Only available in `v1.0.6+`)</sub>
```js
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" }]
const results = Search({
searchText: 'jane',
searchItems: nestedArr,
exact: true
});
// Results: [{ name: 'Jane', lastName: 'Doe' }]
```
<sub>It will only match Jane and not Janet.</sub>
_Note: Only the exact term 'Jane' is matched, not 'Janet' or other partial matches._
## Demo
## Contributing
You can check the [working demo](https://runkit.com/smakss/5f738b7f464579001bfda2d0) in runkit.
Interested in contributing to this project? Please refer to [CONTRIBUTING.md](./CONTRIBUTING.md) for contribution guidelines.
or
## Code of Conduct
[![View @smakss/search](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/smakss-search-zlqtu3?fontsize=14&hidenavigation=1&theme=dark)
Our community prioritizes the well-being and inclusivity of all contributors and users. Please review our [Code of Conduct](./CODE_OF_CONDUCT.md) to help maintain a supportive environment for everyone.
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc