find-index
Advanced tools
Comparing version 0.1.1 to 1.0.0
30
index.js
@@ -1,26 +0,8 @@ | ||
function findIndex(array, predicate, self) { | ||
var len = array.length; | ||
var i; | ||
if (len === 0) return -1; | ||
if (typeof predicate !== 'function') { | ||
throw new TypeError(predicate + ' must be a function'); | ||
// Use the native ES6 `Array.findIndex` if available. | ||
if (typeof Array.prototype.findIndex === 'function') { | ||
module.exports = function (array, predicate, self) { | ||
return array.findIndex(predicate, self); | ||
} | ||
if (self) { | ||
for (i = 0; i < len; i++) { | ||
if (predicate.call(self, array[i], i, array)) { | ||
return i; | ||
} | ||
} | ||
} else { | ||
for (i = 0; i < len; i++) { | ||
if (predicate(array[i], i, array)) { | ||
return i; | ||
} | ||
} | ||
} | ||
return -1; | ||
} else { | ||
module.exports = require('./findIndex'); | ||
} | ||
module.exports = findIndex |
27
last.js
@@ -1,26 +0,1 @@ | ||
function findLastIndex(array, predicate, self) { | ||
var len = array.length; | ||
var i; | ||
if (len === 0) return -1; | ||
if (typeof predicate !== 'function') { | ||
throw new TypeError(predicate + ' must be a function'); | ||
} | ||
if (self) { | ||
for (i = len - 1; i >= 0; i--) { | ||
if (predicate.call(self, array[i], i, array)) { | ||
return i; | ||
} | ||
} | ||
} else { | ||
for (i = len - 1; i >= 0; i--) { | ||
if (predicate(array[i], i, array)) { | ||
return i; | ||
} | ||
} | ||
} | ||
return -1; | ||
} | ||
module.exports = findLastIndex | ||
module.exports = require('./findLastIndex'); |
{ | ||
"name": "find-index", | ||
"version": "0.1.1", | ||
"version": "1.0.0", | ||
"description": "finds an item in an array matching a predicate function, and returns its index", | ||
@@ -8,3 +8,8 @@ "main": "index.js", | ||
"array", | ||
"findindex" | ||
"findindex", | ||
"es6", | ||
"es2015", | ||
"ponyfill", | ||
"polyfill", | ||
"find" | ||
], | ||
@@ -16,4 +21,9 @@ "files": [ | ||
"scripts": { | ||
"test": "node test/test" | ||
"test": "tap test" | ||
}, | ||
"devDependencies": { | ||
"array-findindex": "0.1.0", | ||
"lodash.findindex": "3.2.0", | ||
"tap": "^1.0.4" | ||
}, | ||
"homepage": "https://github.com/jsdf/find-index", | ||
@@ -20,0 +30,0 @@ "bugs": "https://github.com/jsdf/find-index/issues", |
# find-index | ||
finds an item in an array matching a predicate function, | ||
and returns its index | ||
**ES6 `Array#findIndex` ponyfill** | ||
fast both when `thisArg` is used and also when it isn't: [jsPerf](http://jsperf.com/array-prototype-findindex-shims) | ||
**Ponyfill: A polyfill that doesn't overwrite the native method.** | ||
Finds an item in an array matching a predicate function, and returns its index. | ||
Fast both when `thisArg` is used and also when it isn't: | ||
[jsPerf](http://jsperf.com/array-prototype-findindex-shims) | ||
### usage | ||
```bash | ||
npm install find-index | ||
``` | ||
```js | ||
findIndex = require('find-index') | ||
findLastIndex = require('find-index/last') | ||
var findIndex = require('find-index') // will use native Array#findIndex if available. | ||
var findIndex = require('find-index/findIndex') // won't defer to native implementation | ||
var findLastIndex = require('find-index/last') // search backwards from end | ||
``` | ||
@@ -33,2 +40,2 @@ findIndex(array, callback[, thisArg]) | ||
based on [array-findindex](https://www.npmjs.org/package/array-findindex) | ||
Based on [array-findindex](https://www.npmjs.org/package/array-findindex) |
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
0
41
2339
3
9
1
1