Skip to content

Commit 3083e56

Browse files
committed
reinit
0 parents  commit 3083e56

File tree

7 files changed

+284
-0
lines changed

7 files changed

+284
-0
lines changed

.gitignore

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Compiled source #
2+
###################
3+
*.com
4+
*.class
5+
*.dll
6+
*.exe
7+
*.o
8+
*.so
9+
10+
# Packages #
11+
############
12+
# it's better to unpack these files and commit the raw source
13+
# git has its own built in compression methods
14+
*.7z
15+
*.dmg
16+
*.gz
17+
*.iso
18+
*.jar
19+
*.rar
20+
*.tar
21+
*.zip
22+
23+
# Logs and databases #
24+
######################
25+
*.log
26+
*.sql
27+
*.sqlite
28+
29+
# OS generated files #
30+
######################
31+
.DS_Store*
32+
ehthumbs.db
33+
Icon?
34+
Thumbs.db
35+
36+
# Node.js #
37+
###########
38+
lib-cov
39+
*.seed
40+
*.log
41+
*.csv
42+
*.dat
43+
*.out
44+
*.pid
45+
*.gz
46+
47+
pids
48+
logs
49+
results
50+
51+
node_modules
52+
npm-debug.log
53+
54+
# Git #
55+
#######
56+
*.orig
57+
*.BASE.*
58+
*.BACKUP.*
59+
*.LOCAL.*
60+
*.REMOTE.*
61+
62+
# Components #
63+
##############
64+
65+
/build
66+
/components

.travis.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
language: node_js
2+
node_js:
3+
- "0.10"
4+
- "0.12"
5+
- "4"
6+
branches:
7+
only:
8+
- master
9+
script:
10+
- npm test

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
The MIT License (MIT)
3+
4+
Copyright (c) 2014 Jonathan Ong [email protected]
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
THE SOFTWARE.

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Parse GitHub Repo URL
2+
3+
Parse all the stupid ways you could write a GitHub URL in your damn `package.json`.
4+
Supports:
5+
6+
- `<user>/<repo#<commit>`
7+
- `git://` and `.git` w/ `#commit` or `@version`
8+
- `git@` and `https:git@`
9+
- `www.github.com`
10+
- All 5 different ways you could download a freaking tarball/zipball
11+
12+
[![Build status][ci-image] ][ci-url]
13+
14+
## API
15+
16+
### [user, repo, version] = parse(url)
17+
18+
`version` could be `false`y, a semantic version, a commit, or a branch, etc.
19+
20+
```js
21+
var parse = require('parse-github-repo-url')
22+
parse('component/emitter#1') // => ['component', 'emitter', '1']
23+
```
24+
25+
See the tests for all the different types of supported URLs.
26+
27+
[ci-image]: https://travis-ci.org/repo-utils/parse-github-repo-url.png?branch=master
28+
[ci-url]: https://travis-ci.org/repo-utils/parse-github-repo-url

index.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
var parse = require('url').parse
3+
4+
module.exports = function (string) {
5+
// user/repo#version
6+
var m = /^([\w-]+)\/([\w-.]+)((?:#|@).+)?$/.exec(string)
7+
if (m) return format(m)
8+
9+
string = string.replace('//www.', '//')
10+
// normalize git@ and https:git@ urls
11+
string = string.replace(/^git@/, 'https://')
12+
string = string.replace(/^https:git@/, 'https://')
13+
string = string.replace('.com:', '.com/')
14+
15+
if (!~string.indexOf('://')) {
16+
return false
17+
}
18+
var url = parse(string)
19+
20+
switch (url.hostname) {
21+
case 'github.com':
22+
case 'api.github.com':
23+
case 'codeload.github.com':
24+
break
25+
default:
26+
return false
27+
}
28+
29+
var path = url.pathname.replace(/\.git$/, '')
30+
31+
// https://www.npmjs.org/doc/json.html#Git-URLs-as-Dependencies
32+
var m = /^\/([\w-]+)\/([\w-.]+)$/.exec(path)
33+
if (m) return m.slice(1, 3).concat((url.hash || '').slice(1))
34+
35+
// archive link
36+
// https://developer.github.com/v3/repos/contents/#get-archive-link
37+
var m = /^\/repos\/([\w-]+)\/([\w-.]+)\/(?:tarball|zipball)(\/.+)?$/.exec(path)
38+
if (m) return format(m)
39+
40+
// codeload link
41+
// https://developer.github.com/v3/repos/contents/#response-4
42+
var m = /^\/([\w-]+)\/([\w-.]+)\/(?:legacy\.(?:zip|tar\.gz))(\/.+)?$/.exec(path)
43+
if (m) return format(m)
44+
45+
// tarball link
46+
// https://github.com/LearnBoost/socket.io-client/blob/master/package.json#L14
47+
var m = /^\/([\w-]+)\/([\w-.]+)\/archive\/(.+)\.tar\.gz?$/.exec(path)
48+
if (m) return m.slice(1, 4)
49+
50+
return false
51+
}
52+
53+
function format(m) {
54+
var version = (m[3] || '').slice(1)
55+
if (/^['"]/.test(version)) version = version.slice(1, -1)
56+
return [m[1], m[2], version]
57+
}

package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "parse-github-repo-url",
3+
"description": "Parse a GitHub URL for user/project@version",
4+
"version": "1.1.0",
5+
"author": {
6+
"name": "Jonathan Ong",
7+
"email": "[email protected]",
8+
"url": "http://jongleberry.com",
9+
"twitter": "https://twitter.com/jongleberry"
10+
},
11+
"license": "MIT",
12+
"repository": "repo-utils/parse-github-repo-url",
13+
"devDependencies": {
14+
"mocha": "1"
15+
},
16+
"main": "index.js",
17+
"scripts": {
18+
"test": "mocha --reporter spec --bail"
19+
}
20+
}

test.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
2+
var assert = require('assert')
3+
4+
var parse = require('./')
5+
6+
describe('versionless', function () {
7+
[
8+
'component/emitter',
9+
'https://github.com/component/emitter',
10+
'git://github.com/component/emitter.git',
11+
'https://github.com/repos/component/emitter/tarball',
12+
'https://github.com/repos/component/emitter/zipball',
13+
'https://codeload.github.com/component/emitter/legacy.zip',
14+
'https://codeload.github.com/component/emitter/legacy.tar.gz',
15+
].forEach(function (url) {
16+
it(url, function () {
17+
assert.deepEqual(['component', 'emitter', ''], parse(url))
18+
})
19+
})
20+
21+
it('works for www.github.com', function () {
22+
var url = 'https://www.github.com/component/emitter'
23+
var parsed = parse(url)
24+
assert.deepEqual(['component', 'emitter', ''], parsed)
25+
})
26+
27+
it('works for http://www.github.com', function () {
28+
var url = 'http://www.github.com/component/emitter'
29+
var parsed = parse(url)
30+
assert.deepEqual(['component', 'emitter', ''], parsed)
31+
})
32+
})
33+
34+
describe('versioned', function () {
35+
[
36+
'component/emitter#1',
37+
'component/emitter@1',
38+
'component/emitter#"1"',
39+
'component/emitter@"1"',
40+
'git://github.com/component/emitter.git#1',
41+
'https://github.com/repos/component/emitter/tarball/1',
42+
'https://github.com/repos/component/emitter/zipball/1',
43+
'https://codeload.github.com/component/emitter/legacy.zip/1',
44+
'https://codeload.github.com/component/emitter/legacy.tar.gz/1',
45+
'https://github.com/component/emitter/archive/1.tar.gz',
46+
].forEach(function (url) {
47+
it(url, function () {
48+
assert.deepEqual(['component', 'emitter', '1'], parse(url))
49+
})
50+
})
51+
})
52+
53+
describe('url parse', function () {
54+
var builtinUrlParse = require('url').parse
55+
56+
it('handles https:// url', function () {
57+
var url = 'https://foo.com/bar'
58+
var parsed = builtinUrlParse(url)
59+
assert.equal('foo.com', parsed.hostname)
60+
})
61+
62+
it('does not handle emails', function () {
63+
var url = '[email protected]/bar'
64+
var parsed = builtinUrlParse(url)
65+
assert.equal(null, parsed.hostname, JSON.stringify(parsed))
66+
})
67+
})
68+
69+
describe('git @ syntax', function () {
70+
it('works for git url', function () {
71+
var url = '[email protected]:bahmutov/lazy-ass.git'
72+
var parsed = parse(url)
73+
assert.deepEqual(['bahmutov', 'lazy-ass', ''], parsed)
74+
});
75+
76+
it('works for https:git url', function () {
77+
var url = 'https:[email protected]:bahmutov/lazy-ass.git'
78+
var parsed = parse(url)
79+
assert.deepEqual(['bahmutov', 'lazy-ass', ''], parsed)
80+
});
81+
})

0 commit comments

Comments
 (0)