Skip to content

Commit

Permalink
Require Node.js 8
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed May 28, 2019
1 parent 5a23670 commit fc977dc
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 69 deletions.
3 changes: 1 addition & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: node_js
node_js:
- '12'
- '10'
- '8'
- '6'
- '4'
15 changes: 7 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,32 @@ const PluginError = require('plugin-error');
const multimatch = require('multimatch');
const streamfilter = require('streamfilter');

module.exports = (pattern, options) => {
module.exports = (pattern, options = {}) => {
pattern = typeof pattern === 'string' ? [pattern] : pattern;
options = options || {};

if (!Array.isArray(pattern) && typeof pattern !== 'function') {
throw new PluginError('gulp-filter', '`pattern` should be a string, array, or function');
}

return streamfilter((file, enc, cb) => {
return streamfilter((file, encoding, callback) => {
let match;

if (typeof pattern === 'function') {
match = pattern(file);
} else {
let relPath = path.relative(file.cwd, file.path);
let relativePath = path.relative(file.cwd, file.path);

// If the path leaves the current working directory, then we need to
// resolve the absolute path so that the path can be properly matched
// by minimatch (via multimatch)
if (/^\.\.[\\/]/.test(relPath)) {
relPath = path.resolve(relPath);
if (/^\.\.[\\/]/.test(relativePath)) {
relativePath = path.resolve(relativePath);
}

match = multimatch(relPath, pattern, options).length > 0;
match = multimatch(relativePath, pattern, options).length > 0;
}

cb(!match);
callback(!match);
}, {
objectMode: true,
passthrough: options.passthrough !== false,
Expand Down
81 changes: 41 additions & 40 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,42 +1,43 @@
{
"name": "gulp-filter",
"version": "5.1.0",
"description": "Filter files in a Vinyl stream",
"license": "MIT",
"repository": "sindresorhus/gulp-filter",
"author": {
"name": "Sindre Sorhus",
"email": "[email protected]",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=4"
},
"scripts": {
"test": "xo && mocha"
},
"files": [
"index.js"
],
"keywords": [
"gulpplugin",
"filter",
"ignore",
"file",
"files",
"match",
"minimatch",
"glob",
"globbing"
],
"dependencies": {
"multimatch": "^2.0.0",
"plugin-error": "^0.1.2",
"streamfilter": "^1.0.5"
},
"devDependencies": {
"mocha": "*",
"vinyl": "^2.1.0",
"xo": "*"
}
"name": "gulp-filter",
"version": "5.1.0",
"description": "Filter files in a `vinyl` stream",
"license": "MIT",
"repository": "sindresorhus/gulp-filter",
"author": {
"name": "Sindre Sorhus",
"email": "[email protected]",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=8"
},
"scripts": {
"test": "xo && mocha"
},
"files": [
"index.js"
],
"keywords": [
"gulpplugin",
"filter",
"ignore",
"file",
"files",
"match",
"minimatch",
"glob",
"globbing",
"vinyl"
],
"dependencies": {
"multimatch": "^4.0.0",
"plugin-error": "^1.0.1",
"streamfilter": "^3.0.0"
},
"devDependencies": {
"mocha": "^6.1.4",
"vinyl": "^2.1.0",
"xo": "^0.24.0"
}
}
21 changes: 8 additions & 13 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# gulp-filter [![Build Status](https://travis-ci.org/sindresorhus/gulp-filter.svg?branch=master)](https://travis-ci.org/sindresorhus/gulp-filter)

> Filter files in a [Vinyl](https://github.com/gulpjs/vinyl) stream
> Filter files in a [`vinyl`](https://github.com/gulpjs/vinyl) stream
Enables you to work on a subset of the original files by filtering them using glob patterns. When you're done and want all the original files back you just use the `restore` stream.
Enables you to work on a subset of the original files by filtering them using glob patterns. When you're done and want all the original files back, you just use the `restore` stream.


## Install
Expand Down Expand Up @@ -114,27 +114,27 @@ gulp.task('default', () => {

### filter(pattern, [options])

Returns a [transform stream](http://nodejs.org/api/stream.html#stream_class_stream_transform) with a [.restore](#optionsrestore) property.
Returns a [transform stream](https://nodejs.org/api/stream.html#stream_class_stream_transform) with a [.restore](#optionsrestore) property.

#### pattern

Type: `string` `Array` `Function`
Type: `string | string[] | Function`

Accepts a string/array with globbing patterns which are run through [multimatch](https://github.com/sindresorhus/multimatch).

If you supply a function, you'll get a [vinyl file object](https://github.com/wearefractal/vinyl#file) as the first argument and you're expected to return a boolean of whether to include the file:
If you supply a function, you'll get a [`vinyl` file object](https://github.com/wearefractal/vinyl#file) as the first argument and you're expected to return a boolean of whether to include the file:

```js
filter(file => /unicorns/.test(file.path));
```

#### options

Type: `Object`
Type: `object`

Accepts [minimatch options](https://github.com/isaacs/minimatch#options).
Accepts [`minimatch` options](https://github.com/isaacs/minimatch#options).

*Note:* Set `dot: true` if you need to match files prefixed with a dot (e.g. `.gitignore`).
*Note:* Set `dot: true` if you need to match files prefixed with a dot, for example, `.gitignore`.

##### restore

Expand All @@ -151,8 +151,3 @@ Default: `true`
When set to `true`, filtered files are restored with a `stream.PassThrough`, otherwise, when set to `false`, filtered files are restored as a `stram.Readable`.

When the stream is a `stream.Readable`, it ends by itself, but when it's `stream.PassThrough`, you are responsible of ending the stream.


## License

MIT © [Sindre Sorhus](https://sindresorhus.com)
6 changes: 2 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
/* eslint-env mocha */
const path = require('path');
const assert = require('assert');
const {strict: assert} = require('assert');
const Vinyl = require('vinyl');
const filter = require('.');

Expand Down Expand Up @@ -90,9 +90,7 @@ describe('filter()', () => {
});

it('should filter using a function', cb => {
const stream = filter(file => {
return file.path === 'included.js';
});
const stream = filter(file => file.path === 'included.js');

const buffer = [];

Expand Down

0 comments on commit fc977dc

Please sign in to comment.