page-path-builder
Advanced tools
Comparing version 1.0.1 to 1.0.2
39
index.js
var parser = require('./path-parser') | ||
var stringifyQuerystring = require('querystring').stringify | ||
module.exports = function(pathStr, parameters) { | ||
var parsed | ||
if (typeof pathStr === 'string') { | ||
parsed = parser(pathStr) | ||
} else { | ||
parsed = pathStr | ||
} | ||
var parsed = typeof pathStr === 'string' ? parser(pathStr) : pathStr | ||
var allTokens = parsed.allTokens | ||
@@ -16,3 +11,3 @@ var regex = parsed.regex | ||
if (parameters) { | ||
var path = allTokens.map(function(bit) { | ||
var path = allTokens.map(function(bit) { | ||
if (bit.string) { | ||
@@ -33,3 +28,3 @@ return bit.string | ||
return path | ||
return buildPathWithQuerystring(path, parameters, allTokens) | ||
} else { | ||
@@ -39,1 +34,27 @@ return parsed | ||
} | ||
function buildPathWithQuerystring(path, parameters, tokenArray) { | ||
var parametersInQuerystring = getParametersWithoutMatchingToken(parameters, tokenArray) | ||
if (Object.keys(parametersInQuerystring).length === 0) { | ||
return path | ||
} | ||
return path + '?' + stringifyQuerystring(parametersInQuerystring) | ||
} | ||
function getParametersWithoutMatchingToken(parameters, tokenArray) { | ||
var tokenHash = tokenArray.reduce(function(memo, bit) { | ||
if (!bit.string) { | ||
memo[bit.name] = bit | ||
} | ||
return memo | ||
}, {}) | ||
return Object.keys(parameters).filter(function(param) { | ||
return !tokenHash[param] | ||
}).reduce(function(newParameters, param) { | ||
newParameters[param] = parameters[param] | ||
return newParameters | ||
}, {}) | ||
} |
{ | ||
"name": "page-path-builder", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Uses a page.js-style routing string to build up a path you can link to", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -0,1 +1,2 @@ | ||
[![Build Status](https://travis-ci.org/TehShrike/page-path-builder.svg)](https://travis-ci.org/TehShrike/page-path-builder) | ||
@@ -2,0 +3,0 @@ For those times when you want to programmatically generate a link to page.js route. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3502
6
59
26