From a1289d98de905388cb1d6c0ca25fe950b39326bf Mon Sep 17 00:00:00 2001 From: Matthew Mueller Date: Wed, 4 Feb 2015 02:31:48 -0800 Subject: [PATCH] cleanup --- lib/x-ray.js | 165 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 103 insertions(+), 62 deletions(-) diff --git a/lib/x-ray.js b/lib/x-ray.js index 56f3938..cc76ff3 100644 --- a/lib/x-ray.js +++ b/lib/x-ray.js @@ -36,11 +36,12 @@ module.exports = Xray; function Xray(url) { if (!(this instanceof Xray)) return new Xray(url); + this._paginate = false; this._limit = Infinity; this._throws = true; + this._format = noop; this.selects = {}; - this._end = noop; this.url = url; this.keys = []; this.from = 0; @@ -61,7 +62,100 @@ Xray.prototype.select = function(select) { } /** - * Run + * Add a plugin + * + * @param {Function} fn + * @retun {Xray} + * @api public + */ + +Xray.prototype.use = function(fn) { + fn(this); + return this; +} + +/** + * Throw errors or not + * + * @param {Boolean} throws + * @return {Boolean|Xray} + * @api public + */ + +Xray.prototype.throws = function(throws) { + if (!arguments.length) return this._throws; + this._throws = !!throws; + return this; +}; + +/** + * Delay the next request between + * `from` and `to` ms + * + * @param {Number} from + * @param {Number} to + * @return {Xray|Number} + * @api public + */ + +Xray.prototype.delay = function delay(from, to) { + if (arguments.length) { + this.from = from; + this.to = to || from; + return this; + } else { + return Math.floor(Math.random() * this.to) + this.from; + } +}; + +/** + * Format the output + * + * @param {Function} fn; + * @return {Xray} + * @api public + */ + +Xray.prototype.format = function(fn) { + if (!arguments.length) return this._format; + this._format = fn; + return this; +}; + + +/** + * Paginate + * + * @param {String} el + * @return {Xray} + * @api public + */ + +Xray.prototype.paginate = function(el) { + if (!arguments.length) return this._paginate; + this._paginate = el; + return this; +}; + +/** + * A maximum number of pages to traverse + * + * @param {Number} limit + * @return {Xray} + * @api public + */ + +Xray.prototype.limit = function(n) { + if (!arguments.length) return this._limit; + this._limit = n; + return this; +}; + +/** + * Run `x-ray` + * + * @param {Function} fn + * @return Xray */ Xray.prototype.run = function(fn) { @@ -70,6 +164,7 @@ Xray.prototype.run = function(fn) { var out = []; this.traverse(next, done); + return this; // each selection function next(json) { @@ -96,6 +191,7 @@ Xray.prototype.run = function(fn) { * * @param {String|Stream} file * @return {Stream} + * @api public */ Xray.prototype.write = function(file) { @@ -103,7 +199,6 @@ Xray.prototype.write = function(file) { var written = false; this.traverse(next, done); - return stream; function next(json) { @@ -129,7 +224,11 @@ Xray.prototype.write = function(file) { } /** - * Traverse + * Traverse the pages + * + * @param {Function} fn + * @param {Function} done + * @api private */ Xray.prototype.traverse = function(fn, done) { @@ -191,61 +290,3 @@ Xray.prototype.traverse = function(fn, done) { return $; } }; - -/** - * Use - */ - -Xray.prototype.use = function(fn) { - fn(this); - return this; -} - -/** - * throws - */ - -Xray.prototype.throws = function(throws) { - this._throws = !!throws; - return this; -}; - -/** - * Delay the next request - */ - -Xray.prototype.delay = function delay(from, to) { - if (arguments.length) { - this.from = from; - this.to = to || from; - return this; - } else { - return Math.floor(Math.random() * this.to) + this.from; - } -}; - -/** - * Paginate - * - * @param {String} el - * @return {Xray} - * @api public - */ - -Xray.prototype.paginate = function(el) { - this._paginate = el; - return this; -}; - -/** - * A maximum number of pages to traverse - * - * @param {Number} limit - * @return {Xray} - * @api public - */ - -Xray.prototype.limit = function(n) { - this._limit = n; - return this; -};