Comparing version 0.1.3 to 0.1.4
56
index.js
@@ -10,2 +10,10 @@ 'use strict'; | ||
// | ||
// Generate a "random" "undefined" variable so we know when we need to ignore | ||
// a row during an assignment operation. | ||
// | ||
var undef = '_assign_undef:'+ [1, 1, 1, 1].map(function generator() { | ||
return Math.random().toString(36).substring(2).toUpperCase(); | ||
}).concat([process.pid]).join('-'); | ||
/** | ||
@@ -104,2 +112,38 @@ * Data processor. Map/Reduce as promise like api. *buzzword* | ||
/** | ||
* Filter the result out of the set. | ||
* | ||
* @param {Function} fn | ||
* @returns {Assignment} | ||
* @api public | ||
*/ | ||
Assignment.readable('filter', function setfilter(fn) { | ||
var assign = this; | ||
if (!assign.flow) return assign; | ||
/** | ||
* Simple wrapper around the actual filter function that processes the | ||
* content. | ||
* | ||
* @param {Mixed} row The data to process. | ||
* @param {Function} next Call the next flow. | ||
* @param {Function} done Fuck it, we're done. | ||
* @api private | ||
*/ | ||
function filter(row, next, done) { | ||
if (!filter.async) return next(undefined, fn(row, assign.length)); | ||
if (fn.length === 2) fn(row, next); | ||
else fn(row, assign.length, next); | ||
} | ||
filter.async = assign._async; // Should we do this async. | ||
filter.assignment = 'filter'; // Process type. | ||
assign.flow.push(filter); // Store. | ||
assign._async = false; // Reset. | ||
return assign; | ||
}); | ||
/** | ||
* Reduce the results to a single value. | ||
@@ -267,8 +311,16 @@ * | ||
if (err) return done(err); | ||
if (arguments.length === 2) row = data; | ||
if ('filter' === fn.assignment) { | ||
if (!data) row = undef; | ||
return next(); | ||
} | ||
if (arguments.length === 2 && row !== undef) { | ||
row = data; | ||
} | ||
next(); | ||
}, done, index); | ||
}, function finished(err) { | ||
assign.rows.push(row); | ||
if (undef !== row) assign.rows.push(row); | ||
done(err); | ||
@@ -275,0 +327,0 @@ }); |
{ | ||
"name": "assign", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"description": "Map/Reduce promise like returned API -- Really not way to properly describe this module..", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -44,2 +44,48 @@ describe('Assign', function () { | ||
describe('#filter', function () { | ||
it('receives the written data', function (done) { | ||
var assign = new Assignment(function (err, data) { | ||
expect(data).to.be.a('array'); | ||
expect(data).to.have.length(2); | ||
expect(data[0]).to.equal('foo'); | ||
expect(data[1]).to.equal('foo'); | ||
done(err); | ||
}); | ||
assign.filter(function map(data) { | ||
return 'foo' === data; | ||
}); | ||
assign.write('foo'); | ||
assign.write('foo'); | ||
assign.write('bar', { | ||
end: true | ||
}); | ||
}); | ||
it('allows multiple filter operations', function (done) { | ||
var assign = new Assignment(function (err, data) { | ||
expect(data).to.have.length(1); | ||
expect(data[0]).to.equal(true); | ||
done(err); | ||
}); | ||
assign.filter(Boolean); | ||
assign.filter(function map(data) { | ||
return true === data; | ||
}); | ||
assign.write(0); | ||
assign.write(1); | ||
assign.write(false); | ||
assign.write(true); | ||
assign.write(undefined); | ||
assign.write(null, { | ||
end: true | ||
}); | ||
}); | ||
}); | ||
describe('#map', function () { | ||
@@ -67,3 +113,3 @@ it('receives the written data', function (done) { | ||
it('allows multiple map sequences', function (done) { | ||
it('allows multiple map operations', function (done) { | ||
var assign = new Assignment(function (err, data) { | ||
@@ -70,0 +116,0 @@ expect(data[0]).to.equal('bar'); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15751
495