node/deps/npm/node_modules/github-url-from-username-repo/index.js

22 lines
785 B
JavaScript
Raw Normal View History

2013-10-25 00:21:59 +08:00
module.exports = getUrl
2014-09-17 06:38:50 +08:00
function getUrl (r, forBrowser) {
if (!r) return null
2014-09-25 05:41:07 +08:00
// Regex taken from https://github.com/npm/npm-package-arg/commit/01dce583c64afae07b66a2a8a6033aeba871c3cd
// Note: This does not fully test the git ref format.
// See https://www.kernel.org/pub/software/scm/git/docs/git-check-ref-format.html
//
// The only way to do this properly would be to shell out to
// git-check-ref-format, and as this is a fast sync function,
// we don't want to do that. Just let git fail if it turns
// out that the commit-ish is invalid.
// GH usernames cannot start with . or -
if (/^[^@%\/\s\.-][^:@%\/\s]*\/[^@\s\/%]+(?:#.*)?$/.test(r)) {
2014-09-17 06:38:50 +08:00
if (forBrowser)
r = r.replace("#", "/tree/")
2014-08-01 00:05:30 +08:00
return "https://github.com/" + r
2014-09-17 06:38:50 +08:00
}
return null
}