Skip to content

Commit

Permalink
custom pattern support
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Williams committed May 22, 2011
1 parent 1dc4bba commit 2926240
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
28 changes: 14 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ var http = require('http')
, parseUrl = require('url').parse
, Expanda = {};

Expanda.pattern = /https?:\/\/(bit.ly|ow.ly|is.gd|t.co)\/\S+/g;

Expanda.expandUrl = function(url, options, callback) {
expandUrl = function(url, options, callback) {
if(!options)
var options = {orignialUrl: url};

Expand All @@ -16,39 +14,41 @@ Expanda.expandUrl = function(url, options, callback) {
, method: 'HEAD'
}, function(response) {
if(!response.headers.location) {
callback(null,{old: options.orignialUrl, new: options.lastUrl})
callback(null,{old: options.orignialUrl, new: options.lastUrl || options.orignialUrl})
} else {
options.lastUrl = response.headers.location;
Expanda.expandUrl(response.headers.location, options, callback);
expandUrl(response.headers.location, options, callback);
}
});
request.on('error',callback)
request.end();
}

Expanda.process = function(inputString, callback) {
module.exports = function(inputString, pattern, callback) {
var expandedCount = 0
, urls = []
, expandadUrls = []
, locatedUrls = inputString.match(Expanda.pattern);
, expandaUrls = [];

if (!(pattern instanceof RegExp))
callback = pattern, pattern = /https?:\/\/(bit.ly|ow.ly|is.gd|t.co)\/\S+/g;

var locatedUrls = inputString.match(pattern);
if(!locatedUrls) {
callback(null, inputString, []);
return;
};

locatedUrls.forEach(function(url) {
Expanda.expandUrl(url, false, function(err, expanded) {
expandadUrls.push(expanded);
expandUrl(url, false, function(err, expanded) {
expandaUrls.push(expanded);
urls.push(expanded.new);
if(++expandedCount == locatedUrls.length) {
var outputString = inputString;
expandadUrls.forEach(function(replacement){
expandaUrls.forEach(function(replacement){
outputString = outputString.replace(replacement.old, replacement.new);
});
callback(null, outputString, urls);
}
})
})
}

module.exports = Expanda;
}
17 changes: 12 additions & 5 deletions spec/expanda_spec.coffee
Original file line number Diff line number Diff line change
@@ -1,37 +1,44 @@
vows = require 'vows'
assert = require 'assert'

Expanda = require('../')
expanda = require('../')

examples = require('./fixtures/examples')

vows
.describe('Expanda')
.addBatch
'base:':
'when using a custom pattern':
topic: -> expanda 'This is cool http://twitter.com/', /http:\/\/twitter.com/, @callback
'should match the google domain': (err, output, urls) ->
assert.equal urls.length, 1
assert.equal urls[0], 'http://twitter.com'

'expansion for: ':
'ow.ly':
topic: -> Expanda.process examples.owly.input, @callback
topic: -> expanda examples.owly.input, @callback
'should have the expected url' : (err, output) ->
assert.equal output, examples.owly.output
'should return the proper array of urls': (err, ouput, urls) ->
assert.equal urls.length, 1
assert.equal urls[0], 'http://blogs.forbes.com/tomtaulli/2011/05/19/the-linkedin-ipo-a-valuable-lesson-for-entrepreneurs/'
'bit.ly':
topic: -> Expanda.process examples.bitly.input, @callback
topic: -> expanda examples.bitly.input, @callback
'should have the expected url' : (err, output) ->
assert.equal output, examples.bitly.output
'should return the proper array of urls': (err, ouput, urls) ->
assert.equal urls.length, 1
assert.equal urls[0], 'http://www.yelp.ca/biz/q-smokehouse-and-southern-barbecue-halifax'
'is.gd':
topic: -> Expanda.process examples.isgd.input, @callback
topic: -> expanda examples.isgd.input, @callback
'should have the expected url' : (err, output) ->
assert.equal output, examples.isgd.output
'should return the proper array of urls': (err, ouput, urls) ->
assert.equal urls.length, 1
assert.equal urls[0], 'http://maps.google.co.uk/maps?f=q&source=s_q&hl=en&geocode=&q=louth&sll=53.800651,-4.064941&sspn=33.219383,38.803711&ie=UTF8&hq=&hnear=Louth,+United+Kingdom&z=14'
't.co':
topic: -> Expanda.process examples.tco.input, @callback
topic: -> expanda examples.tco.input, @callback
'should have the expected url' : (err, output) ->
assert.equal output, examples.tco.output
'should return the proper array of urls': (err, ouput, urls) ->
Expand Down

0 comments on commit 2926240

Please sign in to comment.