filter-array
Advanced tools
Comparing version 0.1.0 to 0.2.0
55
index.js
/*! | ||
* filter-array <https://github.com/jonschlinkert/filter-array> | ||
* | ||
* Copyright (c) 2014 Jon Schlinkert, contributors. | ||
* Licensed under the MIT license. | ||
* Copyright (c) 2014-2015 Jon Schlinkert, contributors. | ||
* Licensed under the MIT License | ||
*/ | ||
@@ -10,23 +10,46 @@ | ||
var makeIterator = require('make-iterator'); | ||
var typeOf = require('kind-of'); | ||
var filter = require('arr-filter'); | ||
var mm = require('micromatch'); | ||
module.exports = function filter(arr, cb, thisArg) { | ||
cb = makeIterator(cb, thisArg); | ||
if (arr == null) { | ||
/** | ||
* Filter array against given glob | ||
* patterns, regex or given function. | ||
* | ||
* ```js | ||
* var filter = require('filter-array'); | ||
* | ||
* filter(['a', 'b', 'c', 'b', 'c', 'e'], function(ele) { | ||
* return ele === 'a' || ele === 'b'; | ||
* }); | ||
* | ||
* //=> ['a', 'b', 'b'] | ||
* ``` | ||
* | ||
* @name filterArray | ||
* @param {Array} `arr` array to filter | ||
* @param {Array|String|Function|RegExp} `filters` | ||
* @param {Object} `opts` options to pass to [micromatch] | ||
* @return {Array} | ||
* @api public | ||
*/ | ||
module.exports = function filterArray(arr, filters, opts) { | ||
if (arr.length === 0) { | ||
return []; | ||
} | ||
var len = arr.length; | ||
var i = 0; | ||
var res = []; | ||
var val; | ||
if (typeOf(filters) === 'function' || typeOf(filters) === 'regexp') { | ||
var isMatch = mm.matcher(filters, opts); | ||
while (len--) { | ||
val = arr[i++]; | ||
if (cb(val, i, arr)) { | ||
res.push(val); | ||
} | ||
return filter(arr, function _filter(val) { | ||
return isMatch(val); | ||
}); | ||
} | ||
return res; | ||
if (typeOf(filters) === 'string' || typeOf(filters) === 'array') { | ||
return filter(arr, mm.filter(filters, opts)); | ||
} | ||
return []; | ||
}; |
{ | ||
"name": "filter-array", | ||
"description": "Iterates over the elements in an array, returning an array with only the elements for which the callback returns truthy.", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"homepage": "https://github.com/jonschlinkert/filter-array", | ||
@@ -19,4 +19,5 @@ "author": { | ||
"type": "MIT", | ||
"url": "https://github.com/jonschlinkert/filter-array/blob/master/LICENSE-MIT" | ||
"url": "https://github.com/jonschlinkert/filter-array/blob/master/LICENSE" | ||
}, | ||
"files": ["index.js"], | ||
"main": "index.js", | ||
@@ -27,21 +28,22 @@ "engines": { | ||
"scripts": { | ||
"test": "mocha -R spec" | ||
"test": "mocha", | ||
"test-cov": "istanbul cover _mocha", | ||
"test-travis": "istanbul cover _mocha --report lcovonly" | ||
}, | ||
"dependencies": { | ||
"make-iterator": "^0.1.1" | ||
"arr-filter": "^1.1.0", | ||
"kind-of": "^1.1.0", | ||
"micromatch": "^2.1.0" | ||
}, | ||
"devDependencies": { | ||
"mocha": "*", | ||
"should": "*" | ||
"istanbul-harmony": "^0.3.1", | ||
"mocha": "^2.2.1" | ||
}, | ||
"keywords": [ | ||
"array", | ||
"match", | ||
"elements", | ||
"filter", | ||
"iterate", | ||
"util", | ||
"utilities", | ||
"utils", | ||
"values" | ||
"iterate" | ||
] | ||
} |
@@ -13,6 +13,19 @@ # filter-array [![NPM version](https://badge.fury.io/js/filter-array.svg)](http://badge.fury.io/js/filter-array) | ||
### [filterArray](index.js#L36) | ||
Filter array against given glob patterns, regex or given function. | ||
**Params** | ||
* `arr` **{Array}**: array to filter | ||
* `filters` **{Array|String|Function|RegExp}** | ||
* `opts` **{Object}**: options to pass to [micromatch](https://github.com/jonschlinkert/micromatch) | ||
* `returns` **{Array}** | ||
**Example** | ||
```js | ||
var filter = require('filter-array'); | ||
filter(['a', 'b', 'c', 'b', 'c', 'e'], function (ele) { | ||
filter(['a', 'b', 'c', 'b', 'c', 'e'], function(ele) { | ||
return ele === 'a' || ele === 'b'; | ||
@@ -24,9 +37,20 @@ }); | ||
## Run tests | ||
For more examples see the [tests](./test.js) | ||
## Related | ||
* [arr-filter](https://github.com/jonschlinkert/arr-filter): Faster alternative to javascript's native filter method. | ||
* [filter-object](https://github.com/jonschlinkert/filter-object): Return a copy of an object, filtered to have only keys that match the given… [more](https://github.com/jonschlinkert/filter-object) | ||
* [micromatch](https://github.com/jonschlinkert/micromatch): Glob matching for javascript/node.js. A drop-in replacement and faster alternative to minimatch and multimatch. Just… [more](https://github.com/jonschlinkert/micromatch) | ||
## Run Tests | ||
Install dev dependencies: | ||
```bash | ||
npm test | ||
npm i -d && npm test | ||
``` | ||
## Contributing | ||
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/filter-array/issues) | ||
@@ -37,12 +61,15 @@ | ||
**Jon Schlinkert** | ||
+ [github/jonschlinkert](https://github.com/jonschlinkert) | ||
+ [twitter/jonschlinkert](http://twitter.com/jonschlinkert) | ||
+ [twitter/jonschlinkert](http://twitter.com/jonschlinkert) | ||
## License | ||
Copyright (c) 2014 Jon Schlinkert | ||
Released under the MIT license | ||
Copyright (c) 2014-2015 Jon Schlinkert | ||
Released under the MIT license. | ||
*** | ||
_This file was generated by [verb](https://github.com/assemble/verb) on December 12, 2014. To update, run `npm i -g verb && verb`._ | ||
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on May 01, 2015._ | ||
<!-- reflinks generated by verb-reflinks plugin --> |
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
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
46
73
5547
3
4
+ Addedarr-filter@^1.1.0
+ Addedkind-of@^1.1.0
+ Addedmicromatch@^2.1.0
+ Addedarr-diff@2.0.0(transitive)
+ Addedarr-filter@1.1.2(transitive)
+ Addedarr-flatten@1.1.0(transitive)
+ Addedarray-unique@0.2.1(transitive)
+ Addedbraces@1.8.5(transitive)
+ Addedexpand-brackets@0.1.5(transitive)
+ Addedexpand-range@1.8.2(transitive)
+ Addedextglob@0.3.2(transitive)
+ Addedfilename-regex@2.0.1(transitive)
+ Addedfill-range@2.2.4(transitive)
+ Addedglob-base@0.3.0(transitive)
+ Addedglob-parent@2.0.0(transitive)
+ Addedis-buffer@1.1.6(transitive)
+ Addedis-dotfile@1.0.3(transitive)
+ Addedis-equal-shallow@0.1.3(transitive)
+ Addedis-extendable@0.1.1(transitive)
+ Addedis-extglob@1.0.0(transitive)
+ Addedis-glob@2.0.1(transitive)
+ Addedis-number@2.1.04.0.0(transitive)
+ Addedis-posix-bracket@0.1.1(transitive)
+ Addedis-primitive@2.0.0(transitive)
+ Addedisarray@1.0.0(transitive)
+ Addedisobject@2.1.0(transitive)
+ Addedkind-of@1.1.03.2.26.0.3(transitive)
+ Addedmake-iterator@1.0.1(transitive)
+ Addedmath-random@1.0.4(transitive)
+ Addedmicromatch@2.3.11(transitive)
+ Addednormalize-path@2.1.1(transitive)
+ Addedobject.omit@2.0.1(transitive)
+ Addedparse-glob@3.0.4(transitive)
+ Addedpreserve@0.2.0(transitive)
+ Addedrandomatic@3.1.1(transitive)
+ Addedregex-cache@0.4.4(transitive)
+ Addedremove-trailing-separator@1.1.0(transitive)
+ Addedrepeat-element@1.1.4(transitive)
+ Addedrepeat-string@1.6.1(transitive)
- Removedmake-iterator@^0.1.1
- Removedmake-iterator@0.1.1(transitive)