gulp-ab-filter
Use it for filtering and separating stream vinyl objects. Easy to connect plugins in the chain for each branch. Easy create custom handlers for objects.
Installation
$ npm i -D gulp-ab-filter
API
The main module
// Importconst gulp = ;const abFilter = ;
abFilter(condition [, branches ] [, options])
/ / |
/ +-> yes +---{debug, // enable debug mode to control the route of objects
/ +-> yes, no end(vinyl, cb, obj), // main close handler for all branches
/ +-> namedBranch[] flush(cb, obj), // main flush handler for all branches
/ / | \ minimatch, // options which apply for blob condition
| {n: Name, p: Pipe, stop: Boolean} name // name for debug mode
| }
| result
+-> RegExp ---------> boolean
+-> blob -----------> boolean
+-> blob [] --------> boolean
+-> function(vinyl)
| |
| +-> string --> branch name
| +-> other ---> boolean
+-> other ----------> boolean
Condition
Possible types:
- RegExp - regular expression.
- blob string.
If blob starts with
!
this beginning is discarded, and the result is converted to the opposite. - Array blob strings.
Only works the first match, the rest are ignored.
If the array is used only blob with
!
and and there are no matches then this is equivalent to true. - Function - user-defined function with vinyl argument. If function returns the string then used it as branch name else converted to a boolean.
- Other is converted to boolean.
Notice: In the RegExp and blob is passed the function result relPath.
Branches
Parameter yes, no, namedBranch.p can be:
- gulp plugin
- function(vinyl, cb, obj)
- an array containing the gulp plugins and functions in any combination.
NamedBranch
Array from objects with properties:
- namedBranch.n - name branch
- namedBranch.p - branch
- namedBranch.stop - don'n push objects to out
Cb
Callback function that must be called with two parameters:: null or error and vinyl object. If the parameters are omitted, the object is not passed on.
Obj
A context object. It contains two properties: n - branch name and the s - link to the branch stream. Branch stream supports push. Possible to set custom properties.
End handler
Call for each branches for each objects when go out of branch.
Flush handler
Call for each branch, before closure.
The logic of the filter:
- Condition depending on its type is converted into
result
. - If the parameter branches is missing then only push obj into empty branch yes. The branch no does not exist.
- If
result
=== string then push obj into branch with the name =result
. If this branch not exists then push obj into branch no. - If
result
=== true then push obj into branch yes. - If
result
=== false then push obj into branch no.
example.js
Examples of usage: seevinyl objects.
1. Stream filtergulp ; // Exclude block files
vinyl objects and handlers.
2. Stream filtergulp ;
vinyl objects with a standard branches yes and no.
3. Use as a separator streamconst yes = // This set of plugins will be executed sequentially // 1 gulp plugin { // Function as 2 plugin // actions with the object, see examples in test.js filecontents = Buffer; ; // Mandatory run the callback function } // 3 gulp plugin; // Re-use of yes is unacceptable!const no = ; gulp ;
vinyl objects with a standard branch yes and the handler end.
4. Separator streamconst end = { if objn === 'Yes' filecontents = Buffer; else filecontents = Buffer; ;}; gulp ;
vinyl objects with array of named branches.
5. Use as a separator streamconst path = ;const pipe1 = { filecontents = Buffer; ;};const pipe2 = { filecontents = Buffer; ;}; gulp ;
6. Two ways for flush stream.
See example.js
vinyl)
abFilter.relPath(Returns the relative path to the vinyl including the object name using the posix separators without the current directory.
vinyl, condition [, minimathOptions])
abFilter.match(Returns a value which depends on the type condition.
Contribute
Please send your improvements and enhancements. To begin, you must perform preparatory steps:
git clone https://github.com/talipoff/gulp-ab-filtercd gulp-ab-filternpm i
After you need to run the tests:
npm run -s test-srcnpm run -s test-logic