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

filter-array

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

filter-array - npm Package Compare versions

Comparing version 0.1.0 to 0.2.0

LICENSE

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 -->
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