Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support of filepath #9

Merged
merged 1 commit into from
Dec 31, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ function parse(str) {
var hasBlob = seg[2] === 'blob';
if (hasBlob && !isChecksum(seg[3])) {
obj.branch = seg[3];
if(seg.length > 4)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the pr! would you mind updating code formatting to be consistent with the rest of the code? In particular the braces and whitespace around if statements. thanks!

{
obj.filepath = seg.slice(4).join('/');
}
}

var blob = str.indexOf('blob');
Expand Down Expand Up @@ -80,14 +84,20 @@ function parse(str) {
}
}

obj.branch = obj.branch || seg[2] || getBranch(obj.path, obj);
if(!obj.branch){
obj.branch = seg[2] || getBranch(obj.path, obj);
if(seg.length > 3){
obj.filepath = seg.slice(3).join('/');
}
}
var res = {};
res.host = obj.host || 'github.com';
res.owner = obj.owner || null;
res.name = obj.name || null;
res.repo = obj.repo;
res.repository = res.repo;
res.branch = obj.branch;
res.filepath = obj.filepath || null;
return res;
}

Expand Down
12 changes: 12 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ describe('parse-github-url', function() {
assert.equal(gh('https://raw.githubusercontent.com/assemble/verb/dev/README.md').branch, 'dev');
});

it('should get the filepath:', function() {
assert.equal(gh('assemble/verb#branch').filepath, null);
assert.equal(gh('[email protected]:assemble/verb.git#0.6.0').filepath, null);
assert.equal(gh('https://github.com/assemble/verb/blob/foo/README.md').filepath, 'README.md');
assert.equal(gh('https://github.com/assemble/verb/blob/foo/').filepath, null);
assert.equal(gh('https://github.com/assemble/verb/blob/foo').filepath, null);
assert.equal(gh('https://github.com/assemble/verb/blob/foo/bar/README.md').filepath, 'bar/README.md');
assert.equal(gh('https://github.com/assemble/verb/tree/dev').filepath, null);
assert.equal(gh('https://raw.githubusercontent.com/assemble/verb/dev/README.md').filepath, 'README.md');
assert.equal(gh('https://raw.githubusercontent.com/assemble/verb/dev/bar/README.md').filepath, 'bar/README.md');
});
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the unit tests! looks great


it('should use master branch when another branch is not defined:', function() {
assert.equal(gh('assemble/verb').branch, 'master');
assert.equal(gh('git://github.com/foo/bar.git').branch, 'master');
Expand Down