Skip to content

Commit 4cf1897

Browse files
add xray.prepare(str, fn) for custom 'title | uppercase' filters
1 parent d62f2a0 commit 4cf1897

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

lib/x-ray.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44

55
var debug = require('debug')('x-ray');
6+
var assign = require('object-assign');
67
var Select = require('x-ray-select');
78
var request = require('./request');
89
var yieldly = require('yieldly');
@@ -41,6 +42,7 @@ function Xray(url) {
4142
this._paginate = false;
4243
this._limit = Infinity;
4344
this._throws = true;
45+
this.prepares = {};
4446
this.selects = {};
4547
this.url = url;
4648
this.keys = [];
@@ -122,6 +124,23 @@ Xray.prototype.format = function(fn) {
122124
return this;
123125
};
124126

127+
/**
128+
* Prepare
129+
*/
130+
131+
Xray.prototype.prepare = function(str, fn) {
132+
if (!arguments.length) return this.prepares;
133+
134+
if (1 == arguments.length) {
135+
this.prepares = assign(this.prepares, str);
136+
} else {
137+
this.prepares[str] = fn;
138+
}
139+
140+
return this;
141+
};
142+
143+
125144

126145
/**
127146
* Paginate
@@ -255,6 +274,7 @@ Xray.prototype.traverse = function(fn, done) {
255274
var get = this.request ? this.request : (this.request = this.use(request()).request);
256275
var limit = this._paginate ? this._limit : 1;
257276
var paginate = this._paginate;
277+
var prepares = this.prepares;
258278
var selects = this.selects;
259279
var throws = this._throws;
260280
var format = this._format;
@@ -270,7 +290,7 @@ Xray.prototype.traverse = function(fn, done) {
270290
debug('received response');
271291

272292
var $ = load(body);
273-
var select = Select($);
293+
var select = Select($, prepares);
274294
var json = select(selects);
275295
var href = select(paginate);
276296

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
"name": "x-ray",
33
"version": "1.0.2",
44
"description": "structure any website",
5-
"keywords": ["scraper", "cheerio", "web"],
5+
"keywords": [
6+
"scraper",
7+
"cheerio",
8+
"web"
9+
],
610
"author": "Matthew Mueller <mattmuelle@gmail.cm>",
711
"repository": {
812
"type": "git",
@@ -12,6 +16,7 @@
1216
"catch-stdout": "0.0.1",
1317
"cheerio": "^0.17.0",
1418
"debug": "^2.0.0",
19+
"object-assign": "^2.0.0",
1520
"superagent": "^0.21.0",
1621
"x-ray-select": "^1.0.3",
1722
"yieldly": "0.0.1"

test/x-ray.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,23 @@ describe('x-ray', function() {
2727
});
2828
})
2929

30+
describe('prepares', function() {
31+
it('should support prepares', function(done) {
32+
function uppercase(str) {
33+
return str.toUpperCase();
34+
}
35+
36+
xray('http://mat.io')
37+
.select('title | uppercase')
38+
.prepare({ uppercase: uppercase })
39+
.run(function(err, str) {
40+
if (err) return done(err);
41+
assert.equal('MAT.IO', str)
42+
done();
43+
});
44+
})
45+
});
46+
3047
it('should support arrays', function(done) {
3148
xray('http://mat.io')
3249
.select(['.Header-list-item a'])

0 commit comments

Comments
 (0)