-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
51 lines (43 loc) · 1.19 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import _ from './helper';
import { IOptions } from './interfaces';
export const search = (
keyword?: string,
collection: Array<object> = [],
options?: IOptions,
) => (new Promise((resolve, reject) => {
try {
if (
keyword
&& collection.length
) {
const results: Array<object> = [];
const { columns = [] } = options || {};
const sanitizedKeyword = _.sanitizeString(keyword);
for (let i = 0; i < collection.length; i += 1) {
const item: object = collection[i];
const keys: Array<string> = Object.keys(item);
for (let j = 0; j < keys.length; j += 1) {
const key = keys[j];
const valid = columns.length
? _.includes(columns, key)
: true;
if (
valid === true
&& typeof (item[key]) === 'string'
) {
const { result, meta } = _.isKeywordExists(item[key], sanitizedKeyword);
if (result === true) {
item['__meta'] = meta;
results.push(item);
break;
}
}
}
}
resolve(results);
}
resolve(collection);
} catch (err) {
reject(err);
}
}));