Repository: sindresorhus/query-string (js)

Issue:
New feature : Option to add leading "?"
Would you be open to PR for adding following option?


Example:

```
let params = {
"foo": bar
}
const url = `http://abc.com/xyz${stringify(params,{separator:true}})`
//prints http://abc.com/xyz?foo=bar

```

with falsy params
```
let params = undefined;
const url = `http://abc.com/xyz${stringify(params,{seperator:true})`
//prints http://abc.com/xyz
```

Rationale:
Avoid ternary checks like this everywhere:

`const url = params?  'http://abc.com/xyz?${stringify(params)' :  'http://abc.com/xyz'`

Pull request description:
Add `.stringifyUrl()` method
resolve #205 
I create a method named `stringifyUrl` that does the inverse of `.parseUrl()`

Patch:
diff --git a/index.d.ts b/index.d.ts
index cf85856..3fb11f0 100644
--- a/index.d.ts
+++ b/index.d.ts
@@ -12,6 +12,8 @@ export interface ParseOptions {
 	- `bracket`: Parse arrays with bracket representation:
 
 		```
+		import queryString = require('query-string');
+
 		queryString.parse('foo[]=1&foo[]=2&foo[]=3', {arrayFormat: 'bracket'});
 		//=> {foo: ['1', '2', '3']}
 		```
@@ -19,6 +21,8 @@ export interface ParseOptions {
 	- `index`: Parse arrays with index representation:
 
 		```
+		import queryString = require('query-string');
+
 		queryString.parse('foo[0]=1&foo[1]=2&foo[3]=3', {arrayFormat: 'index'});
 		//=> {foo: ['1', '2', '3']}
 		```
@@ -26,6 +30,8 @@ export interface ParseOptions {
 	- `comma`: Parse arrays with elements separated by comma:
 
 		```
+		import queryString = require('query-string');
+
 		queryString.parse('foo=1,2,3', {arrayFormat: 'comma'});
 		//=> {foo: ['1', '2', '3']}
 		```
@@ -33,6 +39,8 @@ export interface ParseOptions {
 	- `none`: Parse arrays with elements using duplicate keys:
 
 		```
+		import queryString = require('query-string');
+
 		queryString.parse('foo=1&foo=2&foo=3');
 		//=> {foo: ['1', '2', '3']}
 		```
@@ -48,6 +56,8 @@ export interface ParseOptions {
 
 	@example
 	```
+	import queryString = require('query-string');
+
 	const order = ['c', 'a', 'b'];
 
 	queryString.parse('?a=one&b=two&c=three', {
@@ -56,6 +66,10 @@ export interface ParseOptions {
 	//=> {c: 'three', a: 'one', b: 'two'}
 	```
 
+	@example
+	```
+	import queryString = require('query-string');
+
 	queryString.parse('?a=one&c=three&b=two', {sort: false});
 	//=> {a: 'one', c: 'three', b: 'two'}
 	```
@@ -68,7 +82,9 @@ export interface ParseOptions {
 	@default false
 
 	@example
-	```js
+	```
+	import queryString = require('query-string');
+
 	queryString.parse('foo=1', {parseNumbers: true});
 	//=> {foo: 1}
 	```
@@ -82,6 +98,8 @@ export interface ParseOptions {
 
 	@example
 	```
+	import queryString = require('query-string');
+
 	queryString.parse('foo=true', {parseBooleans: true});
 	//=> {foo: true}
 	```
@@ -117,6 +135,8 @@ Extract the URL and the query string as an object.
 
 @example
 ```
+import queryString = require('query-string');
+
 queryString.parseUrl('https://foo.bar?foo=bar');
 //=> {url: 'https://foo.bar', query: {foo: 'bar'}}
 ```
@@ -144,6 +164,8 @@ export interface StringifyOptions {
 	- `bracket`: Serialize arrays using bracket representation:
 
 		```
+		import queryString = require('query-string');
+
 		queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'bracket'});
 		//=> 'foo[]=1&foo[]=2&foo[]=3'
 		```
@@ -151,6 +173,8 @@ export interface StringifyOptions {
 	- `index`: Serialize arrays using index representation:
 
 		```
+		import queryString = require('query-string');
+
 		queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'index'});
 		//=> 'foo[0]=1&foo[1]=2&foo[2]=3'
 		```
@@ -158,6 +182,8 @@ export interface StringifyOptions {
 	- `comma`: Serialize arrays by separating elements with comma:
 
 		```
+		import queryString = require('query-string');
+
 		queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'comma'});
 		//=> 'foo=1,2,3'
 		```
@@ -165,6 +191,8 @@ export interface StringifyOptions {
 	- `none`: Serialize arrays by using duplicate keys:
 
 		```
+		import queryString = require('query-string');
+
 		queryString.stringify({foo: [1, 2, 3]});
 		//=> 'foo=1&foo=2&foo=3'
 		```
@@ -180,18 +208,49 @@ export interface StringifyOptions {
 
 	@example
 	```
+	import queryString = require('query-string');
+
 	const order = ['c', 'a', 'b'];
 
 	queryString.stringify({a: 1, b: 2, c: 3}, {
 		sort: (itemLeft, itemRight) => order.indexOf(itemLeft) - order.indexOf(itemRight)
 	});
 	//=> 'c=3&a=1&b=2'
+	```
+
+	@example
+	```
+	import queryString = require('query-string');
 
 	queryString.stringify({b: 1, c: 2, a: 3}, {sort: false});
 	//=> 'b=1&c=2&a=3'
 	```
 	*/
 	readonly sort?: ((itemLeft: string, itemRight: string) => number) | false;
+
+	/**
+	Skip keys with `null` as the value.
+
+	Note that keys with `undefined` as the value are always skipped.
+
+	@default false
+
+	@example
+	```
+	import queryString = require('query-string');
+
+	queryString.stringify({a: 1, b: undefined, c: null, d: 4}, {
+		skipNull: true
+	});
+	//=> 'a=1&d=4'
+
+	queryString.stringify({a: undefined, b: null}, {
+		skipNull: true
+	});
+	//=> ''
+	```
+	*/
+	readonly skipNull?: boolean;
 }
 
 /**
@@ -204,5 +263,26 @@ export function stringify(
 
 /**
 Extract a query string from a URL that can be passed into `.parse()`.
+
+Note: This behaviour can be changed with the `skipNull` option.
 */
 export function extract(url: string): string;
+
+/**
+Stringify an object into a URL with a query string and sorting the keys. The inverse of [`.parseUrl()`](https://github.com/sindresorhus/query-string#parseurlstring-options)
+
+Query items in the `query` property overrides queries in the `url` property.
+
+@example
+```
+queryString.stringifyUrl({url: 'https://foo.bar', query: {foo: 'bar'}});
+//=> 'https://foo.bar?foo=bar'
+
+queryString.stringifyUrl({url: 'https://foo.bar?foo=baz', query: {foo: 'bar'}});
+//=> 'https://foo.bar?foo=bar'
+```
+*/
+export function stringifyUrl(
+	object: ParsedUrl,
+	options?: StringifyOptions
+): string;
diff --git a/index.js b/index.js
index b122f27..d939e4a 100644
--- a/index.js
+++ b/index.js
@@ -8,7 +8,7 @@ function encoderForArrayFormat(options) {
 		case 'index':
 			return key => (result, value) => {
 				const index = result.length;
-				if (value === undefined) {
+				if (value === undefined || (options.skipNull && value === null)) {
 					return result;
 				}
 
@@ -24,7 +24,7 @@ function encoderForArrayFormat(options) {
 
 		case 'bracket':
 			return key => (result, value) => {
-				if (value === undefined) {
+				if (value === undefined || (options.skipNull && value === null)) {
 					return result;
 				}
 
@@ -36,12 +36,12 @@ function encoderForArrayFormat(options) {
 			};
 
 		case 'comma':
-			return key => (result, value, index) => {
+			return key => (result, value) => {
 				if (value === null || value === undefined || value.length === 0) {
 					return result;
 				}
 
-				if (index === 0) {
+				if (result.length === 0) {
 					return [[encode(key, options), '=', encode(value, options)].join('')];
 				}
 
@@ -50,7 +50,7 @@ function encoderForArrayFormat(options) {
 
 		default:
 			return key => (result, value) => {
-				if (value === undefined) {
+				if (value === undefined || (options.skipNull && value === null)) {
 					return result;
 				}
 
@@ -161,6 +161,16 @@ function removeHash(input) {
 	return input;
 }
 
+function getHash(url) {
+	let hash = '';
+	const hashStart = url.indexOf('#');
+	if (hashStart !== -1) {
+		hash = url.slice(hashStart);
+	}
+
+	return hash;
+}
+
 function extract(input) {
 	input = removeHash(input);
 	const queryStart = input.indexOf('?');
@@ -206,7 +216,7 @@ function parse(input, options) {
 	}
 
 	for (const param of input.split('&')) {
-		let [key, value] = splitOnFirst(param.replace(/\+/g, ' '), '=');
+		let [key, value] = splitOnFirst(options.decode ? param.replace(/\+/g, ' ') : param, '=');
 
 		// Missing `=` should be `null`:
 		// http://w3.org/TR/2012/WD-url-20120524/#collect-url-parameters
@@ -257,7 +267,17 @@ exports.stringify = (object, options) => {
 	}, options);
 
 	const formatter = encoderForArrayFormat(options);
-	const keys = Object.keys(object);
+
+	const objectCopy = Object.assign({}, object);
+	if (options.skipNull) {
+		for (const key of Object.keys(objectCopy)) {
+			if (objectCopy[key] === undefined || objectCopy[key] === null) {
+				delete objectCopy[key];
+			}
+		}
+	}
+
+	const keys = Object.keys(objectCopy);
 
 	if (options.sort !== false) {
 		keys.sort(options.sort);
@@ -290,3 +310,17 @@ exports.parseUrl = (input, options) => {
 		query: parse(extract(input), options)
 	};
 };
+
+exports.stringifyUrl = (input, options) => {
+	const url = removeHash(input.url).split('?')[0] || '';
+	const queryFromUrl = this.extract(input.url);
+	const parsedQueryFromUrl = this.parse(queryFromUrl);
+	const hash = getHash(input.url);
+	const query = Object.assign(parsedQueryFromUrl, input.query);
+	let queryString = this.stringify(query, options);
+	if (queryString) {
+		queryString = `?${queryString}`;
+	}
+
+	return `${url}${queryString}${hash}`;
+};
diff --git a/package.json b/package.json
index c70bf41..23dc5f3 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
 	"name": "query-string",
-	"version": "6.8.3",
+	"version": "6.9.0",
 	"description": "Parse and stringify URL query strings",
 	"license": "MIT",
 	"repository": "sindresorhus/query-string",
diff --git a/readme.md b/readme.md
index 297a47d..95770f3 100644
--- a/readme.md
+++ b/readme.md
@@ -2,6 +2,34 @@
 
 > Parse and stringify URL [query strings](https://en.wikipedia.org/wiki/Query_string)
 
+<br>
+
+---
+
+<div align="center">
+	<p>
+		<p>
+			<sup>
+				<a href="https://github.com/sponsors/sindresorhus">My open source work is supported by the community</a>
+			</sup>
+		</p>
+		<sup>Special thanks to:</sup>
+		<br>
+		<br>
+		<a href="https://github.com/botpress/botpress">
+			<img src="https://sindresorhus.com/assets/thanks/botpress-logo.svg" width="260" alt="Botpress">
+		</a>
+		<br>
+		<sub><b>Botpress is an open-source conversational assistant creation platform.</b></sub>
+		<br>
+		<sub>They <a href="https://github.com/botpress/botpress/blob/master/.github/CONTRIBUTING.md">welcome contributions</a> from anyone, whether you're into machine learning,<br>want to get started in open-source, or just have an improvement idea.</sub>
+		<br>
+	</p>
+</div>
+
+---
+
+<br>
 
 ## Install
 
@@ -9,7 +37,7 @@
 $ npm install query-string
 ```
 
-This module targets Node.js 6 or later and the latest version of Chrome, Firefox, and Safari. If you want support for older browsers, or, [if your project is using create-react-app v1](https://github.com/sindresorhus/query-string/pull/148#issuecomment-399656020), use version 5: `npm install query-string@5`.
+This module targets Node.js 6 or later and the latest version of Chrome, Firefox, and Safari. If you want support for older browsers, or, if your project is using create-react-app v1, use version 5: `npm install query-string@5`.
 
 
 ## Usage
@@ -58,19 +86,21 @@ Type: `object`
 
 ##### decode
 
-Type: `boolean`<br>
+Type: `boolean`\
 Default: `true`
 
 Decode the keys and values. URL components are decoded with [`decode-uri-component`](https://github.com/SamVerschueren/decode-uri-component).
 
 ##### arrayFormat
 
-Type: `string`<br>
+Type: `string`\
 Default: `'none'`
 
 - `'bracket'`: Parse arrays with bracket representation:
 
 ```js
+const queryString = require('query-string');
+
 queryString.parse('foo[]=1&foo[]=2&foo[]=3', {arrayFormat: 'bracket'});
 //=> {foo: ['1', '2', '3']}
 ```
@@ -78,6 +108,8 @@ queryString.parse('foo[]=1&foo[]=2&foo[]=3', {arrayFormat: 'bracket'});
 - `'index'`: Parse arrays with index representation:
 
 ```js
+const queryString = require('query-string');
+
 queryString.parse('foo[0]=1&foo[1]=2&foo[3]=3', {arrayFormat: 'index'});
 //=> {foo: ['1', '2', '3']}
 ```
@@ -85,6 +117,8 @@ queryString.parse('foo[0]=1&foo[1]=2&foo[3]=3', {arrayFormat: 'index'});
 - `'comma'`: Parse arrays with elements separated by comma:
 
 ```js
+const queryString = require('query-string');
+
 queryString.parse('foo=1,2,3', {arrayFormat: 'comma'});
 //=> {foo: ['1', '2', '3']}
 ```
@@ -92,23 +126,27 @@ queryString.parse('foo=1,2,3', {arrayFormat: 'comma'});
 - `'none'`: Parse arrays with elements using duplicate keys:
 
 ```js
+const queryString = require('query-string');
+
 queryString.parse('foo=1&foo=2&foo=3');
 //=> {foo: ['1', '2', '3']}
 ```
 
 ##### sort
 
-Type: `Function | boolean`<br>
+Type: `Function | boolean`\
 Default: `true`
 
 Supports both `Function` as a custom sorting function or `false` to disable sorting.
 
 ##### parseNumbers
 
-Type: `boolean`<br>
+Type: `boolean`\
 Default: `false`
 
 ```js
+const queryString = require('query-string');
+
 queryString.parse('foo=1', {parseNumbers: true});
 //=> {foo: 1}
 ```
@@ -117,17 +155,19 @@ Parse the value as a number type instead of string type if it's a number.
 
 ##### parseBooleans
 
-Type: `boolean`<br>
+Type: `boolean`\
 Default: `false`
 
 ```js
+const queryString = require('query-string');
+
 queryString.parse('foo=true', {parseBooleans: true});
 //=> {foo: true}
 ```
 
 Parse the value as a boolean type instead of string type if it's a boolean.
 
-### .stringify(object, [options])
+### .stringify(object, options?)
 
 Stringify an object into a query string and sorting the keys.
 
@@ -137,26 +177,28 @@ Type: `object`
 
 ##### strict
 
-Type: `boolean`<br>
+Type: `boolean`\
 Default: `true`
 
 Strictly encode URI components with [strict-uri-encode](https://github.com/kevva/strict-uri-encode). It uses [encodeURIComponent](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent) if set to false. You probably [don't care](https://github.com/sindresorhus/query-string/issues/42) about this option.
 
 ##### encode
 
-Type: `boolean`<br>
+Type: `boolean`\
 Default: `true`
 
 [URL encode](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent) the keys and values.
 
 ##### arrayFormat
 
-Type: `string`<br>
+Type: `string`\
 Default: `'none'`
 
 - `'bracket'`: Serialize arrays using bracket representation:
 
 ```js
+const queryString = require('query-string');
+
 queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'bracket'});
 //=> 'foo[]=1&foo[]=2&foo[]=3'
 ```
@@ -164,6 +206,8 @@ queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'bracket'});
 - `'index'`: Serialize arrays using index representation:
 
 ```js
+const queryString = require('query-string');
+
 queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'index'});
 //=> 'foo[0]=1&foo[1]=2&foo[2]=3'
 ```
@@ -171,6 +215,8 @@ queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'index'});
 - `'comma'`: Serialize arrays by separating elements with comma:
 
 ```js
+const queryString = require('query-string');
+
 queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'comma'});
 //=> 'foo=1,2,3'
 ```
@@ -178,6 +224,8 @@ queryString.stringify({foo: [1, 2, 3]}, {arrayFormat: 'comma'});
 - `'none'`: Serialize arrays by using duplicate keys:
 
 ```js
+const queryString = require('query-string');
+
 queryString.stringify({foo: [1, 2, 3]});
 //=> 'foo=1&foo=2&foo=3'
 ```
@@ -189,6 +237,8 @@ Type: `Function | boolean`
 Supports both `Function` as a custom sorting function or `false` to disable sorting.
 
 ```js
+const queryString = require('query-string');
+
 const order = ['c', 'a', 'b'];
 
 queryString.stringify({a: 1, b: 2, c: 3}, {
@@ -198,16 +248,47 @@ queryString.stringify({a: 1, b: 2, c: 3}, {
 ```
 
 ```js
+const queryString = require('query-string');
+
 queryString.stringify({b: 1, c: 2, a: 3}, {sort: false});
 //=> 'b=1&c=2&a=3'
 ```
 
 If omitted, keys are sorted using `Array#sort()`, which means, converting them to strings and comparing strings in Unicode code point order.
 
+##### skipNull
+
+Skip keys with `null` as the value.
+
+Note that keys with `undefined` as the value are always skipped.
+
+Type: `boolean`\
+Default: `false`
+
+```js
+const queryString = require('query-string');
+
+queryString.stringify({a: 1, b: undefined, c: null, d: 4}, {
+	skipNull: true
+});
+//=> 'a=1&d=4'
+```
+
+```js
+const queryString = require('query-string');
+
+queryString.stringify({a: undefined, b: null}, {
+	skipNull: true
+});
+//=> ''
+```
+
 ### .extract(string)
 
 Extract a query string from a URL that can be passed into `.parse()`.
 
+Note: This behaviour can be changed with the `skipNull` option.
+
 ### .parseUrl(string, options?)
 
 Extract the URL and the query string as an object.
@@ -217,10 +298,45 @@ The `options` are the same as for `.parse()`.
 Returns an object with a `url` and `query` property.
 
 ```js
+const queryString = require('query-string');
+
 queryString.parseUrl('https://foo.bar?foo=bar');
 //=> {url: 'https://foo.bar', query: {foo: 'bar'}}
 ```
 
+### .stringifyUrl(object, options?)
+
+Stringify an object into a URL with a query string and sorting the keys. The inverse of [`.parseUrl()`](https://github.com/sindresorhus/query-string#parseurlstring-options)
+
+The `options` are the same as for `.stringify()`.
+
+Returns a string with the URL and a query string.
+
+Query items in the `query` property overrides queries in the `url` property.
+
+```js
+queryString.stringifyUrl({url: 'https://foo.bar', query: {foo: 'bar'}});
+//=> 'https://foo.bar?foo=bar'
+
+queryString.stringifyUrl({url: 'https://foo.bar?foo=baz', query: {foo: 'bar'}});
+//=> 'https://foo.bar?foo=bar'
+```
+
+#### object
+
+Type: `object`
+
+##### url
+
+Type: `string`
+
+The URL to stringify.
+
+##### query
+
+Type: `object`
+
+Query items to add to the URL.
 
 ## Nesting
 
@@ -229,6 +345,8 @@ This module intentionally doesn't support nesting as it's not spec'd and varies
 You're much better off just converting the object to a JSON string:
 
 ```js
+const queryString = require('query-string');
+
 queryString.stringify({
 	foo: 'bar',
 	nested: JSON.stringify({
@@ -241,6 +359,8 @@ queryString.stringify({
 However, there is support for multiple instances of the same key:
 
 ```js
+const queryString = require('query-string');
+
 queryString.parse('likes=cake&name=bob&likes=icecream');
 //=> {likes: ['cake', 'icecream'], name: 'bob'}
 
@@ -254,6 +374,8 @@ queryString.stringify({color: ['taupe', 'chartreuse'], id: '515'});
 Sometimes you want to unset a key, or maybe just make it present without assigning a value to it. Here is how falsy values are stringified:
 
 ```js
+const queryString = require('query-string');
+
 queryString.stringify({foo: false});
 //=> 'foo=false'
 
@@ -265,14 +387,8 @@ queryString.stringify({foo: undefined});
 ```
 
 
----
+## query-string for enterprise
 
-<div align="center">
-	<b>
-		<a href="https://tidelift.com/subscription/pkg/npm-query-string?utm_source=npm-query-string&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
-	</b>
-	<br>
-	<sub>
-		Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
-	</sub>
-</div>
+Available as part of the Tidelift Subscription.
+
+The maintainers of query-string and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/npm-query-string?utm_source=npm-query-string&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)


Test patch:
diff --git a/index.test-d.ts b/index.test-d.ts
index 11e15c8..a4cb36d 100644
--- a/index.test-d.ts
+++ b/index.test-d.ts
@@ -22,6 +22,7 @@ expectType<string>(queryString.stringify({foo: 'bar'}, {arrayFormat: 'index'}));
 expectType<string>(queryString.stringify({foo: 'bar'}, {arrayFormat: 'none'}));
 expectType<string>(queryString.stringify({foo: 'bar'}, {arrayFormat: 'comma'}));
 expectType<string>(queryString.stringify({foo: 'bar'}, {sort: false}));
+expectType<string>(queryString.stringify({foo: 'bar'}, {skipNull: true}));
 const order = ['c', 'a', 'b'];
 expectType<string>(
 	queryString.stringify(
diff --git a/test/parse.js b/test/parse.js
index 5f714e3..f75a745 100644
--- a/test/parse.js
+++ b/test/parse.js
@@ -75,6 +75,10 @@ test('handle `+` correctly', t => {
 	t.deepEqual(queryString.parse('foo+faz=bar+baz++'), {'foo faz': 'bar baz  '});
 });
 
+test('handle `+` correctly when not decoding', t => {
+	t.deepEqual(queryString.parse('foo+faz=bar+baz++', {decode: false}), {'foo+faz': 'bar+baz++'});
+});
+
 test('handle multiple of the same key', t => {
 	t.deepEqual(queryString.parse('foo=bar&foo=baz'), {foo: ['bar', 'baz']});
 });
diff --git a/test/stringify-url.js b/test/stringify-url.js
new file mode 100644
index 0000000..2bde443
--- /dev/null
+++ b/test/stringify-url.js
@@ -0,0 +1,38 @@
+import test from 'ava';
+import queryString from '..';
+
+test('stringify URL without a query string', t => {
+	t.deepEqual(queryString.stringifyUrl({url: 'https://foo.bar/'}), 'https://foo.bar/');
+	t.deepEqual(queryString.stringifyUrl({url: 'https://foo.bar/', query: {}}), 'https://foo.bar/');
+	t.deepEqual(queryString.stringifyUrl({url: 'https://foo.bar/#top', query: {}}), 'https://foo.bar/#top');
+	t.deepEqual(queryString.stringifyUrl({url: '', query: {}}), '');
+	t.deepEqual(queryString.stringifyUrl({url: 'https://foo.bar?', query: {}}), 'https://foo.bar');
+	t.deepEqual(queryString.stringifyUrl({url: 'https://foo.bar?foo=bar', query: {}}), 'https://foo.bar?foo=bar');
+});
+
+test('stringify URL with a query string', t => {
+	t.deepEqual(queryString.stringifyUrl({url: 'https://foo.bar', query: {foo: 'bar'}}), 'https://foo.bar?foo=bar');
+	t.deepEqual(queryString.stringifyUrl({url: 'https://foo.bar?', query: {foo: 'bar'}}), 'https://foo.bar?foo=bar');
+	t.deepEqual(queryString.stringifyUrl({url: 'https://foo.bar/#top', query: {foo: 'bar'}}), 'https://foo.bar/?foo=bar#top');
+	t.deepEqual(queryString.stringifyUrl({url: 'https://foo.bar', query: {foo: 'bar', a: 'b'}}), 'https://foo.bar?a=b&foo=bar');
+	t.deepEqual(queryString.stringifyUrl({url: 'https://foo.bar?a=b', query: {foo: ['bar', 'baz']}}), 'https://foo.bar?a=b&foo=bar&foo=baz');
+	t.deepEqual(queryString.stringifyUrl({url: 'https://foo.bar?foo=baz', query: {foo: 'bar'}}), 'https://foo.bar?foo=bar');
+});
+
+test('stringify URL from the result of `parseUrl` without query string', t => {
+	const url = 'https://foo.bar';
+	const parsedUrl = queryString.parseUrl(url);
+	t.deepEqual(queryString.stringifyUrl(parsedUrl), url);
+});
+
+test('stringify URL from the result of `parseUrl` with query string', t => {
+	const url = 'https://foo.bar?foo=bar&foo=baz';
+	const parsedUrl = queryString.parseUrl(url);
+	t.deepEqual(queryString.stringifyUrl(parsedUrl), url);
+});
+
+test('stringify URL from the result of `parseUrl` with query string that contains `=`', t => {
+	const url = 'https://foo.bar?foo=bar=&foo=baz=';
+	const parsedUrl = queryString.parseUrl(url);
+	t.deepEqual(queryString.stringifyUrl(parsedUrl, {encode: false}), url);
+});
diff --git a/test/stringify.js b/test/stringify.js
index 8553ebd..09b6371 100644
--- a/test/stringify.js
+++ b/test/stringify.js
@@ -128,7 +128,7 @@ test('array stringify representation with array commas', t => {
 
 test('array stringify representation with array commas and null value', t => {
 	t.is(queryString.stringify({
-		foo: ['a', null, ''],
+		foo: [null, 'a', null, ''],
 		bar: [null]
 	}, {
 		arrayFormat: 'comma'
@@ -193,3 +193,69 @@ test('should disable sorting', t => {
 		sort: false
 	}), 'c=foo&b=bar&a=baz');
 });
+
+test('should ignore null when skipNull is set', t => {
+	t.is(queryString.stringify({
+		a: 1,
+		b: null,
+		c: 3
+	}, {
+		skipNull: true
+	}), 'a=1&c=3');
+});
+
+test('should ignore undefined when skipNull is set', t => {
+	t.is(queryString.stringify({
+		a: 1,
+		b: undefined,
+		c: 3
+	}, {
+		skipNull: true
+	}), 'a=1&c=3');
+});
+
+test('should ignore both null and undefined when skipNull is set', t => {
+	t.is(queryString.stringify({
+		a: undefined,
+		b: null
+	}, {
+		skipNull: true
+	}), '');
+});
+
+test('should ignore both null and undefined when skipNull is set for arrayFormat', t => {
+	t.is(queryString.stringify({
+		a: [undefined, null, 1, undefined, 2, null],
+		b: null,
+		c: 1
+	}, {
+		skipNull: true
+	}), 'a=1&a=2&c=1');
+
+	t.is(queryString.stringify({
+		a: [undefined, null, 1, undefined, 2, null],
+		b: null,
+		c: 1
+	}, {
+		skipNull: true,
+		arrayFormat: 'bracket'
+	}), 'a[]=1&a[]=2&c=1');
+
+	t.is(queryString.stringify({
+		a: [undefined, null, 1, undefined, 2, null],
+		b: null,
+		c: 1
+	}, {
+		skipNull: true,
+		arrayFormat: 'comma'
+	}), 'a=1,2&c=1');
+
+	t.is(queryString.stringify({
+		a: [undefined, null, 1, undefined, 2, null],
+		b: null,
+		c: 1
+	}, {
+		skipNull: true,
+		arrayFormat: 'index'
+	}), 'a[0]=1&a[1]=2&c=1');
+});