@@ -36,11 +36,12 @@ module.exports = Xray;
36
36
function Xray ( url ) {
37
37
if ( ! ( this instanceof Xray ) ) return new Xray ( url ) ;
38
38
39
+
39
40
this . _paginate = false ;
40
41
this . _limit = Infinity ;
41
42
this . _throws = true ;
43
+ this . _format = noop ;
42
44
this . selects = { } ;
43
- this . _end = noop ;
44
45
this . url = url ;
45
46
this . keys = [ ] ;
46
47
this . from = 0 ;
@@ -61,7 +62,100 @@ Xray.prototype.select = function(select) {
61
62
}
62
63
63
64
/**
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
65
159
*/
66
160
67
161
Xray . prototype . run = function ( fn ) {
@@ -70,6 +164,7 @@ Xray.prototype.run = function(fn) {
70
164
var out = [ ] ;
71
165
72
166
this . traverse ( next , done ) ;
167
+ return this ;
73
168
74
169
// each selection
75
170
function next ( json ) {
@@ -96,14 +191,14 @@ Xray.prototype.run = function(fn) {
96
191
*
97
192
* @param {String|Stream } file
98
193
* @return {Stream }
194
+ * @api public
99
195
*/
100
196
101
197
Xray . prototype . write = function ( file ) {
102
198
var stream = 'string' == typeof file ? fs . createWriteStream ( file ) : file ;
103
199
var written = false ;
104
200
105
201
this . traverse ( next , done ) ;
106
-
107
202
return stream ;
108
203
109
204
function next ( json ) {
@@ -129,7 +224,11 @@ Xray.prototype.write = function(file) {
129
224
}
130
225
131
226
/**
132
- * Traverse
227
+ * Traverse the pages
228
+ *
229
+ * @param {Function } fn
230
+ * @param {Function } done
231
+ * @api private
133
232
*/
134
233
135
234
Xray . prototype . traverse = function ( fn , done ) {
@@ -191,61 +290,3 @@ Xray.prototype.traverse = function(fn, done) {
191
290
return $ ;
192
291
}
193
292
} ;
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