# filter-files

> Recursively read directories and return a list of files, filtered to have only the files for which the (optional) filter function returns `true`. Sync and async.

Latest version **0.4.0** (published 2014-11-17) · 0 weekly downloads

## Install

```sh
npm install filter-files
pnpm add filter-files
yarn add filter-files
bun add filter-files
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.4.0 |
| Published | 2014-11-17 |
| First published | 2014-10-29 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=0.10.0 |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 9 |
| Author | Jon Schlinkert |
| Maintainers | jonschlinkert |
| Keywords | dir, directories, ext, extension, file, file-path, file-system, filepath, files, filter, fs, javascript, js, path, util, utils |

## Links

- npm: https://www.npmjs.com/package/filter-files
- Repository: https://github.com/jonschlinkert/filter-files
- Issues: https://github.com/jonschlinkert/filter-files/issues
- npm.io page: https://npm.io/package/filter-files

## Dependencies (2)

- [async](https://npm.io/package/async.md) ^0.9.0
- [is-directory](https://npm.io/package/is-directory.md) ^0.2.2

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 0.4.0 (latest) — 2014-11-17
- 0.3.1 — 2014-11-17
- 0.3.0 — 2014-11-15
- 0.2.5 — 2014-11-13
- 0.2.4 — 2014-10-30
- 0.2.3 — 2014-10-30
- 0.2.2 — 2014-10-30
- 0.2.1 — 2014-10-30
- 0.2.0 — 2014-10-30
- 0.1.0 — 2014-10-29

## README

# filter-files [![NPM version](https://badge.fury.io/js/filter-files.svg)](http://badge.fury.io/js/filter-files)

> Recursively read directories and return a list of files, filtered to have only the files for which the (optional) filter function returns `true`. Sync and async.

## Install
### Install with [npm](npmjs.org)

```bash
npm i filter-files --save
```

## Run tests

```bash
npm test
```

## Usage

```js
var filter = require('filter-files');
```

### sync

```js
filter.sync(dir, [recurse], [filterFn]);
```
**Params**

 - `dir` **{String}**: the directory to start from. Returns all files, recursively, starting with this path.
 - `filterFn` **{Function}**: optionally pass a filter function to use for filtering files/dirs. This function filters "on the fly", so recursion is very fast.
 - `recurse` **{Boolean}**: pass `false` to disable recursion.

**Examples**

```js
var files = filter.sync('lib');
console.log(files);
//=> [ 'lib/async.js', 'lib/filter.js', 'lib/sync.js' ]
```

Pass a filter function:

```js
filter.sync('lib', function(fp) {
  return /a/.test(fp);
});
//=> [ 'lib/async.js' ]
```

Or an array of filter functions:

```js
function include(fp) {
  return /^\./.test(fp);
}
function exclude(fp) {
  return !/^\.[jntv]/.test(fp);
}

// pass `false` to prevent recursion
filter('.', [include, exclude], false);
//=> ['.git', '.gitignore', '.gitattribuets']
```

### async

```js
filter(dir, [recurse], [filterFn], callback));
```
**Params**

Same as sync with the addition of `callback`.

**Examples**

```js
filter('lib', function(err, files) {
  console.log(files);
  //=> [ 'lib/async.js', 'lib/filter.js', 'lib/sync.js' ]
});
```

Pass a filter function:

```js
var fn = function(fp) {
  return /a/.test(fp);
};

filter('lib', fn, function(err, files) {
  console.log(files);
  //=> [ 'lib/async.js' ]
});
```

### Filtering

Filter functions take four parameters and return `true` or `false`.

**Params**

 - `fp` filepath being looped over
 - `dir` current directory
 - `files` accumulated array of files
 - `recurse` whether recurse is `true` or `false`

**Example**

This function returns a filter function for getting files with the given `extname`:

```js
var path = require('path');
var isDir = require('is-directory');

function ext(extname) {
  // this is our filter function
  return function filter(fp, dir, files, recurse) {
    if (isDir(path.join(dir, fp)) && recurse === true) {
      return true;
    }
    return path.extname(fp) === extname;
  }
}
```

See [the tests](./test/test.js) for more examples.


## Contributing
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/filter-files/issues).

## Author

**Jon Schlinkert**
 
+ [github/jonschlinkert](https://github.com/jonschlinkert)
+ [twitter/jonschlinkert](http://twitter.com/jonschlinkert) 

## License
Copyright (c) 2014 Jon Schlinkert  
Released under the MIT license

***

_This file was generated by [verb](https://github.com/assemble/verb) on November 17, 2014._

---
_Source: https://npm.io/package/filter-files · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
