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 2.0.1 to 2.1.0

27

dist/cjs/index.js

@@ -24,4 +24,5 @@ 'use strict';

*
* @param {SearchItem[]} results - The current list of search results.
* @param {SearchItem} item - The item to potentially add to the results.
* @template T - The type of the object to search within, extending SearchItem.
* @param {T[]} results - The current list of search results.
* @param {T} item - The item to potentially add to the results.
*

@@ -45,7 +46,8 @@ * @example

*
* @param {SearchItem} object - The object to search within.
* @template T - The type of the object to search within, extending SearchItem.
* @param {T} object - The object to search within.
* @param {string[]} keys - The keys to include or exclude in the search.
* @param {boolean} include - Whether to include or exclude the specified keys in the search.
* @param {RegExp} regex - The regex pattern to match against the object values.
* @param {SearchItem[]} results - The array to store matching objects.
* @param {T[]} results - The array to store matching objects.
*

@@ -75,3 +77,4 @@ * @example

*
* @param {SearchItem | SearchItem[]} items - The items to search through. Can be a single item or an array of items.
* @template T - The type of the items to search through, extending SearchItem.
* @param {T | T[]} items - The items to search through. Can be a single item or an array of items.
* @param {string[]} keys - The keys to include or exclude in the search.

@@ -106,3 +109,3 @@ * @param {boolean} include - Whether to include or exclude the specified keys in the search.

else if (regex.test(String(items))) {
addUniqueMatch(results, [items]);
addUniqueMatch(results, items);
}

@@ -114,9 +117,11 @@ }

*
* @param {Partial<SearchOptions>} options - The search parameters including searchText, searchItems, keys to search in,
* @param {SearchOptions} options - The search parameters including searchText, searchItems, keys to search in,
* whether to include keys and if the search is exact.
* @returns {SearchItem[]} The matched items as an array.
* @template T - The type of the items to search through, extending SearchItem.
* @returns {T[]} The matched items as an array.
*
* @example
* type Person = { name: string; lastName: string; }
* // Define a list of objects to search
* const people = [
* const people: Person[] = [
* { name: "John", lastName: "Doe" },

@@ -136,3 +141,3 @@ * { name: "Jane", lastName: "Smith" },

* // Perform the search
* const found = search(options);
* const found = search<Person>(options);
*

@@ -142,3 +147,3 @@ * // found will contain the object with lastName 'Doe'

*/
function search({ searchText = '', searchItems = [], keys = [], include = true, exact = false }) {
function search({ searchText, searchItems, keys = [], include = true, exact = false }) {
const regex = new RegExp(exact ? `^${searchText}$` : searchText, 'i');

@@ -145,0 +150,0 @@ const results = [];

@@ -1,2 +0,2 @@

import { SearchItem } from './types';
import type { SearchItem, KeyOf } from './types';
/**

@@ -6,7 +6,8 @@ * Searches for matches within an object based on a regex pattern.

*
* @param {SearchItem} object - The object to search within.
* @template T - The type of the object to search within, extending SearchItem.
* @param {T} object - The object to search within.
* @param {string[]} keys - The keys to include or exclude in the search.
* @param {boolean} include - Whether to include or exclude the specified keys in the search.
* @param {RegExp} regex - The regex pattern to match against the object values.
* @param {SearchItem[]} results - The array to store matching objects.
* @param {T[]} results - The array to store matching objects.
*

@@ -24,3 +25,3 @@ * @example

*/
export declare function searchWithinObject(object: SearchItem, keys: string[], include: boolean, regex: RegExp, results: SearchItem[]): void;
export declare function searchWithinObject<T extends SearchItem>(object: T, keys: KeyOf<T>[], include: boolean, regex: RegExp, results: T[]): void;
/**

@@ -30,3 +31,4 @@ * Recursively searches through items for matches based on a regex pattern.

*
* @param {SearchItem | SearchItem[]} items - The items to search through. Can be a single item or an array of items.
* @template T - The type of the items to search through, extending SearchItem.
* @param {T | T[]} items - The items to search through. Can be a single item or an array of items.
* @param {string[]} keys - The keys to include or exclude in the search.

@@ -51,2 +53,2 @@ * @param {boolean} include - Whether to include or exclude the specified keys in the search.

*/
export declare function recursiveSearch(items: SearchItem | SearchItem[], keys: string[], include: boolean, regex: RegExp, results: SearchItem[]): void;
export declare function recursiveSearch<T extends SearchItem>(items: T | T[], keys: KeyOf<T>[], include: boolean, regex: RegExp, results: T[]): void;

@@ -1,12 +0,14 @@

import { SearchItem, SearchOptions } from './types';
import type { SearchItem, SearchOptions } from './types';
/**
* Searches for items within a collection that match the given search text.
*
* @param {Partial<SearchOptions>} options - The search parameters including searchText, searchItems, keys to search in,
* @param {SearchOptions} options - The search parameters including searchText, searchItems, keys to search in,
* whether to include keys and if the search is exact.
* @returns {SearchItem[]} The matched items as an array.
* @template T - The type of the items to search through, extending SearchItem.
* @returns {T[]} The matched items as an array.
*
* @example
* type Person = { name: string; lastName: string; }
* // Define a list of objects to search
* const people = [
* const people: Person[] = [
* { name: "John", lastName: "Doe" },

@@ -26,3 +28,3 @@ * { name: "Jane", lastName: "Smith" },

* // Perform the search
* const found = search(options);
* const found = search<Person>(options);
*

@@ -32,3 +34,3 @@ * // found will contain the object with lastName 'Doe'

*/
declare function search({ searchText, searchItems, keys, include, exact }: Partial<SearchOptions>): SearchItem[];
declare function search<T extends SearchItem = SearchItem>({ searchText, searchItems, keys, include, exact }: SearchOptions<T>): T[];
export default search;

@@ -24,8 +24,13 @@ /**

*/
export interface SearchOptions {
export interface SearchOptions<T extends SearchItem> {
searchText: string;
searchItems: SearchItem | SearchItem[];
keys: string[];
include: boolean;
exact: boolean;
searchItems: T | T[];
keys?: KeyOf<T>[];
include?: boolean;
exact?: boolean;
}
/**
* Represents a key of a given object type.
* @alias keyof
*/
export type KeyOf<T> = keyof T;

@@ -1,2 +0,2 @@

import { SearchItem } from './types';
import type { SearchItem, KeyOf } from './types';
/**

@@ -15,8 +15,9 @@ * Determines whether a given key is included in the search based on the include parameter and the keys provided.

*/
export declare function isKeyIncluded(key: string, keys: string[], include: boolean): boolean;
export declare function isKeyIncluded<T extends SearchItem>(key: KeyOf<T>, keys: KeyOf<T>[], include: boolean): boolean;
/**
* Adds a search item to the results if it's not already included.
*
* @param {SearchItem[]} results - The current list of search results.
* @param {SearchItem} item - The item to potentially add to the results.
* @template T - The type of the object to search within, extending SearchItem.
* @param {T[]} results - The current list of search results.
* @param {T} item - The item to potentially add to the results.
*

@@ -30,2 +31,2 @@ * @example

*/
export declare function addUniqueMatch(results: SearchItem[], item: SearchItem): void;
export declare function addUniqueMatch<T extends SearchItem>(results: T[], item: T): void;

@@ -20,4 +20,5 @@ /**

*
* @param {SearchItem[]} results - The current list of search results.
* @param {SearchItem} item - The item to potentially add to the results.
* @template T - The type of the object to search within, extending SearchItem.
* @param {T[]} results - The current list of search results.
* @param {T} item - The item to potentially add to the results.
*

@@ -41,7 +42,8 @@ * @example

*
* @param {SearchItem} object - The object to search within.
* @template T - The type of the object to search within, extending SearchItem.
* @param {T} object - The object to search within.
* @param {string[]} keys - The keys to include or exclude in the search.
* @param {boolean} include - Whether to include or exclude the specified keys in the search.
* @param {RegExp} regex - The regex pattern to match against the object values.
* @param {SearchItem[]} results - The array to store matching objects.
* @param {T[]} results - The array to store matching objects.
*

@@ -71,3 +73,4 @@ * @example

*
* @param {SearchItem | SearchItem[]} items - The items to search through. Can be a single item or an array of items.
* @template T - The type of the items to search through, extending SearchItem.
* @param {T | T[]} items - The items to search through. Can be a single item or an array of items.
* @param {string[]} keys - The keys to include or exclude in the search.

@@ -102,3 +105,3 @@ * @param {boolean} include - Whether to include or exclude the specified keys in the search.

else if (regex.test(String(items))) {
addUniqueMatch(results, [items]);
addUniqueMatch(results, items);
}

@@ -110,9 +113,11 @@ }

*
* @param {Partial<SearchOptions>} options - The search parameters including searchText, searchItems, keys to search in,
* @param {SearchOptions} options - The search parameters including searchText, searchItems, keys to search in,
* whether to include keys and if the search is exact.
* @returns {SearchItem[]} The matched items as an array.
* @template T - The type of the items to search through, extending SearchItem.
* @returns {T[]} The matched items as an array.
*
* @example
* type Person = { name: string; lastName: string; }
* // Define a list of objects to search
* const people = [
* const people: Person[] = [
* { name: "John", lastName: "Doe" },

@@ -132,3 +137,3 @@ * { name: "Jane", lastName: "Smith" },

* // Perform the search
* const found = search(options);
* const found = search<Person>(options);
*

@@ -138,3 +143,3 @@ * // found will contain the object with lastName 'Doe'

*/
function search({ searchText = '', searchItems = [], keys = [], include = true, exact = false }) {
function search({ searchText, searchItems, keys = [], include = true, exact = false }) {
const regex = new RegExp(exact ? `^${searchText}$` : searchText, 'i');

@@ -141,0 +146,0 @@ const results = [];

@@ -1,2 +0,2 @@

import { SearchItem } from './types';
import type { SearchItem, KeyOf } from './types';
/**

@@ -6,7 +6,8 @@ * Searches for matches within an object based on a regex pattern.

*
* @param {SearchItem} object - The object to search within.
* @template T - The type of the object to search within, extending SearchItem.
* @param {T} object - The object to search within.
* @param {string[]} keys - The keys to include or exclude in the search.
* @param {boolean} include - Whether to include or exclude the specified keys in the search.
* @param {RegExp} regex - The regex pattern to match against the object values.
* @param {SearchItem[]} results - The array to store matching objects.
* @param {T[]} results - The array to store matching objects.
*

@@ -24,3 +25,3 @@ * @example

*/
export declare function searchWithinObject(object: SearchItem, keys: string[], include: boolean, regex: RegExp, results: SearchItem[]): void;
export declare function searchWithinObject<T extends SearchItem>(object: T, keys: KeyOf<T>[], include: boolean, regex: RegExp, results: T[]): void;
/**

@@ -30,3 +31,4 @@ * Recursively searches through items for matches based on a regex pattern.

*
* @param {SearchItem | SearchItem[]} items - The items to search through. Can be a single item or an array of items.
* @template T - The type of the items to search through, extending SearchItem.
* @param {T | T[]} items - The items to search through. Can be a single item or an array of items.
* @param {string[]} keys - The keys to include or exclude in the search.

@@ -51,2 +53,2 @@ * @param {boolean} include - Whether to include or exclude the specified keys in the search.

*/
export declare function recursiveSearch(items: SearchItem | SearchItem[], keys: string[], include: boolean, regex: RegExp, results: SearchItem[]): void;
export declare function recursiveSearch<T extends SearchItem>(items: T | T[], keys: KeyOf<T>[], include: boolean, regex: RegExp, results: T[]): void;

@@ -1,12 +0,14 @@

import { SearchItem, SearchOptions } from './types';
import type { SearchItem, SearchOptions } from './types';
/**
* Searches for items within a collection that match the given search text.
*
* @param {Partial<SearchOptions>} options - The search parameters including searchText, searchItems, keys to search in,
* @param {SearchOptions} options - The search parameters including searchText, searchItems, keys to search in,
* whether to include keys and if the search is exact.
* @returns {SearchItem[]} The matched items as an array.
* @template T - The type of the items to search through, extending SearchItem.
* @returns {T[]} The matched items as an array.
*
* @example
* type Person = { name: string; lastName: string; }
* // Define a list of objects to search
* const people = [
* const people: Person[] = [
* { name: "John", lastName: "Doe" },

@@ -26,3 +28,3 @@ * { name: "Jane", lastName: "Smith" },

* // Perform the search
* const found = search(options);
* const found = search<Person>(options);
*

@@ -32,3 +34,3 @@ * // found will contain the object with lastName 'Doe'

*/
declare function search({ searchText, searchItems, keys, include, exact }: Partial<SearchOptions>): SearchItem[];
declare function search<T extends SearchItem = SearchItem>({ searchText, searchItems, keys, include, exact }: SearchOptions<T>): T[];
export default search;

@@ -24,8 +24,13 @@ /**

*/
export interface SearchOptions {
export interface SearchOptions<T extends SearchItem> {
searchText: string;
searchItems: SearchItem | SearchItem[];
keys: string[];
include: boolean;
exact: boolean;
searchItems: T | T[];
keys?: KeyOf<T>[];
include?: boolean;
exact?: boolean;
}
/**
* Represents a key of a given object type.
* @alias keyof
*/
export type KeyOf<T> = keyof T;

@@ -1,2 +0,2 @@

import { SearchItem } from './types';
import type { SearchItem, KeyOf } from './types';
/**

@@ -15,8 +15,9 @@ * Determines whether a given key is included in the search based on the include parameter and the keys provided.

*/
export declare function isKeyIncluded(key: string, keys: string[], include: boolean): boolean;
export declare function isKeyIncluded<T extends SearchItem>(key: KeyOf<T>, keys: KeyOf<T>[], include: boolean): boolean;
/**
* Adds a search item to the results if it's not already included.
*
* @param {SearchItem[]} results - The current list of search results.
* @param {SearchItem} item - The item to potentially add to the results.
* @template T - The type of the object to search within, extending SearchItem.
* @param {T[]} results - The current list of search results.
* @param {T} item - The item to potentially add to the results.
*

@@ -30,2 +31,2 @@ * @example

*/
export declare function addUniqueMatch(results: SearchItem[], item: SearchItem): void;
export declare function addUniqueMatch<T extends SearchItem>(results: T[], item: T): void;

@@ -8,16 +8,20 @@ {

"devDependencies": {
"@commitlint/cli": "^18.4.3",
"@commitlint/config-conventional": "^18.4.3",
"@commitlint/cli": "^19.2.1",
"@commitlint/config-conventional": "^19.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.16.0",
"@typescript-eslint/parser": "^6.16.0",
"@rollup/plugin-typescript": "^11.1.6",
"@types/jest": "^29.5.12",
"@types/node": "^20.12.5",
"@typescript-eslint/eslint-plugin": "^7.5.0",
"@typescript-eslint/parser": "^7.5.0",
"eslint": "^8.56.0",
"husky": "^8.0.3",
"lint-staged": "^15.2.0",
"prettier": "^3.1.1",
"rollup": "^4.9.1",
"husky": "^9.0.11",
"jest": "^29.7.0",
"lint-staged": "^15.2.2",
"prettier": "^3.2.5",
"rollup": "^4.14.1",
"ts-jest": "^29.1.2",
"tslib": "^2.6.2",
"typescript": "^5.3.3"
"typescript": "^5.4.4"
},

@@ -71,7 +75,9 @@ "engines": {

"setup": "yarn && husky install",
"typecheck": "tsc -b ."
"test": "jest",
"typecheck": "tsc -b .",
"upgrade:deps": "rm -rf node_modules yarn.lock && ncu -u && yarn"
},
"type": "module",
"types": "lib",
"version": "2.0.1"
"version": "2.1.0"
}

@@ -45,4 +45,12 @@ # Search

The search function can take a generic type to specify the type of the search items.
## Examples of Usage
Lets suppose we have the following type:
```ts
type Person = { name: string; lastName: string };
```
### Searching Within an Object

@@ -52,6 +60,6 @@

```js
const obj = { name: 'John', lastName: 'Doe' };
```ts
const obj: Person = { name: 'John', lastName: 'Doe' };
const results = Search({ searchText: 'john', searchItems: [obj] });
const results = Search<Person>({ searchText: 'john', searchItems: [obj] });
// Results: [{ name: 'John', lastName: 'Doe' }]

@@ -62,4 +70,4 @@ ```

```js
const arr = [
```ts
const arr: Person[] = [
{ name: 'John', lastName: 'Doe' },

@@ -69,3 +77,3 @@ { name: 'Joe', lastName: 'Doe' }

const results = Search({ searchText: 'john', searchItems: arr });
const results = Search<Person>({ searchText: 'john', searchItems: arr });
// Results: [{ name: 'John', lastName: 'Doe' }]

@@ -76,4 +84,4 @@ ```

```js
const nestedArr = [
```ts
const nestedArr: (Person | Person[])[] = [
{ name: 'John', lastName: 'Doe' },

@@ -84,3 +92,6 @@ { name: 'Joe', lastName: 'Doe' },

const results = Search({ searchText: 'jane', searchItems: nestedArr });
const results = Search<Person | Person[]>({
searchText: 'jane',
searchItems: nestedArr
});
// Results: [{ name: 'Jane', lastName: 'Doe' }]

@@ -91,4 +102,4 @@ ```

```js
const results = Search({
```ts
const results = Search<Person>({
searchText: 'jane',

@@ -103,4 +114,4 @@ searchItems: nestedArr,

```js
const results = Search({
```ts
const results = Search<Person>({
searchText: 'jane',

@@ -118,4 +129,4 @@ searchItems: nestedArr,

```js
const results = Search({
```ts
const results = Search<Person>({
searchText: 'jane',

@@ -122,0 +133,0 @@ searchItems: nestedArr,

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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