-
-
Save natanfelles/f6999bc296a63af2881d9a68fc12945f to your computer and use it in GitHub Desktop.
Parse Github `Links` header 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
/* | |
* 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