1 | # array-includes <sup>[![Version Badge][npm-version-svg]][package-url]</sup>
|
---|
2 |
|
---|
3 | [![github actions][actions-image]][actions-url]
|
---|
4 | [![coverage][codecov-image]][codecov-url]
|
---|
5 | [![dependency status][deps-svg]][deps-url]
|
---|
6 | [![dev dependency status][dev-deps-svg]][dev-deps-url]
|
---|
7 | [![License][license-image]][license-url]
|
---|
8 | [![Downloads][downloads-image]][downloads-url]
|
---|
9 |
|
---|
10 | [![npm badge][npm-badge-png]][package-url]
|
---|
11 |
|
---|
12 | An ES7/ES2016 spec-compliant `Array.prototype.includes` shim/polyfill/replacement that works as far down as ES3.
|
---|
13 |
|
---|
14 | This package implements the [es-shim API](https://github.com/es-shims/api) interface. It works in an ES3-supported environment and complies with the proposed [spec](https://262.ecma-international.org/6.0/).
|
---|
15 |
|
---|
16 | Because `Array.prototype.includes` depends on a receiver (the `this` value), the main export takes the array to operate on as the first argument.
|
---|
17 |
|
---|
18 | Engines that need this package include:
|
---|
19 | - IE (all versions)
|
---|
20 | - Safari < 9
|
---|
21 | - Firefox < 43, and 99-101
|
---|
22 | - Chrome < 47
|
---|
23 | - Edge < 14
|
---|
24 | - node < 6
|
---|
25 |
|
---|
26 | ## Getting started
|
---|
27 |
|
---|
28 | ```sh
|
---|
29 | npm install --save array-includes
|
---|
30 | ```
|
---|
31 |
|
---|
32 | ## Usage
|
---|
33 |
|
---|
34 | Basic usage: **includes(array, value[, fromIndex=0])**
|
---|
35 |
|
---|
36 | ```js
|
---|
37 | var includes = require('array-includes');
|
---|
38 | var assert = require('assert');
|
---|
39 | var arr = [ 'one', 'two' ];
|
---|
40 |
|
---|
41 | includes(arr, 'one'); // true
|
---|
42 | includes(arr, 'three'); // false
|
---|
43 | includes(arr, 'one', 1); // false
|
---|
44 | ```
|
---|
45 |
|
---|
46 |
|
---|
47 |
|
---|
48 | ## Example
|
---|
49 |
|
---|
50 | ```js
|
---|
51 | var arr = [
|
---|
52 | 1,
|
---|
53 | 'foo',
|
---|
54 | NaN,
|
---|
55 | -0
|
---|
56 | ];
|
---|
57 |
|
---|
58 | assert.equal(arr.indexOf(0) > -1, true);
|
---|
59 | assert.equal(arr.indexOf(-0) > -1, true);
|
---|
60 | assert.equal(includes(arr, 0), true);
|
---|
61 | assert.equal(includes(arr, -0), true);
|
---|
62 |
|
---|
63 | assert.equal(arr.indexOf(NaN) > -1, false);
|
---|
64 | assert.equal(includes(arr, NaN), true);
|
---|
65 |
|
---|
66 | assert.equal(includes(arr, 'foo', 0), true);
|
---|
67 | assert.equal(includes(arr, 'foo', 1), true);
|
---|
68 | assert.equal(includes(arr, 'foo', 2), false);
|
---|
69 | ```
|
---|
70 |
|
---|
71 | ```js
|
---|
72 | /* when Array#includes is not present */
|
---|
73 | delete Array.prototype.includes;
|
---|
74 | var shimmedIncludes = includes.shim();
|
---|
75 |
|
---|
76 | assert.equal(shimmedIncludes, includes.getPolyfill());
|
---|
77 | assert.equal(arr.includes('foo', 1), includes(arr, 'foo', 1));
|
---|
78 | ```
|
---|
79 |
|
---|
80 | ```js
|
---|
81 | /* when Array#includes is present */
|
---|
82 | var shimmedIncludes = includes.shim();
|
---|
83 |
|
---|
84 | assert.equal(shimmedIncludes, Array.prototype.includes);
|
---|
85 | assert.equal(arr.includes(1, 'foo'), includes(arr, 1, 'foo'));
|
---|
86 | ```
|
---|
87 |
|
---|
88 | ## Tests
|
---|
89 | Simply clone the repo, `npm install`, and run `npm test`
|
---|
90 |
|
---|
91 | [package-url]: https://npmjs.org/package/array-includes
|
---|
92 | [npm-version-svg]: https://versionbadg.es/es-shims/array-includes.svg
|
---|
93 | [deps-svg]: https://david-dm.org/es-shims/array-includes.svg
|
---|
94 | [deps-url]: https://david-dm.org/es-shims/array-includes
|
---|
95 | [dev-deps-svg]: https://david-dm.org/es-shims/array-includes/dev-status.svg
|
---|
96 | [dev-deps-url]: https://david-dm.org/es-shims/array-includes#info=devDependencies
|
---|
97 | [npm-badge-png]: https://nodei.co/npm/array-includes.png?downloads=true&stars=true
|
---|
98 | [license-image]: https://img.shields.io/npm/l/array-includes.svg
|
---|
99 | [license-url]: LICENSE
|
---|
100 | [downloads-image]: https://img.shields.io/npm/dm/array-includes.svg
|
---|
101 | [downloads-url]: https://npm-stat.com/charts.html?package=array-includes
|
---|
102 | [codecov-image]: https://codecov.io/gh/es-shims/array-includes/branch/main/graphs/badge.svg
|
---|
103 | [codecov-url]: https://app.codecov.io/gh/es-shims/array-includes/
|
---|
104 | [actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/es-shims/array-includes
|
---|
105 | [actions-url]: https://github.com/es-shims/array-includes/actions
|
---|