Created
January 28, 2013 13:12
-
-
Save inancsevinc/4655400 to your computer and use it in GitHub Desktop.
Link Header javascript parser function
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
function parseLinkHeader(linkHeaderStr) { | |
// http://tools.ietf.org/html/rfc5988 | |
var linkHeader = {}, | |
matchedElements, | |
linkHeaderRegex = /<\s*"?(.*?)"?\s*>\s*;\s*rel\s*=\s*"(\w+)"/gi; | |
while((matchedElements = linkHeaderRegex.exec(linkHeaderStr)) !== null) { | |
linkHeader[matchedElements[2]] = matchedElements[1]; | |
} | |
return linkHeader; | |
} |
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 linkHeaderStr = '<https://api.github.com/user/repos?page=3&per_page=100>; rel="next", <https://api.github.com/user/repos?page=50&per_page=100>; rel="last"'; | |
console.log( parseLinkHeader(linkHeaderStr) ); | |
// Output: | |
// { next: 'https://api.github.com/user/repos?page=3&per_page=100', | |
// last: 'https://api.github.com/user/repos?page=50&per_page=100' } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment