Skip to content

Instantly share code, notes, and snippets.

@sintaxi
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sintaxi/20298a3b2a93390cd630 to your computer and use it in GitHub Desktop.
Save sintaxi/20298a3b2a93390cd630 to your computer and use it in GitHub Desktop.
/**
* Implementation
*/
var url = require("url")
var u = function(domain){
var urlObj = url.parse(domain || "https://foo.com")
if (["http:", "https:"].indexOf(urlObj.protocol) === -1) {
urlObj.protocol = "http"
}
return url.format(urlObj)
}
/**
* Tests
*/
var assert = require("assert")
describe("u", function(){
it("should exist", function(done){
assert.notEqual(u, undefined, "oops")
assert.notEqual(u, null, "oops")
done()
})
it("should have sane default of foo.com over SSL", function(done){
assert.equal(u(), "https://foo.com/")
done()
})
it("should be able to set protocol", function(done){
assert.equal(u("http://foo.com"), "http://foo.com/")
done()
})
it("should be able to set port", function(done){
assert.equal(u("foo.com:5000"), "http://foo.com:5000/")
done()
})
it("should be able to override the protocol", function(done){
assert.equal(u("http://foo.com"), "http://foo.com/")
done()
})
it("should be able to omit protocol altogether. will default to http", function(done){
assert.equal(u("foo.com"), "http://foo.com/")
done()
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment