Skip to content

Commit a1289d9

Browse files
cleanup
1 parent 0989cdc commit a1289d9

File tree

1 file changed

+103
-62
lines changed

1 file changed

+103
-62
lines changed

lib/x-ray.js

Lines changed: 103 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,12 @@ module.exports = Xray;
3636
function Xray(url) {
3737
if (!(this instanceof Xray)) return new Xray(url);
3838

39+
3940
this._paginate = false;
4041
this._limit = Infinity;
4142
this._throws = true;
43+
this._format = noop;
4244
this.selects = {};
43-
this._end = noop;
4445
this.url = url;
4546
this.keys = [];
4647
this.from = 0;
@@ -61,7 +62,100 @@ Xray.prototype.select = function(select) {
6162
}
6263

6364
/**
64-
* Run
65+
* Add a plugin
66+
*
67+
* @param {Function} fn
68+
* @retun {Xray}
69+
* @api public
70+
*/
71+
72+
Xray.prototype.use = function(fn) {
73+
fn(this);
74+
return this;
75+
}
76+
77+
/**
78+
* Throw errors or not
79+
*
80+
* @param {Boolean} throws
81+
* @return {Boolean|Xray}
82+
* @api public
83+
*/
84+
85+
Xray.prototype.throws = function(throws) {
86+
if (!arguments.length) return this._throws;
87+
this._throws = !!throws;
88+
return this;
89+
};
90+
91+
/**
92+
* Delay the next request between
93+
* `from` and `to` ms
94+
*
95+
* @param {Number} from
96+
* @param {Number} to
97+
* @return {Xray|Number}
98+
* @api public
99+
*/
100+
101+
Xray.prototype.delay = function delay(from, to) {
102+
if (arguments.length) {
103+
this.from = from;
104+
this.to = to || from;
105+
return this;
106+
} else {
107+
return Math.floor(Math.random() * this.to) + this.from;
108+
}
109+
};
110+
111+
/**
112+
* Format the output
113+
*
114+
* @param {Function} fn;
115+
* @return {Xray}
116+
* @api public
117+
*/
118+
119+
Xray.prototype.format = function(fn) {
120+
if (!arguments.length) return this._format;
121+
this._format = fn;
122+
return this;
123+
};
124+
125+
126+
/**
127+
* Paginate
128+
*
129+
* @param {String} el
130+
* @return {Xray}
131+
* @api public
132+
*/
133+
134+
Xray.prototype.paginate = function(el) {
135+
if (!arguments.length) return this._paginate;
136+
this._paginate = el;
137+
return this;
138+
};
139+
140+
/**
141+
* A maximum number of pages to traverse
142+
*
143+
* @param {Number} limit
144+
* @return {Xray}
145+
* @api public
146+
*/
147+
148+
Xray.prototype.limit = function(n) {
149+
if (!arguments.length) return this._limit;
150+
this._limit = n;
151+
return this;
152+
};
153+
154+
/**
155+
* Run `x-ray`
156+
*
157+
* @param {Function} fn
158+
* @return Xray
65159
*/
66160

67161
Xray.prototype.run = function(fn) {
@@ -70,6 +164,7 @@ Xray.prototype.run = function(fn) {
70164
var out = [];
71165

72166
this.traverse(next, done);
167+
return this;
73168

74169
// each selection
75170
function next(json) {
@@ -96,14 +191,14 @@ Xray.prototype.run = function(fn) {
96191
*
97192
* @param {String|Stream} file
98193
* @return {Stream}
194+
* @api public
99195
*/
100196

101197
Xray.prototype.write = function(file) {
102198
var stream = 'string' == typeof file ? fs.createWriteStream(file) : file;
103199
var written = false;
104200

105201
this.traverse(next, done);
106-
107202
return stream;
108203

109204
function next(json) {
@@ -129,7 +224,11 @@ Xray.prototype.write = function(file) {
129224
}
130225

131226
/**
132-
* Traverse
227+
* Traverse the pages
228+
*
229+
* @param {Function} fn
230+
* @param {Function} done
231+
* @api private
133232
*/
134233

135234
Xray.prototype.traverse = function(fn, done) {
@@ -191,61 +290,3 @@ Xray.prototype.traverse = function(fn, done) {
191290
return $;
192291
}
193292
};
194-
195-
/**
196-
* Use
197-
*/
198-
199-
Xray.prototype.use = function(fn) {
200-
fn(this);
201-
return this;
202-
}
203-
204-
/**
205-
* throws
206-
*/
207-
208-
Xray.prototype.throws = function(throws) {
209-
this._throws = !!throws;
210-
return this;
211-
};
212-
213-
/**
214-
* Delay the next request
215-
*/
216-
217-
Xray.prototype.delay = function delay(from, to) {
218-
if (arguments.length) {
219-
this.from = from;
220-
this.to = to || from;
221-
return this;
222-
} else {
223-
return Math.floor(Math.random() * this.to) + this.from;
224-
}
225-
};
226-
227-
/**
228-
* Paginate
229-
*
230-
* @param {String} el
231-
* @return {Xray}
232-
* @api public
233-
*/
234-
235-
Xray.prototype.paginate = function(el) {
236-
this._paginate = el;
237-
return this;
238-
};
239-
240-
/**
241-
* A maximum number of pages to traverse
242-
*
243-
* @param {Number} limit
244-
* @return {Xray}
245-
* @api public
246-
*/
247-
248-
Xray.prototype.limit = function(n) {
249-
this._limit = n;
250-
return this;
251-
};

0 commit comments

Comments
 (0)