Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

array.prototype.findindex

Package Overview
Dependencies
Maintainers
2
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

array.prototype.findindex - npm Package Compare versions

Comparing version 1.0.0 to 2.0.0

.editorconfig

48

index.js

@@ -1,31 +0,23 @@

// Array.prototype.findIndex - MIT License (c) 2013 Paul Miller <http://paulmillr.com>
// For all details and docs: <https://github.com/paulmillr/Array.prototype.findIndex>
(function (globals) {
if (Array.prototype.findIndex) return;
'use strict';
var findIndex = function(predicate) {
var list = Object(this);
var length = Math.max(0, list.length) >>> 0; // ES.ToUint32;
if (length === 0) return -1;
if (typeof predicate !== 'function' || Object.prototype.toString.call(predicate) !== '[object Function]') {
throw new TypeError('Array#findIndex: predicate must be a function');
}
var thisArg = arguments.length > 1 ? arguments[1] : undefined;
for (var i = 0; i < length; i++) {
if (predicate.call(thisArg, list[i], i, list)) return i;
}
return -1;
};
var define = require('define-properties');
var ES = require('es-abstract/es6');
if (Object.defineProperty) {
try {
Object.defineProperty(Array.prototype, 'findIndex', {
value: findIndex, configurable: true, writable: true
});
} catch(e) {}
}
var implementation = require('./implementation');
var getPolyfill = require('./polyfill');
var shim = require('./shim');
if (!Array.prototype.findIndex) {
Array.prototype.findIndex = findIndex;
}
}(this));
var slice = Array.prototype.slice;
var boundShim = function findIndex(array, predicate) {
ES.RequireObjectCoercible(array);
return implementation.apply(array, predicate);
};
define(boundShim, {
implementation: implementation,
getPolyfill: getPolyfill,
shim: shim
});
module.exports = boundShim;
{
"name": "array.prototype.findindex",
"version": "1.0.0",
"description": "Array.prototype.findIndex ES6 polyfill.",
"keywords": ["Array.prototype.findIndex", "findIndex", "es6"],
"main": "index.js",
"scripts": {
"test": "mocha test.js"
},
"repository": {
"type": "git",
"url": "git://github.com/paulmillr/Array.prototype.findIndex.git"
},
"author": "Paul Miller <http://paulmillr.com>",
"license": "MIT",
"bugs": {
"url": "https://github.com/paulmillr/Array.prototype.findIndex/issues"
},
"devDependencies": {
"chai": "~1.9.1",
"mocha": "~1.21.4"
}
"name": "array.prototype.findindex",
"version": "2.0.0",
"description": "Array.prototype.findIndex ES6 polyfill.",
"keywords": [
"Array.prototype.findIndex",
"findIndex",
"es6"
],
"main": "index.js",
"scripts": {
"test": "es-shim-api --bound && mocha test.js"
},
"repository": {
"type": "git",
"url": "git://github.com/paulmillr/Array.prototype.findIndex.git"
},
"author": "Paul Miller <http://paulmillr.com>",
"license": "MIT",
"bugs": {
"url": "https://github.com/paulmillr/Array.prototype.findIndex/issues"
},
"devDependencies": {
"@es-shims/api": "^1.0.0",
"chai": "^3.5.0",
"mocha": "^2.4.5"
},
"dependencies": {
"define-properties": "^1.1.2",
"es-abstract": "^1.5.0"
}
}

@@ -9,3 +9,3 @@ # ES6 `Array.prototype.findIndex` polyfill

* Just include repo before your scripts.
* `npm install array.prototype.findIndex` if you’re using node.js.
* `npm install array.prototype.findindex` if you’re using node.js.
* `component install paulmillr/Array.prototype.findIndex` if you’re using [component(1)](https://github.com/component/component).

@@ -33,3 +33,3 @@ * `bower install Array.prototype.findIndex` if you’re using [Twitter Bower](http://bower.io).

// component(1)
require('Array.prototype.findIndex');
require('array.prototype.findindex');
```

@@ -36,0 +36,0 @@

var expect = require("chai").expect;
var arrayFindIndex = require('./');
var runArrayTests = function() {
var list = [5, 10, 15, 20];
var runTests = function(findIndex) {
var list = [5, 10, 15, 20];
describe('Array#findIndex', function() {
it('should have a length of 1', function() {
expect(Array.prototype.findIndex.length).to.equal(1);
});
describe('Array#findIndex', function() {
it('should have a length of 1', function() {
expect(findIndex.length).to.equal(1);
});
it('should find item key by predicate', function() {
var result = list.findIndex(function(item) { return item === 15; });
expect(result).to.equal(2);
});
it('should find item key by predicate', function() {
var result = findIndex.call(list, function(item) { return item === 15; });
expect(result).to.equal(2);
});
it('should return -1 when nothing matched', function() {
var result = list.findIndex(function(item) { return item === 'a'; });
expect(result).to.equal(-1);
});
it('should return -1 when nothing matched', function() {
var result = findIndex.call(list, function(item) { return item === 'a'; });
expect(result).to.equal(-1);
});
it('should throw TypeError when function was not passed', function() {
expect(function() { list.findIndex(); }).to.throw(TypeError);
});
it('should throw TypeError when function was not passed', function() {
expect(function() { list.findIndex(); }).to.throw(TypeError);
});
it('should receive all three parameters', function() {
var index = list.findIndex(function(value, index, arr) {
expect(list[index]).to.equal(value);
expect(list).to.eql(arr);
return false;
});
expect(index).to.equal(-1);
});
it('should receive all three parameters', function() {
var index = findIndex.call(list, function(value, index, arr) {
expect(list[index]).to.equal(value);
expect(list).to.eql(arr);
return false;
});
expect(index).to.equal(-1);
});
it('should work with the context argument', function() {
var context = {};
[1].findIndex(function() { expect(this).to.equal(context); }, context);
});
it('should work with the context argument', function() {
var context = {};
findIndex.call([1], function() { expect(this).to.equal(context); }, context);
});
it('should work with an array-like object', function() {
var obj = { '0': 1, '1': 2, '2': 3, length: 3 };
var foundIndex = Array.prototype.findIndex.call(obj, function(item) { return item === 2; });
expect(foundIndex).to.equal(1);
});
it('should work with an array-like object', function() {
var obj = { '0': 1, '1': 2, '2': 3, length: 3 };
var foundIndex = findIndex.call(obj, function(item) { return item === 2; });
expect(foundIndex).to.equal(1);
});
it('should work with an array-like object with negative length', function() {
var obj = { '0': 1, '1': 2, '2': 3, length: -3 };
var foundIndex = Array.prototype.findIndex.call(obj, function(item) {
throw new Error('should not reach here');
});
expect(foundIndex).to.equal(-1);
});
it('should work with an array-like object with negative length', function() {
var obj = { '0': 1, '1': 2, '2': 3, length: -3 };
var foundIndex = findIndex.call(obj, function(item) {
throw new Error('should not reach here');
});
expect(foundIndex).to.equal(-1);
});
it('should work with a sparse array', function() {
var obj = [1, , undefined];
expect(1 in obj).to.equal(false);
var seen = [];
var foundIndex = obj.findIndex(function(item, idx) {
seen.push([idx, item]);
return item === undefined && idx === 2;
});
expect(foundIndex).to.equal(2);
expect(seen).to.eql([[0, 1], [1, undefined], [2, undefined]]);
});
it('should work with a sparse array', function() {
var obj = [1, , undefined];
expect(1 in obj).to.equal(false);
var seen = [];
var foundIndex = findIndex.call(obj, function(item, idx) {
seen.push([idx, item]);
return item === undefined && idx === 2;
});
expect(foundIndex).to.equal(2);
expect(seen).to.eql([[0, 1], [1, undefined], [2, undefined]]);
});
it('should work with a sparse array-like object', function() {
var obj = { '0': 1, '2': undefined, length: 3.2 };
var seen = [];
var foundIndex = Array.prototype.findIndex.call(obj, function(item, idx) {
seen.push([idx, item]);
return false;
});
expect(foundIndex).to.equal(-1);
expect(seen).to.eql([[0, 1], [1, undefined], [2, undefined]]);
});
});
it('should work with a sparse array-like object', function() {
var obj = { '0': 1, '2': undefined, length: 3.2 };
var seen = [];
var foundIndex = findIndex.call(obj, function(item, idx) {
seen.push([idx, item]);
return false;
});
expect(foundIndex).to.equal(-1);
expect(seen).to.eql([[0, 1], [1, undefined], [2, undefined]]);
});
});
};
describe('clean Object.prototype', runArrayTests);
describe('polyfill', function() {
describe('clean Object.prototype', function() {
runTests(arrayFindIndex.implementation)
});
describe('polluted Object.prototype', function() {
Object.prototype[1] = 42;
runArrayTests();
delete Object.prototype[1];
describe('polluted Object.prototype', function() {
Object.prototype[1] = 42;
runTests(arrayFindIndex.implementation);
delete Object.prototype[1];
});
});
describe('shim', function() {
arrayFindIndex.shim();
var implementation = Array.prototype.findIndex;
describe('clean Object.prototype', function() {
runTests(implementation);
});
describe('polluted Object.prototype', function() {
Object.prototype[1] = 42;
runTests(implementation);
delete Object.prototype[1];
});
});

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc