Skip to content

Commit

Permalink
pull out and name anonymous functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jarofghosts committed Aug 22, 2015
1 parent e299b94 commit d20192b
Showing 1 changed file with 28 additions and 18 deletions.
46 changes: 28 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(';')
Expand All @@ -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;
Expand All @@ -28,16 +44,10 @@ function parseLink(link) {
}

module.exports = function (linkHeader) {
if (!linkHeader) return null;

return linkHeader.split(/,\s*</)
.map(parseLink)
.filter(function (x) { return x && x.rel; })
.reduce(function (acc, x) {
x.rel.split(/\s+/).forEach(function (rel) {
acc[rel] = xtend(x, { rel: rel });
});

return acc;
}, {});
if (!linkHeader) return null;

return linkHeader.split(/,\s*</)
.map(parseLink)
.filter(hasRel)
.reduce(intoRels, {});
};

0 comments on commit d20192b

Please sign in to comment.