0.2.0 • Published 9 years ago

jagrep v0.2.0

Weekly downloads
1
License
CC0-1.0
Repository
github
Last release
9 years ago

jagrep

Node lacks a grep function. It should be pointed out that jquery has a grep. For my own purposes, it was helpful from a design pattern standpoint, to have something that reminded me of perl's grep. I wanted a simple interface that provided both synchronous and asynchronous interfaces to pattern-matching list transforms, and I wanted a syntax that was familiar to me. For many users, Array.filter() will probably be sufficient.

The grep implemented in this package is not optimised for performance, and likely is not The Best Way.

Interface

// Test a list against an expression
//
var results = Jagrep.sync( { 'expression': new RegExp( '^test$' ) }, list )

// Test a list against a function
//
var results = Jagrep.sync( { 'function' : function (t) { if ( t == 'test' ) return 1 } }, list )

// Get a promise to a list containing matching results, with expression
//
var promise = Jagrep.async( { 'expression': new RegExp( '^test$' ) }, list )

// Get a promise to a list containing matching results, with function
//
Jagrep.async( { 'function' : function (t) { if ( t == 'test' ) return 1 } }, list, callback )

// Does 'value' exist in list_a? Will be true or false.
//
var exists = Jagrep.in( list_a, 'value' );

// Show me all the values from list_b that exist in list_a
//
// Note: this uses Jagrep.sync() under the covers, and is blocking.
//
var exists = Jagrep.all_in( list_a, list_b );

// Or -
//
var transformed = Jagrep.all_in( list_a, list_b ).map( function (record) {
	// Do something with the matching records
	//
	// You could also use .forEach() if that is preferred
	//

	// Just an example
	//
	var scalarised = record + '';
	return scalarised;
} )

License

This software is released under a Creative Commons "CC0" license. Basically, do what you like with it.

© @avriette / jane avriette / jane@cpan.org