-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Expand file tree
/
Copy path_linearSearch.js
More file actions
28 lines (27 loc) · 1.03 KB
/
_linearSearch.js
File metadata and controls
28 lines (27 loc) · 1.03 KB
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
import getLength from './_getLength.js';
import isFunction from './isFunction.js';
// Internal function for linearly iterating over arrays.
export default function linearSearch(array, predicate, dir, start) {
var target, length = getLength(array);
dir || (dir = 1);
start = (
start == null ? (dir > 0 ? 0 : length - 1) :
start < 0 ? (dir > 0 ? Math.max(0, start + length) : start + length) :
dir > 0 ? start : Math.min(start, length - 1)
);
// As a special case, in order to elide the `predicate` invocation on every
// loop iteration, we allow the caller to pass a value that should be found by
// strict equality comparison. This is somewhat like a rudimentary iteratee
// shorthand. It is used in `_.indexof` and `_.lastIndexOf`.
if (!isFunction(predicate)) {
target = predicate && predicate.value;
predicate = false;
}
for (; start >= 0 && start < length; start += dir) {
if (
predicate ? predicate(array[start], start, array) :
array[start] === target
) return start;
}
return -1;
}