array.prototype.findindex
Advanced tools
Comparing version 1.0.0 to 2.0.0
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 @@ |
154
test.js
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8942
11
147
2
3
1
+ Addeddefine-properties@^1.1.2
+ Addedes-abstract@^1.5.0
+ Addedarray-buffer-byte-length@1.0.1(transitive)
+ Addedarraybuffer.prototype.slice@1.0.3(transitive)
+ Addedavailable-typed-arrays@1.0.7(transitive)
+ Addedcall-bind@1.0.7(transitive)
+ Addeddata-view-buffer@1.0.1(transitive)
+ Addeddata-view-byte-length@1.0.1(transitive)
+ Addeddata-view-byte-offset@1.0.0(transitive)
+ Addeddefine-data-property@1.1.4(transitive)
+ Addeddefine-properties@1.2.1(transitive)
+ Addedes-abstract@1.23.5(transitive)
+ Addedes-define-property@1.0.0(transitive)
+ Addedes-errors@1.3.0(transitive)
+ Addedes-object-atoms@1.0.0(transitive)
+ Addedes-set-tostringtag@2.0.3(transitive)
+ Addedes-to-primitive@1.2.1(transitive)
+ Addedfor-each@0.3.3(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedfunction.prototype.name@1.1.6(transitive)
+ Addedfunctions-have-names@1.2.3(transitive)
+ Addedget-intrinsic@1.2.4(transitive)
+ Addedget-symbol-description@1.0.2(transitive)
+ Addedglobalthis@1.0.4(transitive)
+ Addedgopd@1.0.1(transitive)
+ Addedhas-bigints@1.0.2(transitive)
+ Addedhas-property-descriptors@1.0.2(transitive)
+ Addedhas-proto@1.0.3(transitive)
+ Addedhas-symbols@1.0.3(transitive)
+ Addedhas-tostringtag@1.0.2(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedinternal-slot@1.0.7(transitive)
+ Addedis-array-buffer@3.0.4(transitive)
+ Addedis-bigint@1.0.4(transitive)
+ Addedis-boolean-object@1.1.2(transitive)
+ Addedis-callable@1.2.7(transitive)
+ Addedis-data-view@1.0.1(transitive)
+ Addedis-date-object@1.0.5(transitive)
+ Addedis-negative-zero@2.0.3(transitive)
+ Addedis-number-object@1.0.7(transitive)
+ Addedis-regex@1.1.4(transitive)
+ Addedis-shared-array-buffer@1.0.3(transitive)
+ Addedis-string@1.0.7(transitive)
+ Addedis-symbol@1.0.4(transitive)
+ Addedis-typed-array@1.1.13(transitive)
+ Addedis-weakref@1.0.2(transitive)
+ Addedisarray@2.0.5(transitive)
+ Addedobject-inspect@1.13.3(transitive)
+ Addedobject-keys@1.1.1(transitive)
+ Addedobject.assign@4.1.5(transitive)
+ Addedpossible-typed-array-names@1.0.0(transitive)
+ Addedregexp.prototype.flags@1.5.3(transitive)
+ Addedsafe-array-concat@1.1.2(transitive)
+ Addedsafe-regex-test@1.0.3(transitive)
+ Addedset-function-length@1.2.2(transitive)
+ Addedset-function-name@2.0.2(transitive)
+ Addedside-channel@1.0.6(transitive)
+ Addedstring.prototype.trim@1.2.9(transitive)
+ Addedstring.prototype.trimend@1.0.8(transitive)
+ Addedstring.prototype.trimstart@1.0.8(transitive)
+ Addedtyped-array-buffer@1.0.2(transitive)
+ Addedtyped-array-byte-length@1.0.1(transitive)
+ Addedtyped-array-byte-offset@1.0.2(transitive)
+ Addedtyped-array-length@1.0.6(transitive)
+ Addedunbox-primitive@1.0.2(transitive)
+ Addedwhich-boxed-primitive@1.0.2(transitive)
+ Addedwhich-typed-array@1.1.15(transitive)