-
-
Save guizmaii/11176512 to your computer and use it in GitHub Desktop.
Helper object that help to parse the "Link" header of the Github API v3, in JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var GithubHelper = { | |
/** | |
* You MUST include Sugar.js (http://sugarjs.com/) if you want this code works. | |
* | |
* Initial code found here : https://gist.github.com/niallo/3109252 | |
* | |
* Parse the Github Link HTTP header used for pagination | |
* | |
* http://developer.github.com/v3/#pagination | |
* | |
* @param linkHeader | |
*/ | |
parseLinkHeader: function(linkHeader) { | |
if (linkHeader.length == 0) { | |
throw new Error("input must not be of zero length"); | |
} | |
// Split parts by comma | |
var parts = linkHeader.split(','); | |
var links = {}; | |
// Parse each part into a named link | |
parts.each(function (p) { | |
var section = p.split(';'); | |
if (section.length != 2) { | |
throw new Error("section could not be split on ';'"); | |
} | |
var url = section[0].replace(/<(.*)>/, '$1').trim(); | |
var name = section[1].replace(/rel="(.*)"/, '$1').trim(); | |
links[name] = url; | |
}); | |
return links; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment