diff --git a/index.js b/index.js index cf9e1c1..7e89e24 100644 --- a/index.js +++ b/index.js @@ -4,6 +4,27 @@ var qs = require('querystring') , url = require('url') , xtend = require('xtend'); +function hasRel(x) { + return x && x.rel; +} + +function intoRels (acc, x) { + function splitRel (rel) { + acc[rel] = xtend(x, { rel: rel }); + } + + x.rel.split(/\s+/).forEach(splitRel); + + return acc; +} + +function createObjects (acc, p) { + // rel="next" => 1: rel 2: next + var m = p.match(/\s*(.+)\s*=\s*"?([^"]+)"?/) + if (m) acc[m[1]] = m[2]; + return acc; +} + function parseLink(link) { try { var parts = link.split(';') @@ -12,12 +33,7 @@ function parseLink(link) { , qry = qs.parse(parsedUrl.query); var info = parts - .reduce(function (acc, p) { - // rel="next" => 1: rel 2: next - var m = p.match(/\s*(.+)\s*=\s*"?([^"]+)"?/) - if (m) acc[m[1]] = m[2]; - return acc; - }, {}); + .reduce(createObjects, {}); info = xtend(qry, info); info.url = linkUrl; @@ -28,16 +44,10 @@ function parseLink(link) { } module.exports = function (linkHeader) { - if (!linkHeader) return null; - - return linkHeader.split(/,\s*