A gulp plugin to filter files by customized filters.
$ npm install --save-dev gulp-custom-filter
var filter = require('gulp-custom-filter');
var gulp = require('gulp');
var less = require('gulp-less');
function myFilter(file) {
return file.basename.startsWith('my_');
}
gulp.task('less', function() {
return gulp.src('./less/**/*.less')
.pipe(filter(myFilter))
.pipe(less())
.pipe(gulp.dest('./css')
});
This is equivalent to:
var filter = require('gulp-custom-filter');
var gulp = require('gulp');
var less = require('gulp-less');
gulp.task('less', function() {
return gulp.src('./less/**/my_*.less')
.pipe(less())
.pipe(gulp.dest('./css')
});
You may set any predicate function to a vinyl file.
Nagates filter conditions
var not = filter.not;
gulp.src('./less/**/*.less')
.pipe(filter(not(myFilter)) // will pass files which not satisfy myFilter
This filter passes less files which its name does not start with 'my_'.
Passes files which satisfy every filter conditions.
var and = filter.and;
gulp.src('./less/**/*.less')
.pipe(filter(and(myFilter1, myFilter2)) // will pass files which satisfy myFilter1 and myFilter2
Passes files which satisfy at least one of the filter conditions.
var or = filter.or;
gulp.src('./less/**/*.less')
.pipe(filter(or(myFilter1, myFilter2)) // will pass files which satisfy myFilter1 or myFilter2
Passes every files. This is equivalent function() { return true; }
var all = filter.all;
gulp.src('./less/**/*.less')
.pipe(filter(all())) // no file will be filtered out.
Passes no file. This is equivalent function() { return false; }
var none = filter.none;
gulp.src('./less/**/*.less')
.pipe(filter(none())) // no file will be passed.
Glob pattern filter for file name. Just a wrapper of minimatch.
var glob = filter.glob;
gulp.src('./**/*.*')
.pipe(filter(glob('./**/*.less'))).
pattern
: a string or an array of glob pattern.options
: optional options.
Ignore list filter.
var ignore = filter.ignore;
gulp.src('./**/*.*')
.pipe(filter(ignore('.gitignore')))
filename
: filename to ignore file.
gulp-custom-filter
accepts three type of predicate functions.
A predicate must have one or two argument(s).
It returns a boolean value true
/false
.
gulp.src('./**/*.*')
.pipe(filter(function(file, encode) {
return file.isBuffer();
});
To fail the predicate, just throw an error.
gulp.src('./**/*.*')
.pipe(filter(function(file, encode) {
throw new Error('error');
});
A predicate must have three arguments. The third argument is a callback function to settle a result.
Call done
with the second argument of true
/false
.
gulp.src('./**/*.*')
.pipe(filter(function(file, encode, done) {
setTimeout(function() {
done(null, file.isBuffer()); // 1st argument must be falsy.
}, 0);
});
To fail the predicate, call done
with the first argument of non-falsy value.
gulp.src('./**/*.*')
.pipe(filter(function(file, encode, done) {
setTimeout(function() {
done(new Error('error'));
}, 0);
});
A predicate must have one or two argument(s).
It returns a fulfilling promise of boolean value true
/false
.
gulp.src('./**/*.*')
.pipe(filter(function(file, encode) {
return Promise.resolve(file.isBuffer());
});
To fail the predicate, return a rejecting promise.
gulp.src('./**/*.*')
.pipe(filter(function(file, encode) {
return Promise.reject(new Error('error'));
});
-
v0.4.4
- Security updates (library dependency)
-
v0.4.3
- Update library dependency
-
v0.3.0
- Update library dependency
- Update supporting version of Node.js
- Now we support Node 6 or higher
-
v0.2.5
- Update supporting version of Node.js
- Fix assertion error message of or() filter
- MIT License
- see LICENSE