An ES2015 spec-compliant Array.prototype.keys
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 spec.
Because Array.prototype.keys
depends on a receiver (the “this” value), the main export takes the array to operate on as the first argument.
var keys = require('array.prototype.keys');
var assert = require('assert');
var iterate = require('iterate-iterator');
assert.deepEqual(iterate(keys([1, 2, 3])), [0, 1, 2]);
assert.deepEqual(iterate(keys([1, 0, 1])), [0, 1, 2]);
assert.deepEqual(iterate(keys([NaN])), [0]);
assert.deepEqual(iterate(keys([1,,3])), [0, 1, 2]);
var keys = require('array.prototype.keys');
var assert = require('assert');
/* when Array#keys is not present */
delete Array.prototype.keys;
var shimmedMap = keys.shim();
assert.deepEqual(shimmedMap, keys.getPolyfill());
assert.deepEqual(iterate([1, 2, 3].keys()), [0, 1, 2]);
assert.deepEqual(iterate([1, 0, 1].keys()), [0, 1, 2]);
assert.deepEqual(iterate([NaN].keys()), [0]);
assert.deepEqual(iterate([1,,3].keys()), [0, 1, 2]);
var keys = require('array.prototype.keys');
var assert = require('assert');
/* when Array#keys is present */
var shimmedMap = keys.shim();
assert.equal(shimmedMap, Array.prototype.keys);
assert.deepEqual(iterate([1, 2, 3].keys()), [0, 1, 2]);
Simply clone the repo, npm install
, and run npm test