-
Notifications
You must be signed in to change notification settings - Fork 7
/
indexOf.ts
65 lines (57 loc) · 1.77 KB
/
indexOf.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright (c) 2020 Jozty. All rights reserved. MIT license.
import curryN from './utils/curry_n.ts';
import type { PH } from './utils/types.ts';
import { equals } from './equals.ts';
// @types
type IndexOf_2<T> = (list: T[]) => number;
type IndexOf_1<T> = (value: T) => number;
type IndexOf =
& (<T>(value: T) => IndexOf_2<T>)
& (<T>(value: PH, list: T[]) => IndexOf_1<T>)
& (<T>(value: T, list: T[]) => number);
function _indexOf<T>(value: T, list: T[]) {
switch (typeof value) {
case 'number': {
if (value === 0) {
// handles +0 and -0
const inf = 1 / value;
for (let i = 0; i < list.length; i++) {
const x = list[i] as unknown as number;
if (x === 0 && 1 / x === inf) return i;
}
return -1;
} else if (value !== value) {
// handles NaN
for (let i = 0; i < list.length; i++) {
const x: unknown = list[i];
if (isNaN(x as number)) return i;
}
return -1;
}
}
case 'string':
case 'boolean':
case 'function':
case 'undefined':
return list.indexOf(value);
case 'object':
if (value === null) {
return list.indexOf(value);
}
}
let idx = -1;
list.forEach((a, i) => (idx = equals(value, a) ? i : idx));
return idx;
}
/**
* Returns the position of the first occurrence of `value` in `list`, or -1
* if the item is not included in the array. [`Fae.equals`](#equals) is used to
* determine equality.
*
* Fae.indexOf(3, [1,2,3,4]); //=> 2
* Fae.indexOf(10, [1,2,3,4]); //=> -1
* Fae.indexOf(0, [1, 2, 3, 0, -0, NaN]); //=> 3
* Fae.indexOf(-0, [1, 2, 3, 0, -0, NaN]); //=> 4
* Fae.indexOf(NaN, [1, 2, 3, 0, -0, NaN]); //=> 5
*/
export const indexOf = curryN(2, _indexOf) as IndexOf;