append-query
Advanced tools
Comparing version 1.1.0 to 2.0.0
34
index.js
@@ -5,7 +5,17 @@ var querystring = require('querystring') | ||
module.exports = function appendQuery(uri, q) { | ||
module.defaults = { | ||
encodeComponents: true | ||
, removeNull: false | ||
} | ||
module.exports = function appendQuery(uri, q, opts) { | ||
var parts = url.parse(uri, true) | ||
, parsedQuery = extend(true, {}, parts.query, typeof q === 'string' ? querystring.parse(q) : q) | ||
, originalQuery = parts.query | ||
, queryToAppend = typeof q === 'string' ? querystring.parse(q) : q | ||
, parsedQuery = extend(true, {}, parts.query, queryToAppend) | ||
, opts = extend({}, module.defaults, opts || {}); | ||
parts.search = '?' + serialize(parsedQuery) | ||
parts.query = null | ||
queryString = serialize(parsedQuery, opts) | ||
parts.search = queryString ? '?' + queryString : null | ||
return url.format(parts) | ||
@@ -15,3 +25,3 @@ } | ||
// serialize an object recursively | ||
function serialize(obj, prefix) { | ||
function serialize(obj, opts, prefix) { | ||
var str = [] | ||
@@ -33,6 +43,14 @@ , useArraySyntax = false | ||
query = typeof val === 'object' ? | ||
serialize(val, key) : | ||
encodeURIComponent(key) + '=' + encodeURIComponent(val) | ||
if (val === null) { | ||
if (opts.removeNull) { | ||
return | ||
} | ||
query = opts.encodeComponents ? encodeURIComponent(key) : key | ||
} else if (typeof val === 'object') { | ||
query = serialize(val, opts, key) | ||
} else { | ||
query = opts.encodeComponents ? | ||
encodeURIComponent(key) + '=' + encodeURIComponent(val) : | ||
key + '=' + val; | ||
} | ||
str.push(query) | ||
@@ -39,0 +57,0 @@ }) |
{ | ||
"name": "append-query", | ||
"version": "1.1.0", | ||
"version": "2.0.0", | ||
"description": "Append querystring params to a URL.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -16,2 +16,10 @@ [![Build Status](https://travis-ci.org/lakenen/node-append-query.png?branch=master)](https://travis-ci.org/lakenen/node-append-query) | ||
`appendQuery(url, query[, options])` | ||
* **url** - a string URL to append to. | ||
* **query** - a string or object containing query params to append. | ||
* **options** (optional) | ||
* **encodeComponents** - whether or not to encode appended passed params using `encodeURIComponent`. Default: true. | ||
* **removeNull** - whether or not to remove params for null properties in the query object. Default: false (properties will be preserved with no value). | ||
Example | ||
@@ -27,2 +35,13 @@ ```js | ||
// http://example.com/?beep=boop | ||
appendQuery('http://example.com/', { nothing: null }) | ||
// http://example.com/?nothing | ||
// using pre-encoded values | ||
appendQuery('http://example.com/', { preEncoded: '%22hello%2C%20world!%22' }, { encodeComponents: false }) | ||
// http://example.com/?preEncoded=%22hello%2C%20world!%22 | ||
// remove existing values | ||
appendQuery('http://example.com/?test=1', { test: null }, { removeNull: true }) | ||
// http://example.com/ | ||
``` | ||
@@ -40,2 +59,5 @@ | ||
* **2.0.0** | ||
- fix [#5](https://github.com/lakenen/node-append-query/issues/5) | ||
- add options: `encodeComponents` and `removeNull` | ||
* **1.1.0** | ||
@@ -42,0 +64,0 @@ - add support for recursive serialization of nested objects |
@@ -38,1 +38,38 @@ var test = require('tape') | ||
}) | ||
test('should overwrite existing params by name in url', function (t) { | ||
t.plan(1) | ||
var result = appendQuery('http://example.com/?one=1&two=1', { two: 2 }) | ||
, expected = 'http://example.com/?one=1&two=2' | ||
t.equal(result, expected, 'should be equal') | ||
}) | ||
test('should append just param name when query property is null', function (t) { | ||
t.plan(1) | ||
var result = appendQuery('http://example.com/', { nothing: null }) | ||
, expected = 'http://example.com/?nothing' | ||
t.equal(result, expected, 'should be equal') | ||
}) | ||
// Options | ||
test('should encode a url if encodeComponents is truthy when passed as an option', function (t) { | ||
t.plan(1) | ||
var result = appendQuery('http://example.com/?foo="bar"', 'hello="world"', { encodeComponents: true }) | ||
, expected = 'http://example.com/?foo=%22bar%22&hello=%22world%22' | ||
t.equal(result, expected, 'should be equal') | ||
}) | ||
test('should not encode a url if encodeComponents is falsy when passed as an option', function (t) { | ||
t.plan(1) | ||
var result = appendQuery('http://example.com/?foo="bar"', 'hello="world"', { encodeComponents: false }) | ||
, expected = 'http://example.com/?foo="bar"&hello="world"' | ||
t.equal(result, expected, 'should be equal') | ||
}) | ||
test('should remove null values when removeNull is true', function (t) { | ||
t.plan(1) | ||
var result = appendQuery('http://example.com/?test=1', { test: null }, { removeNull: true }) | ||
, expected = 'http://example.com/' | ||
t.equal(result, expected, 'should be equal') | ||
}) |
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
8148
111
70