Skip to content

Instantly share code, notes, and snippets.

@awesome
Forked from niallo/gist:3109252
Created April 8, 2014 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awesome/10198753 to your computer and use it in GitHub Desktop.
Save awesome/10198753 to your computer and use it in GitHub Desktop.
/*
* parse_link_header()
*
* Parse the Github Link HTTP header used for pageination
* http://developer.github.com/v3/#pagination
*/
function parse_link_header(header) {
if (header.length == 0) {
throw new Error("input must not be of zero length");
}
// Split parts by comma
var parts = header.split(',');
var links = {};
// Parse each part into a named link
_.each(parts, 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