Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulyoung committed Aug 10, 2015
0 parents commit d6746ec
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.DS_Store
node_modules/
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# URL helper

A URL helper for use with design systems at The Grid.
48 changes: 48 additions & 0 deletions lib/url-helper.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "url-helper",
"version": "0.1.0",
"description": "A URL helper for use with design systems at The Grid.",
"main": "lib/url-helper.js",
"scripts": {
"test": "grunt test --stack"
},
"repository": {
"type": "git",
"url": "git://github.com/design-systems/url-helper"
},
"author": "Paul Young",
"license": "proprietary",
"bugs": {
"url": "https://github.com/design-systems/url-helper/issues"
},
"homepage": "https://github.com/design-systems/url-helper",
"dependencies": {
"URIjs": "^1.16.0",
"url": "^0.10.3"
},
"devDependencies": {
"chai": "^3.2.0",
"coffee-script": "^1.9.3",
"mocha": "^2.2.5"
},
"scripts": {
"build": "npm run clean; ./node_modules/.bin/coffee --compile --output lib/ src/",
"clean": "mkdir -p lib; rm -r lib; mkdir lib",
"test": "npm run build; ./node_modules/.bin/mocha 'spec/**/*.coffee' --compilers coffee:coffee-script/register --reporter spec"
}
}
93 changes: 93 additions & 0 deletions spec/url-helper.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
chai = require "chai"
{expect} = chai

urlHelper = require "../lib/url-helper"

describe "url helper", ->

describe "relative", ->

context "absolute URL", ->
it "should pass through unchanged", ->
to = "https://twitter.com/thegrid"
from = "index.html"
resolved = urlHelper.resolveRelativeUrl to, from
expect(resolved).to.equal "https://twitter.com/thegrid"


context "URL relative to index", ->
it "should resolve to the correct URL", ->
to = "index.html"
from = "faq/index.html"
resolved = urlHelper.resolveRelativeUrl to, from
expect(resolved).to.equal "../"


context "URL relative to named page", ->
it "should resolve to the correct URL", ->
to = "foo.html"
from = "bar/baz.html"
resolved = urlHelper.resolveRelativeUrl to, from
expect(resolved).to.equal "../foo.html"


context "URL relative to itself", ->
it "should resolve to the correct URL", ->
to = "faq/index.html"
from = "faq/index.html"
resolved = urlHelper.resolveRelativeUrl to, from
expect(resolved).to.equal ""


context "URL relative to another URL", ->
it "should resolve to the correct URL", ->
to = "blog/foo/index.html"
from = "categories/noflo/index.html"
resolved = urlHelper.resolveRelativeUrl to, from
expect(resolved).to.equal "../../blog/foo/"


context "Sibling page URL relative to index", ->
it "should resolve to the correct URL", ->
to = "index.html"
from = "index.abcde.html"
resolved = urlHelper.resolveRelativeUrl to, from
expect(resolved).to.equal "./"


describe "absolute", ->

context "without a URL", ->
it "should be invalid", ->
isValid = urlHelper.validateAbsoluteUrl null
expect(isValid).to.equal false

context "empty URL", ->
it "should be invalid", ->
isValid = urlHelper.validateAbsoluteUrl ""
expect(isValid).to.equal false

context "relative URL", ->
it "should be invalid", ->
isValid = urlHelper.validateAbsoluteUrl "../index.html"
expect(isValid).to.equal false

context "protocol-relative URL", ->
it "should be invalid", ->
isValid = urlHelper.validateAbsoluteUrl "//thegrid.io"
expect(isValid).to.equal false

context "URL with a file:// protocol", ->
it "should be invalid", ->
isValid = urlHelper.validateAbsoluteUrl "file://thegrid.png"
expect(isValid).to.equal false

context "URL with http:// protocol", ->
it "should be valid", ->
isValid = urlHelper.validateAbsoluteUrl "http://thegrid.io"
expect(isValid).to.equal true

context "URL with https:// protocol", ->
it "should be valid", ->
isValid = urlHelper.validateAbsoluteUrl "https://thegrid.io"
expect(isValid).to.equal true
27 changes: 27 additions & 0 deletions src/url-helper.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
url = require "url"
URI = require "URIjs"

urlHelper =
isAbsoluteUrl: (uri) ->
(new URI(uri)).is "absolute"

resolveRelativeUrl: (to, from) ->
return to if module.exports.isAbsoluteUrl to
uri = new URI("/#{to}").relativeTo("/#{from}")
if uri.filename() is "index.html"
uri = uri.directory(".") if uri.directory() is ""
uri = uri.filename("")
uri.toString()

validateAbsoluteUrl: (remoteUrl) ->
return false unless remoteUrl?
{protocol} = url.parse remoteUrl
protocol is "http:" or protocol is "https:"

normalizeUrl: (uri, pagePath) ->
if urlHelper.isAbsoluteUrl uri
return uri if urlHelper.validateAbsoluteUrl uri
return null
urlHelper.resolveRelativeUrl uri, pagePath

module.exports = urlHelper

0 comments on commit d6746ec

Please sign in to comment.