npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gulp-ab-filter

v1.2.2

Published

gulp smart filter

Downloads

21

Readme

gulp-ab-filter

npm version Build status Downloads 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

// Import
const gulp = require('gulp');
const abFilter = require('gulp-ab-filter');
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:

  1. Condition depending on its type is converted into result.
  2. If the parameter branches is missing then only push obj into empty branch yes. The branch no does not exist.
  3. If result === string then push obj into branch with the name = result. If this branch not exists then push obj into branch no.
  4. If result === true then push obj into branch yes.
  5. If result === false then push obj into branch no.

Examples of usage: see example.js

1. Stream filter vinyl objects.

gulp.src('./test/**/*.txt')
	.pipe(abFilter('!**/block*')); // Exclude block files

2. Stream filter vinyl objects and handlers.

gulp.src('./test/**/*.txt')
	.pipe(abFilter('**/b*.txt', {
		end: (object, cb, obj) => {
			if (obj._result === undefined) {
				obj._result = new Vinyl({
					base: object.base,
					path: object.base + '/result.txt',
					contents: Buffer.from(object.contents)
				});
			} else {
				obj._result.contents = Buffer.concat([obj._result.contents, object.contents]);
			}
			cb(); // Don't push source files
		}, flush: (cb, obj) => {
			obj._result && obj.s.push(obj._result); // Push the result
			cb();
		}
	}));

3. Use as a separator stream vinyl objects with a standard branches yes and no.

const yes = [ // This set of plugins will be executed sequentially
	replaceP('r', '_'), // 1 gulp plugin
	(file, cb) => { // Function as 2 plugin
		// actions with the object, see examples in test.js
		file.contents = Buffer.from(String(file.contents).replace('_', '*'));
		cb(null, file); // Mandatory run the callback function
	},
	replaceP('*', '#') // 3 gulp plugin
]; // Re-use of yes is unacceptable!
const no = replaceP('b', 'B');

gulp.src('./test/**/*.txt')
	.pipe(abFilter('**/*t.txt', yes, no));

4. Separator stream vinyl objects with a standard branch yes and the handler end.

const end = (file, cb, obj) => {
	if (obj.n === 'Yes') {
		file.contents = Buffer.from(String(file.contents).replace('_', 'R'));
	} else {
		file.contents = Buffer.from(String(file.contents) + '=');
	}
	cb(null, file);
};

gulp.src('./test/**/*.txt')
	.pipe(abFilter('**/*t.txt', replaceP('r', '_'), {end: end}));

5. Use as a separator stream vinyl objects with array of named branches.

const path = require('path');
const pipe1 = (file, cb) => {
	file.contents = Buffer.from(String(file.contents) + '1');
	cb(null, file);
};
const pipe2 = (file, cb) => {
	file.contents = Buffer.from(String(file.contents) + '2');
	cb(null, file);
};

gulp.src('./test/**/*.txt')
	.pipe(abFilter(
		file => {
			const relPathParts = abFilter.relPath(file).split(path.posix.sep);
			return relPathParts.length > 2 ? relPathParts[relPathParts.length-2] : '';
		}, // get last segment of path
		[{n: 'block1', p: pipe1}, {n: 'txt', p: pipe2}]));

6. Two ways for flush stream.

See example.js

abFilter.relPath(vinyl)

Returns the relative path to the vinyl including the object name using the posix separators without the current directory.

abFilter.match(vinyl, condition [, minimathOptions])

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-filter
cd gulp-ab-filter
npm i

After you need to run the tests:

npm run -s test-src
npm run -s test-logic