forked from floatdrop/stream-assert
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
af9fe8a
commit a6c64fb
Showing
4 changed files
with
584 additions
and
674 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Oops, something went wrong.