github-url-to-object
Advanced tools
Comparing version 1.5.0 to 1.5.1
46
index.js
@@ -1,8 +0,8 @@ | ||
"use strict" | ||
'use strict' | ||
var url = require("url") | ||
var util = require("util") | ||
var isUrl = require("is-url") | ||
var url = require('url') | ||
var util = require('util') | ||
var isUrl = require('is-url') | ||
module.exports = function(repo_url) { | ||
module.exports = function (repo_url) { | ||
var obj = {} | ||
@@ -19,19 +19,18 @@ | ||
obj.repo = shorthand[2] | ||
obj.branch = shorthand[3] || "master" | ||
obj.branch = shorthand[3] || 'master' | ||
} else if (mediumhand) { | ||
obj.user = mediumhand[1] | ||
obj.repo = mediumhand[2] | ||
obj.branch = mediumhand[3] || "master" | ||
obj.branch = mediumhand[3] || 'master' | ||
} else if (antiquated) { | ||
obj.user = antiquated[1] | ||
obj.repo = antiquated[2].replace(/\.git$/i, "") | ||
obj.branch = "master" | ||
obj.repo = antiquated[2].replace(/\.git$/i, '') | ||
obj.branch = 'master' | ||
} else { | ||
// Turn git+http URLs into http URLs | ||
repo_url = repo_url.replace(/^git\+/, "") | ||
repo_url = repo_url.replace(/^git\+/, '') | ||
if (!isUrl(repo_url)) return null | ||
var parsedURL = url.parse(repo_url) | ||
if (parsedURL.hostname != "github.com") return null | ||
if (parsedURL.hostname !== 'github.com') return null | ||
var parts = parsedURL.pathname.match(/^\/([\w-_]+)\/([\w-_\.]+)(\/tree\/[\w-_\.\/]+)?(\/blob\/[\w-_\.\/]+)?/) | ||
@@ -41,27 +40,26 @@ // ([\w-_\.]+) | ||
obj.user = parts[1] | ||
obj.repo = parts[2].replace(/\.git$/i, "") | ||
obj.repo = parts[2].replace(/\.git$/i, '') | ||
if (parts[3]) { | ||
obj.branch = parts[3].replace(/^\/tree\//, "").match(/[\w-_.]+\/{0,1}[\w-_]+/)[0] | ||
obj.branch = parts[3].replace(/^\/tree\//, '').match(/[\w-_.]+\/{0,1}[\w-_]+/)[0] | ||
} else if (parts[4]) { | ||
obj.branch = parts[4].replace(/^\/blob\//, "").match(/[\w-_.]+\/{0,1}[\w-_]+/)[0] | ||
obj.branch = parts[4].replace(/^\/blob\//, '').match(/[\w-_.]+\/{0,1}[\w-_]+/)[0] | ||
} else { | ||
obj.branch = "master" | ||
obj.branch = 'master' | ||
} | ||
} | ||
obj.tarball_url = util.format("https://api.github.com/repos/%s/%s/tarball/%s", obj.user, obj.repo, obj.branch) | ||
obj.tarball_url = util.format('https://api.github.com/repos/%s/%s/tarball/%s', obj.user, obj.repo, obj.branch) | ||
if (obj.branch === "master") { | ||
obj.https_url = util.format("https://github.com/%s/%s", obj.user, obj.repo) | ||
obj.travis_url = util.format("https://travis-ci.org/%s/%s", obj.user, obj.repo) | ||
if (obj.branch === 'master') { | ||
obj.https_url = util.format('https://github.com/%s/%s', obj.user, obj.repo) | ||
obj.travis_url = util.format('https://travis-ci.org/%s/%s', obj.user, obj.repo) | ||
} else { | ||
obj.https_url = util.format("https://github.com/%s/%s/tree/%s", obj.user, obj.repo, obj.branch) | ||
obj.travis_url = util.format("https://travis-ci.org/%s/%s?branch=%s", obj.user, obj.repo, obj.branch) | ||
obj.https_url = util.format('https://github.com/%s/%s/tree/%s', obj.user, obj.repo, obj.branch) | ||
obj.travis_url = util.format('https://travis-ci.org/%s/%s?branch=%s', obj.user, obj.repo, obj.branch) | ||
} | ||
obj.api_url = util.format("https://api.github.com/repos/%s/%s", obj.user, obj.repo) | ||
obj.api_url = util.format('https://api.github.com/repos/%s/%s', obj.user, obj.repo) | ||
return obj | ||
} |
{ | ||
"name": "github-url-to-object", | ||
"version": "1.5.0", | ||
"version": "1.5.1", | ||
"description": "Extract user, repo, and other interesting properties from GitHub URLs", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha", | ||
"test": "standard --format && mocha", | ||
"build": "browserify index.js --standalone gh > dist/gh.js" | ||
@@ -30,4 +30,5 @@ }, | ||
"mocha": "^1.19.0", | ||
"standard": "^3.7.0", | ||
"uglify-js": "^2.4.15" | ||
} | ||
} |
@@ -79,4 +79,6 @@ # github-url-to-object [![Build Status](https://travis-ci.org/zeke/github-url-to-object.png?branch=master)](https://travis-ci.org/zeke/github-url-to-object) | ||
[![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard) | ||
## License | ||
MIT |
@@ -1,11 +0,10 @@ | ||
var mocha = require("mocha") | ||
var assert = require("assert") | ||
var gh = require("../index") | ||
/* globals before, describe, it */ | ||
describe("github-url-to-object", function() { | ||
var assert = require('assert') | ||
var gh = require('../index') | ||
describe("shorthand", function(){ | ||
it("supports user/repo style", function(){ | ||
var obj = gh("user/repo#branch") | ||
describe('github-url-to-object', function () { | ||
describe('shorthand', function () { | ||
it('supports user/repo style', function () { | ||
var obj = gh('user/repo#branch') | ||
assert.equal(obj.user, 'user') | ||
@@ -15,4 +14,4 @@ assert.equal(obj.repo, 'repo') | ||
it("supports user/repo#branch style", function(){ | ||
var obj = gh("user/repo#branch") | ||
it('supports user/repo#branch style', function () { | ||
var obj = gh('user/repo#branch') | ||
assert.equal(obj.user, 'user') | ||
@@ -23,4 +22,4 @@ assert.equal(obj.repo, 'repo') | ||
it("defaults to master branch", function(){ | ||
var obj = gh("user/repo") | ||
it('defaults to master branch', function () { | ||
var obj = gh('user/repo') | ||
assert.equal(obj.user, 'user') | ||
@@ -33,6 +32,5 @@ assert.equal(obj.repo, 'repo') | ||
describe("mediumhand", function(){ | ||
it("supports github:user/repo style", function(){ | ||
var obj = gh("user/repo#branch") | ||
describe('mediumhand', function () { | ||
it('supports github:user/repo style', function () { | ||
var obj = gh('user/repo#branch') | ||
assert.equal(obj.user, 'user') | ||
@@ -42,4 +40,4 @@ assert.equal(obj.repo, 'repo') | ||
it("supports github:user/repo#branch style", function(){ | ||
var obj = gh("user/repo#branch") | ||
it('supports github:user/repo#branch style', function () { | ||
var obj = gh('user/repo#branch') | ||
assert.equal(obj.user, 'user') | ||
@@ -50,4 +48,4 @@ assert.equal(obj.repo, 'repo') | ||
it("defaults to master branch", function(){ | ||
var obj = gh("github:user/repo") | ||
it('defaults to master branch', function () { | ||
var obj = gh('github:user/repo') | ||
assert.equal(obj.user, 'user') | ||
@@ -58,4 +56,4 @@ assert.equal(obj.repo, 'repo') | ||
it("rejects bitbucket", function(){ | ||
var obj = gh("bitbucket:user/repo") | ||
it('rejects bitbucket', function () { | ||
var obj = gh('bitbucket:user/repo') | ||
assert.equal(obj, null) | ||
@@ -66,6 +64,5 @@ }) | ||
describe("oldschool", function(){ | ||
it("supports git@ URLs", function() { | ||
var obj = gh("git@github.com:heroku/heroku-flags.git") | ||
describe('oldschool', function () { | ||
it('supports git@ URLs', function () { | ||
var obj = gh('git@github.com:heroku/heroku-flags.git') | ||
assert.equal(obj.user, 'heroku') | ||
@@ -75,9 +72,9 @@ assert.equal(obj.repo, 'heroku-flags') | ||
it("defaults to master branch for git@ URLs", function() { | ||
var obj = gh("git@github.com:heroku/heroku-flags.git") | ||
it('defaults to master branch for git@ URLs', function () { | ||
var obj = gh('git@github.com:heroku/heroku-flags.git') | ||
assert.equal(obj.branch, 'master') | ||
}) | ||
it("supports git+https:// URLs", function() { | ||
var obj = gh("git+https://github.com/foo/bar.git") | ||
it('supports git+https:// URLs', function () { | ||
var obj = gh('git+https://github.com/foo/bar.git') | ||
assert.equal(obj.user, 'foo') | ||
@@ -87,4 +84,4 @@ assert.equal(obj.repo, 'bar') | ||
it("supports git:// URLs", function() { | ||
var obj = gh("git://github.com/foo/bar.git") | ||
it('supports git:// URLs', function () { | ||
var obj = gh('git://github.com/foo/bar.git') | ||
assert.equal(obj.user, 'foo') | ||
@@ -94,4 +91,4 @@ assert.equal(obj.repo, 'bar') | ||
it("defaults to master branch for git:// URLs", function() { | ||
var obj = gh("git://github.com/foo/bar.git") | ||
it('defaults to master branch for git:// URLs', function () { | ||
var obj = gh('git://github.com/foo/bar.git') | ||
assert.equal(obj.branch, 'master') | ||
@@ -102,6 +99,5 @@ }) | ||
describe("http", function(){ | ||
it("supports http URLs", function() { | ||
var obj = gh("http://github.com/zeke/outlet.git") | ||
describe('http', function () { | ||
it('supports http URLs', function () { | ||
var obj = gh('http://github.com/zeke/outlet.git') | ||
assert.equal(obj.user, 'zeke') | ||
@@ -111,4 +107,4 @@ assert.equal(obj.repo, 'outlet') | ||
it("supports https URLs", function() { | ||
var obj = gh("https://github.com/zeke/outlet.git") | ||
it('supports https URLs', function () { | ||
var obj = gh('https://github.com/zeke/outlet.git') | ||
assert.equal(obj.user, 'zeke') | ||
@@ -118,4 +114,4 @@ assert.equal(obj.repo, 'outlet') | ||
it("supports deep URLs", function() { | ||
var obj = gh("https://github.com/zeke/ruby-rails-sample/blob/b1e1000fedb6ca448dd78702de4fc78dedfee48c/app.json") | ||
it('supports deep URLs', function () { | ||
var obj = gh('https://github.com/zeke/ruby-rails-sample/blob/b1e1000fedb6ca448dd78702de4fc78dedfee48c/app.json') | ||
assert.equal(obj.user, 'zeke') | ||
@@ -125,4 +121,4 @@ assert.equal(obj.repo, 'ruby-rails-sample') | ||
it("doesn't require .git extension", function() { | ||
var obj = gh("https://github.com/zeke/outlet") | ||
it("doesn't require .git extension", function () { | ||
var obj = gh('https://github.com/zeke/outlet') | ||
assert.equal(obj.user, 'zeke') | ||
@@ -132,24 +128,24 @@ assert.equal(obj.repo, 'outlet') | ||
it("defaults to master branch", function() { | ||
var obj = gh("https://github.com/zeke/outlet") | ||
it('defaults to master branch', function () { | ||
var obj = gh('https://github.com/zeke/outlet') | ||
assert.equal(obj.branch, 'master') | ||
}) | ||
it("resolves tree-style URLS for branches other than master", function() { | ||
var obj = gh("https://github.com/zeke/outlet/tree/other-branch") | ||
it('resolves tree-style URLS for branches other than master', function () { | ||
var obj = gh('https://github.com/zeke/outlet/tree/other-branch') | ||
assert.equal(obj.branch, 'other-branch') | ||
}) | ||
it("resolves URLS for branches containing /", function() { | ||
var obj = gh("https://github.com/zeke/outlet/tree/feature/other-branch") | ||
it('resolves URLS for branches containing /', function () { | ||
var obj = gh('https://github.com/zeke/outlet/tree/feature/other-branch') | ||
assert.equal(obj.branch, 'feature/other-branch') | ||
}) | ||
it("resolves URLS for branches containing .", function() { | ||
var obj = gh("https://github.com/zeke/outlet/tree/2.1") | ||
it('resolves URLS for branches containing .', function () { | ||
var obj = gh('https://github.com/zeke/outlet/tree/2.1') | ||
assert.equal(obj.branch, '2.1') | ||
}) | ||
it("resolves blob-style URLS for branches other than master", function() { | ||
var obj = gh("https://github.com/zeke/ord/blob/new-style/.gitignore") | ||
it('resolves blob-style URLS for branches other than master', function () { | ||
var obj = gh('https://github.com/zeke/ord/blob/new-style/.gitignore') | ||
assert.equal(obj.branch, 'new-style') | ||
@@ -160,35 +156,35 @@ }) | ||
describe("properties", function() { | ||
describe('properties', function () { | ||
var obj | ||
before(function(){ | ||
obj = gh("zeke/ord") | ||
before(function () { | ||
obj = gh('zeke/ord') | ||
}) | ||
it("user", function() { | ||
assert.equal(obj.user, "zeke") | ||
it('user', function () { | ||
assert.equal(obj.user, 'zeke') | ||
}) | ||
it("repo", function() { | ||
assert.equal(obj.repo, "ord") | ||
it('repo', function () { | ||
assert.equal(obj.repo, 'ord') | ||
}) | ||
it("branch", function() { | ||
assert.equal(obj.branch, "master") | ||
it('branch', function () { | ||
assert.equal(obj.branch, 'master') | ||
}) | ||
it("tarball_url", function() { | ||
assert.equal(obj.tarball_url, "https://api.github.com/repos/zeke/ord/tarball/master") | ||
it('tarball_url', function () { | ||
assert.equal(obj.tarball_url, 'https://api.github.com/repos/zeke/ord/tarball/master') | ||
}) | ||
it("api_url", function() { | ||
assert.equal(obj.api_url, "https://api.github.com/repos/zeke/ord") | ||
it('api_url', function () { | ||
assert.equal(obj.api_url, 'https://api.github.com/repos/zeke/ord') | ||
}) | ||
it("https_url", function() { | ||
assert.equal(obj.https_url, "https://github.com/zeke/ord") | ||
it('https_url', function () { | ||
assert.equal(obj.https_url, 'https://github.com/zeke/ord') | ||
}) | ||
it("travis_url", function() { | ||
assert.equal(obj.travis_url, "https://travis-ci.org/zeke/ord") | ||
it('travis_url', function () { | ||
assert.equal(obj.travis_url, 'https://travis-ci.org/zeke/ord') | ||
}) | ||
@@ -198,24 +194,23 @@ | ||
describe("branch other than master", function() { | ||
describe('branch other than master', function () { | ||
var obj | ||
before(function(){ | ||
obj = gh("zeke/ord#experiment") | ||
before(function () { | ||
obj = gh('zeke/ord#experiment') | ||
}) | ||
it("applies to tarball_url", function() { | ||
assert.equal(obj.tarball_url, "https://api.github.com/repos/zeke/ord/tarball/experiment") | ||
it('applies to tarball_url', function () { | ||
assert.equal(obj.tarball_url, 'https://api.github.com/repos/zeke/ord/tarball/experiment') | ||
}) | ||
it("applies to https_url", function() { | ||
assert.equal(obj.https_url, "https://github.com/zeke/ord/tree/experiment") | ||
it('applies to https_url', function () { | ||
assert.equal(obj.https_url, 'https://github.com/zeke/ord/tree/experiment') | ||
}) | ||
it("doesn't apply to api_url", function() { | ||
assert.equal(obj.api_url, "https://api.github.com/repos/zeke/ord") | ||
it("doesn't apply to api_url", function () { | ||
assert.equal(obj.api_url, 'https://api.github.com/repos/zeke/ord') | ||
}) | ||
it("applies to travis_url", function() { | ||
assert.equal(obj.travis_url, "https://travis-ci.org/zeke/ord?branch=experiment") | ||
it('applies to travis_url', function () { | ||
assert.equal(obj.travis_url, 'https://travis-ci.org/zeke/ord?branch=experiment') | ||
}) | ||
@@ -225,13 +220,12 @@ | ||
describe("failure", function(){ | ||
it("returns null if url is falsy", function() { | ||
describe('failure', function () { | ||
it('returns null if url is falsy', function () { | ||
assert.equal(gh(), null) | ||
assert.equal(gh(null), null) | ||
assert.equal(gh(undefined), null) | ||
assert.equal(gh(""), null) | ||
assert.equal(gh(''), null) | ||
}) | ||
it("returns null for non-github URLs", function() { | ||
var obj = gh("https://bitbucket.com/other/thing") | ||
it('returns null for non-github URLs', function () { | ||
var obj = gh('https://bitbucket.com/other/thing') | ||
assert.equal(obj, null) | ||
@@ -238,0 +232,0 @@ }) |
14592
84
4