Created
October 15, 2018 06:18
-
-
Save prameshbhattarai/ee450a804589d978b5918a3ce071444f to your computer and use it in GitHub Desktop.
parse link for pagination in javascript
This file contains 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 for pagination */ | |
(function() { | |
'use strict'; | |
function parseLink(link, api) { | |
if(link === null) return; | |
var split = link.match(/<?([^>]*)>(.*)/); | |
var api = split[1].substring(split[1].indexOf(api), split[1].length); | |
var rel = split[2].match( /"(.*?)"/ )[1]; | |
var nextUrl = { | |
api: api, | |
rel: rel | |
} | |
assert( | |
nextUrl.api === "controllerApi?offset=25&limit=25&count=true&sort=sortBySomeColumn&otherQuery=true", | |
"Api should be parsed." | |
); | |
// should throw error | |
// assert( | |
// nextUrl.api === "controllerApi", | |
// "Api should be parsed." | |
// ); | |
} | |
function assert(condition, message) { | |
if(!condition) { | |
throw Error("Assert failed" + (typeof message !== "undefined" ? ": " + message : "")); | |
} | |
} | |
parseLink( | |
'<http://www.testUrl.com:8080/api/controllerApi?offset=25&limit=25&count=true&sort=sortBySomeColumn&otherQuery=true>; rel="next"', | |
'controllerApi' | |
); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment