Skip to content

Commit

Permalink
fix(core): fixing the functionality phase I
Browse files Browse the repository at this point in the history
  • Loading branch information
SMAKSS committed Nov 4, 2023
1 parent 7333e3f commit 6eb26c4
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 29 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,5 @@
},
"type": "module",
"types": "lib",
"version": "2.0.0-beta.0"
"version": "2.0.0-beta.1"
}
17 changes: 10 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { matchesRegex, addIfUnique } from './utils';
/**
* The SearchOptions interface represents the structure for search configuration.
*/
interface SearchOptions<T> {
interface SearchOptions {
searchText: string;
searchItems: Array<SearchItem<T>>;
searchItems: SearchItem[];
keys: string[];
include: boolean;
exact: boolean;
Expand Down Expand Up @@ -37,21 +37,24 @@ interface SearchOptions<T> {
* });
* // results will be [{ name: 'Jane Doe' }]
*/
function search<T>({

function search({
searchText = '',
searchItems = [],
keys = [],
include = true,
exact = false
}: Partial<SearchOptions<T>>): Array<SearchItem<T>> {
}: Partial<SearchOptions>): SearchItem[] {
const regex = exact
? new RegExp(`^${searchText}$`, 'i')
: new RegExp(searchText, 'i');
const filtered: Array<SearchItem<T>> = [];
const filtered: SearchItem[] = [];

searchItems.forEach((item) => {
if (matchesRegex(item, regex)) {
addIfUnique(filtered, item);
if (typeof item === 'string') {
if (matchesRegex(item, regex)) {
addIfUnique(filtered, item);
}
} else if (Array.isArray(item)) {
searchInArray(item, filtered, keys, include, regex);
} else if (typeof item === 'object' && item !== null) {
Expand Down
25 changes: 12 additions & 13 deletions src/search-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,22 @@ import { addIfUnique, matchesRegex, shouldIncludeKey } from './utils';
* searchInObject(obj, filtered, ['name'], true, /John/);
* // filtered now contains [{ name: 'John', age: 30 }]
*/
export function searchInObject<T>(
obj: SearchItem<T>,
filtered: SearchItem<T>[],
export function searchInObject(
obj: SearchItem,
filtered: SearchItem[],
keys: string[],
include: boolean,
regex: RegExp
): void {
Object.entries(obj).forEach(([key, value]) => {
if (shouldIncludeKey(keys, key, include)) {
if (matchesRegex(value, regex)) {
for (const [key, value] of Object.entries(obj)) {
if (shouldIncludeKey(key, keys, include)) {
if (typeof value === 'string' && matchesRegex(value, regex)) {
addIfUnique(filtered, obj);
} else if (Array.isArray(value)) {
// This function call was previously incorrect; it should be "value" instead of "obj".
searchInArray(value, filtered, keys, include, regex);
}
}
});
}
}

/**
Expand All @@ -54,20 +53,20 @@ export function searchInObject<T>(
* searchInArray(items, filtered, ['name'], true, /Jane/);
* // filtered now contains [{ name: 'Jane' }]
*/
export function searchInArray<T>(
arr: Array<SearchItem<T>>,
filtered: SearchItem<T>[],
export function searchInArray(
arr: SearchItem[],
filtered: SearchItem[],
keys: string[],
include: boolean,
regex: RegExp
): void {
arr.forEach((item) => {
for (const item of arr) {
if (typeof item === 'string' && matchesRegex(item, regex)) {
addIfUnique(filtered, item);
} else if (Array.isArray(item)) {
searchInArray(item, filtered, keys, include, regex);
} else if (typeof item === 'object' && item !== null) {
searchInObject(item, filtered, keys, include, regex);
}
});
}
}
4 changes: 1 addition & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export type SearchItem<T> = T & {
[key: string]: string | SearchItem<T> | Array<SearchItem<T>>;
};
export type SearchItem = Record<string, unknown>;
7 changes: 2 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ export function matchesRegex(value: unknown, regex: RegExp): boolean {
* shouldIncludeKey(['name', 'age'], 'location', true);
*/
export function shouldIncludeKey(
keys: string[],
key: string,
keys: string[],
include: boolean
): boolean {
return include ? keys.includes(key) : !keys.includes(key);
Expand All @@ -59,10 +59,7 @@ export function shouldIncludeKey(
* const filtered = [{name: 'John'}];
* addIfUnique(filtered, {name: 'John'});
*/
export function addIfUnique<T>(
filtered: SearchItem<T>[],
item: SearchItem<T>
): void {
export function addIfUnique(filtered: SearchItem[], item: SearchItem): void {
if (!filtered.some((el) => Object.is(el, item))) {
filtered.push(item);
}
Expand Down

0 comments on commit 6eb26c4

Please sign in to comment.