Skip to content

Commit

Permalink
reconstruct all the code.
Browse files Browse the repository at this point in the history
  • Loading branch information
huang-xiao-jian committed Dec 24, 2014
1 parent af9fe8a commit a6c64fb
Show file tree
Hide file tree
Showing 4 changed files with 584 additions and 674 deletions.
350 changes: 130 additions & 220 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,258 +1,168 @@
/**
* Expose object for assert gulp plugin file
* @modules stream-assert-gulp
* @version v0.4.7
*/

/**
* Just like normal function with gulp file argument
* @typedef {function} assertion
* @example assertion description
* function(file) {
* (file.isBuffer()).should.be.true;
* (file.isStream()).should.be.false;
* })
*/

/**
* Node transform stream object which pipable
* @typedef {object} stream
*/

/**
* Module dependency
*/
var assert = require('assert');
var through = require('through-gulp');

var streamAssert = {};
streamAssert.nth = function(index, assertion) {

/**
* Conform the transform stream assert
* @param {string} flag - generate type, accepted values 'nth', 'all', 'any'
* @param {assertion} assertion
* @param {*} additional - argument for additional action
* @returns {stream}
* @private
*/
streamAssert._origin = function(flag, assertion, additional) {
var assetStorage = []
, stream
, error;
, error
, mark;

stream = through(function (file, encoding, callback) {
if (file instanceof Error) {
error = file;
} else {
this.push(file);
assetStorage.push(file);
}
// just resolve files transfer
if (file instanceof Error) {
error = file;
} else {
this.push(file);
assetStorage.push(file);
}
callback();
}, function (callback) {
// End the stream if error had been caught
if (error instanceof Error) {
this.push(error);
this.emit('end', error);
callback();
} else {
try {
assertion(assetStorage[index-1]);
this.emit('end');
callback();
} catch (err) {
this.push(err);
this.emit('end', err);
callback();
return callback();
}

// conform the assertion
try {
switch (flag) {
case 'nth':
additional = additional === 'last' ? assetStorage.length - 1 : additional - 1;
assertion(assetStorage[additional]);
this.emit('end');
break;
case 'length':
assert.equal(assetStorage.length, additional);
this.emit('end');
break;
case 'all':
for (var i = 0; i < assetStorage.length; i++) {
assertion(assetStorage[i]);
}
this.emit('end');
break;
case 'any':
mark = assetStorage.some(function(file) {
try {
assertion(file);
return true;
} catch (err) {
error = err;
return false;
}
});
if (!mark) throw error;
this.emit('end');
break;
}
} catch(err) {
this.push(err);
this.emit('end', err);
}

callback();
});

return stream;
};

streamAssert.first = function(assertion) {
var assetStorage = []
, stream
, error;
stream = through(function (file, encoding, callback) {
if (file instanceof Error) {
error = file;
} else {
this.push(file);
assetStorage.push(file);
}
callback();
}, function (callback) {
if (error instanceof Error) {
this.push(error);
this.emit('end', error);
callback();
} else {
try {
assertion(assetStorage[0]);
this.emit('end');
callback();
} catch (err) {
this.push(err);
this.emit('end', err);
callback();
}
}
});
/**
* assert the index file in the transformed files
* @param {number} index - the number of files
* @param {assertion} assertion
* @returns {stream}
*/
streamAssert.nth = function(index, assertion) {
return streamAssert._origin('nth', assertion, index);
};

return stream;
/**
* assert the first file in the transformed files
* @param {assertion} assertion
* @returns {stream}
*/
streamAssert.first = function(assertion) {
return streamAssert.nth(1, assertion);
};

/**
* assert the second file in the transformed files
* @param {assertion} assertion
* @returns {stream}
*/
streamAssert.second = function(assertion) {
var assetStorage = []
, stream
, error;
stream = through(function (file, encoding, callback) {
if (file instanceof Error) {
error = file;
} else {
this.push(file);
assetStorage.push(file);
}
callback();
}, function (callback) {
if (error instanceof Error) {
this.push(error);
this.emit('end', error);
callback();
} else {
try {
assertion(assetStorage[1]);
this.emit('end');
callback();
} catch (err) {
this.push(err);
this.emit('end', err);
callback();
}
}
});

return stream;
return streamAssert.nth(2, assertion);
};

/**
* assert the last file in the transformed files
* @param {assertion} assertion
* @returns {stream}
*/
streamAssert.last = function(assertion) {
var assetStorage = [];
var stream;
var error;
stream = through(function (file, encoding, callback) {
if (file instanceof Error) {
error = file;
} else {
this.push(file);
assetStorage.push(file);
}
callback();
}, function (callback) {
if (error instanceof Error) {
this.push(error);
this.emit('end', error);
callback();
} else {
try {
var length = assetStorage.length;
assertion(assetStorage[length-1]);
this.emit('end');
callback();
} catch (err) {
this.push(err);
this.emit('end', err);
callback();
}
}
});

return stream;
return streamAssert.nth('last', assertion);
};

/**
* assert all the files, assert failed if any mismatch
* @param {assertion} assertion
* @returns {stream}
*/
streamAssert.all = function(assertion) {
var assetStorage = [];
var stream;
var flag;
var error;
stream = through(function (file, encoding, callback) {
if (file instanceof Error) {
error = file;
} else {
this.push(file);
assetStorage.push(file);
}
callback();
}, function (callback) {
if (error instanceof Error) {
this.push(error);
this.emit('end', error);
callback();
} else {
flag = assetStorage.every(function(file) {
try {
assertion(file);
return true;
} catch (err) {
error = err;
return false;
}
});

if (flag) {
this.emit('end');
callback();
} else {
this.push(error);
this.emit('end', error);
callback();
}
}
});

return stream;
return streamAssert._origin('all', assertion);
};

/**
* assert all the files, assert success if any match
* @param {assertion} assertion
* @returns {stream}
*/
streamAssert.any = function(assertion) {
var assetStorage = [];
var stream;
var flag;
var error;
stream = through(function (file, encoding, callback) {
if (file instanceof Error) {
error = file;
} else {
this.push(file);
assetStorage.push(file);
}
callback();
}, function (callback) {
if (error instanceof Error) {
this.push(error);
this.emit('end', error);
callback();
} else {
flag = assetStorage.some(function(file) {
try {
assertion(file);
return true;
} catch (err) {
error = err;
return false;
}
});

if (flag) {
this.emit('end');
callback();
} else {
this.push(error);
this.emit('end', error);
callback();
}
}
});

return stream;
return streamAssert._origin('any', assertion);
};

streamAssert.length = function(count) {
var assetStorage = [];
var stream;
var error;
stream = through(function (file, encoding, callback) {
if (file instanceof Error) {
error = file;
} else {
this.push(file);
assetStorage.push(file);
}
callback();
}, function (callback) {
if (error instanceof Error) {
this.push(error);
this.emit('end', error);
callback();
} else {
try {
assert.equal(assetStorage.length, count);
this.emit('end');
callback();
} catch (err) {
this.push(err);
this.emit('end', err);
callback();
}
}
});

return stream;
/**
* assert length of the transformed files
* @param {length} length - expected length
* @returns {stream}
*/
streamAssert.length = function(length) {
return streamAssert._origin('length', null, length);
};

module.exports = streamAssert;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "stream-assert-gulp",
"version": "0.4.5",
"version": "0.4.7",
"description": "simplify assertion library for gulp streams",
"main": "index.js",
"author": "bornkiller <hjj491229492@hotmail.com>",
Expand Down
Empty file added test/fixtures/many-files/16.js
Empty file.
Loading

0 comments on commit a6c64fb

Please sign in to comment.