query-string
Advanced tools
Comparing version 6.9.0 to 6.10.0
@@ -270,1 +270,20 @@ export interface ParseOptions { | ||
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; |
24
index.js
@@ -164,2 +164,12 @@ 'use strict'; | ||
function getHash(url) { | ||
let hash = ''; | ||
const hashStart = url.indexOf('#'); | ||
if (hashStart !== -1) { | ||
hash = url.slice(hashStart); | ||
} | ||
return hash; | ||
} | ||
function extract(input) { | ||
@@ -304,1 +314,15 @@ input = removeHash(input); | ||
}; | ||
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}`; | ||
}; |
{ | ||
"name": "query-string", | ||
"version": "6.9.0", | ||
"version": "6.10.0", | ||
"description": "Parse and stringify URL query strings", | ||
"license": "MIT", | ||
"repository": "sindresorhus/query-string", | ||
"funding": "https://github.com/sponsors/sindresorhus", | ||
"author": { | ||
@@ -8,0 +9,0 @@ "name": "Sindre Sorhus", |
@@ -5,3 +5,31 @@ # query-string [![Build Status](https://travis-ci.org/sindresorhus/query-string.svg?branch=master)](https://travis-ci.org/sindresorhus/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 | ||
@@ -13,5 +41,4 @@ | ||
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 | ||
@@ -48,3 +75,2 @@ | ||
## API | ||
@@ -282,3 +308,36 @@ | ||
### .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 | ||
@@ -314,3 +373,2 @@ | ||
## Falsy values | ||
@@ -333,3 +391,2 @@ | ||
## query-string for enterprise | ||
@@ -336,0 +393,0 @@ |
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
26506
471
391