-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathsearch.js
39 lines (29 loc) · 906 Bytes
/
search.js
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
'use strict';
const { isFunction } = require('../helpers/is');
module.exports = function search(valueOrFunction, strict) {
let valueFn = valueOrFunction;
if (isFunction(valueOrFunction)) {
valueFn = this.items.find((value, key) => valueOrFunction(value, key));
}
let index = false;
if (Array.isArray(this.items)) {
const itemKey = this.items.filter((item) => {
if (strict === true) {
return item === valueFn;
}
return item === Number(valueFn) || item === String(valueFn);
})[0];
index = this.items.indexOf(itemKey);
} else {
return Object.keys(this.items).filter((prop) => {
if (strict === true) {
return this.items[prop] === valueFn;
}
return this.items[prop] === Number(valueFn) || this.items[prop] === valueFn.toString();
})[0] || false;
}
if (index === -1) {
return false;
}
return index;
};