Skip to content

Instantly share code, notes, and snippets.

@guizmaii
Forked from niallo/gist:3109252
Last active August 29, 2015 14:00
Show Gist options
  • Save guizmaii/11176512 to your computer and use it in GitHub Desktop.
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
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