Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/xotic750/index-of-x
An extended ES6 indexOf.
https://github.com/xotic750/index-of-x
array browser ecmascript indexof nodejs
Last synced: 2 months ago
JSON representation
An extended ES6 indexOf.
- Host: GitHub
- URL: https://github.com/xotic750/index-of-x
- Owner: Xotic750
- License: mit
- Created: 2015-11-27T00:56:41.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2023-01-04T23:16:43.000Z (about 2 years ago)
- Last Synced: 2024-11-01T21:36:35.515Z (3 months ago)
- Topics: array, browser, ecmascript, indexof, nodejs
- Language: JavaScript
- Homepage:
- Size: 2.41 MB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 21
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## index-of-x
An extended ES6 indexOf.
**Version**: 2.3.0
**Author**: Xotic750
**License**: [MIT](https://opensource.org/licenses/MIT)
**Copyright**: Xotic750### `module.exports(array, searchElement, [fromIndex], [extend])` ⇒
number
⏏This method returns the first index at which a given element can be found
in the array, or -1 if it is not present.**Kind**: Exported function
**Returns**:number
- Returns index of found element, otherwise -1.
**Throws**:-
TypeError
If `array` is `null` or `undefined`.| Param | Type | Description |
| ------------- | ------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| array |Array
| The array to search. |
| searchElement |\*
| Element to locate in the `array`. |
| [fromIndex] |number
| The index to start the search at. If the index is greater than or equal to the array's length, -1 is returned, which means the array will not be searched. If the provided index value is a negative number, it is taken as the offset from the end of the array. Note: if the provided index is negative, the array is still searched from front to back. If the calculated index is less than 0, then the whole array will be searched. Default: 0 (entire array is searched). |
| [extend] |string
| Extension type: `SameValue` or `SameValueZero`. |**Example**
```js
import indexOf from 'index-of-x';const subject = [2, 3, undefined, true, 'hej', null, 2, false, 0, -0, NaN];
// Standard mode, operates just like `Array.prototype.indexOf`.
console.log(indexOf(subject, null)); // 5
console.log(indexOf(subject, '2')); // -1
console.log(indexOf(subject, NaN)); // -1
console.log(indexOf(subject, -0)); // 8
console.log(indexOf(subject, 2, 2)); //6// `SameValueZero` mode extends `indexOf` to match `NaN`.
console.log(indexOf(subject, null, 'SameValueZero')); // 5
console.log(indexOf(subject, '2', 'SameValueZero')); // -1
console.log(indexOf(subject, NaN, 'SameValueZero')); // 10
console.log(indexOf(subject, -0, 'SameValueZero')); // 8
console.log(indexOf(subject, 2, 2, 'SameValueZero')); //6// `SameValue` mode extends `indexOf` to match `NaN` and signed `0`.
console.log(indexOf(subject, null, 'SameValue')); // 5
console.log(indexOf(subject, '2', 'SameValue')); // -1
console.log(indexOf(subject, NaN, 'SameValue')); // 10
console.log(indexOf(subject, -0, 'SameValue')); // 9
console.log(indexOf(subject, 2, 2, 'SameValue')); //6
```