1. array-includes
An ES7/ES2016 spec-compliant `Array.prototype.includes` shim/polyfill/replacement that works as far down as ES3.
array-includes
Package: array-includes
Created by: es-shims
Last modified: Wed, 20 Mar 2024 20:47:32 GMT
Version: 3.1.8
License: MIT
Downloads: 105,046,094
Repository: https://github.com/es-shims/array-includes

Install

npm install array-includes
yarn add array-includes

array-includes Version Badge

github actions
coverage
dependency status
dev dependency status
License
Downloads

npm badge

An ES7/ES2016 spec-compliant Array.prototype.includes shim/polyfill/replacement that works as far down as ES3.

This package implements the es-shim API interface. It works in an ES3-supported environment and complies with the proposed spec.

Because Array.prototype.includes depends on a receiver (the this value), the main export takes the array to operate on as the first argument.

Engines that need this package include:

  • IE (all versions)
  • Safari < 9
  • Firefox < 43, and 99-101
  • Chrome < 47
  • Edge < 14
  • node < 6

Getting started

 npm install --save array-includes

Usage

Basic usage: includes(array, value[, fromIndex=0])

 var includes = require('array-includes');
var assert = require('assert');
var arr = [ 'one', 'two' ];

includes(arr, 'one'); // true
includes(arr, 'three'); // false
includes(arr, 'one', 1); // false

Example

 var arr = [
	1,
	'foo',
	NaN,
	-0
];

assert.equal(arr.indexOf(0) > -1, true);
assert.equal(arr.indexOf(-0) > -1, true);
assert.equal(includes(arr, 0), true);
assert.equal(includes(arr, -0), true);

assert.equal(arr.indexOf(NaN) > -1, false);
assert.equal(includes(arr, NaN), true);

assert.equal(includes(arr, 'foo', 0), true);
assert.equal(includes(arr, 'foo', 1), true);
assert.equal(includes(arr, 'foo', 2), false);
 /* when Array#includes is not present */
delete Array.prototype.includes;
var shimmedIncludes = includes.shim();

assert.equal(shimmedIncludes, includes.getPolyfill());
assert.equal(arr.includes('foo', 1), includes(arr, 'foo', 1));
 /* when Array#includes is present */
var shimmedIncludes = includes.shim();

assert.equal(shimmedIncludes, Array.prototype.includes);
assert.equal(arr.includes(1, 'foo'), includes(arr, 1, 'foo'));

Tests

Simply clone the repo, npm install, and run npm test

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code