Skip to content

Commit ccd3476

Browse files
committed
init
0 parents  commit ccd3476

File tree

6 files changed

+216
-0
lines changed

6 files changed

+216
-0
lines changed

.gitignore

+66
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

LICENSE

+22
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

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
- All 5 different ways you could download a freaking tarball/zipball
9+
10+
## API
11+
12+
### [user, repo, version] = parse(url)
13+
14+
`version` could be `false`y, a semantic version, a commit, or a branch, etc.
15+
16+
```js
17+
var parse = require('parse-github-repo-url')
18+
parse('component/emitter#1') // => ['component', 'emitter', '1']
19+
```
20+
21+
See the tests for all the different types of supported URLs.

index.js

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

package.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "parse-github-repo-url",
3+
"description": "Parse a GitHub URL for user/project@version",
4+
"version": "1.0.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+
"scripts": {
17+
"test": "mocha --reporter spec --bail"
18+
}
19+
}

test.js

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
22+
describe('versioned', function () {
23+
[
24+
'component/emitter#1',
25+
'component/emitter@1',
26+
'component/emitter#"1"',
27+
'component/emitter@"1"',
28+
'git://github.com/component/emitter.git#1',
29+
'https://github.com/repos/component/emitter/tarball/1',
30+
'https://github.com/repos/component/emitter/zipball/1',
31+
'https://codeload.github.com/component/emitter/legacy.zip/1',
32+
'https://codeload.github.com/component/emitter/legacy.tar.gz/1',
33+
'https://github.com/component/emitter/archive/1.tar.gz',
34+
].forEach(function (url) {
35+
it(url, function () {
36+
assert.deepEqual(['component', 'emitter', '1'], parse(url))
37+
})
38+
})
39+
})

0 commit comments

Comments
 (0)