Html query string parse

query-string

For browser usage, this package targets the latest version of Chrome, Firefox, and Safari.

Usage

import queryString from 'query-string'; console.log(location.search); //=> '?foo=bar' const parsed = queryString.parse(location.search); console.log(parsed); //=> console.log(location.hash); //=> '#token=bada55cafe' const parsedHash = queryString.parse(location.hash); console.log(parsedHash); //=> parsed.foo = 'unicorn'; parsed.ilike = 'pizza'; const stringified = queryString.stringify(parsed); //=> 'foo=unicorn&ilike=pizza' location.search = stringified; // note that `location.search` automatically prepends a question mark console.log(location.search); //=> '?foo=unicorn&ilike=pizza'

API

.parse(string, options?)

Parse a query string into an object. Leading ? or # are ignored, so you can pass location.search or location.hash directly.

The returned object is created with Object.create(null) and thus does not have a prototype .

options

decode

Type: boolean
Default: true

Decode the keys and values. URL components are decoded with decode-uri-component .

arrayFormat
import queryString from 'query-string'; queryString.parse('foo[]=1&foo[]=2&foo[]=3', arrayFormat: 'bracket'>); //=>
import queryString from 'query-string'; queryString.parse('foo[0]=1&foo[1]=2&foo[3]=3', arrayFormat: 'index'>); //=>
import queryString from 'query-string'; queryString.parse('foo=1,2,3', arrayFormat: 'comma'>); //=>
import queryString from 'query-string'; queryString.parse('foo=1|2|3', arrayFormat: 'separator', arrayFormatSeparator: '|'>); //=>
  • ‘bracket-separator’ : Parse arrays (that are explicitly marked with brackets) with elements separated by a custom character:
import queryString from 'query-string'; queryString.parse('foo[]', arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'>); //=> queryString.parse('foo[]=', arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'>); //=> queryString.parse('foo[]=1', arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'>); //=> queryString.parse('foo[]=1|2|3', arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'>); //=> queryString.parse('foo[]=1||3|||6', arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'>); //=> queryString.parse('foo[]=1|2|3&bar=fluffy&baz[]=4', arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'>); //=>
import queryString from 'query-string'; queryString.parse('foo:list=one&foo:list=two', arrayFormat: 'colon-list-separator'>); //=>
import queryString from 'query-string'; queryString.parse('foo=1&foo=2&foo=3'); //=>
arrayFormatSeparator

The character used to separate array elements when using .

sort

Type: Function | boolean
Default: true

Supports both Function as a custom sorting function or false to disable sorting.

parseNumbers

Type: boolean
Default: false

import queryString from 'query-string'; queryString.parse('foo=1', parseNumbers: true>); //=>

Parse the value as a number type instead of string type if it’s a number.

parseBooleans

Type: boolean
Default: false

import queryString from 'query-string'; queryString.parse('foo=true', parseBooleans: true>); //=>

Parse the value as a boolean type instead of string type if it’s a boolean.

.stringify(object, options?)

Stringify an object into a query string and sorting the keys.

options

strict

Type: boolean
Default: true

Strictly encode URI components with strict-uri-encode. It uses encodeURIComponent if set to false. You probably don’t care about this option.

encode

Type: boolean
Default: true

arrayFormat
import queryString from 'query-string'; queryString.stringify(foo: [1, 2, 3]>, arrayFormat: 'bracket'>); //=> 'foo[]=1&foo[]=2&foo[]=3'
import queryString from 'query-string'; queryString.stringify(foo: [1, 2, 3]>, arrayFormat: 'index'>); //=> 'foo[0]=1&foo[1]=2&foo[2]=3'
import queryString from 'query-string'; queryString.stringify(foo: [1, 2, 3]>, arrayFormat: 'comma'>); //=> 'foo=1,2,3' queryString.stringify(foo: [1, null, '']>, arrayFormat: 'comma'>); //=> 'foo=1,,' // Note that typing information for null values is lost // and `.parse('foo=1,,')` would return ``.
import queryString from 'query-string'; queryString.stringify(foo: [1, 2, 3]>, arrayFormat: 'separator', arrayFormatSeparator: '|'>); //=> 'foo=1|2|3'
  • ‘bracket-separator’ : Serialize arrays by explicitly post-fixing array names with brackets and separating elements with a custom character:
import queryString from 'query-string'; queryString.stringify(foo: []>, arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'>); //=> 'foo[]' queryString.stringify(foo: ['']>, arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'>); //=> 'foo[]=' queryString.stringify(foo: [1]>, arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'>); //=> 'foo[]=1' queryString.stringify(foo: [1, 2, 3]>, arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'>); //=> 'foo[]=1|2|3' queryString.stringify(foo: [1, '', 3, null, null, 6]>, arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'>); //=> 'foo[]=1||3|||6' queryString.stringify(foo: [1, '', 3, null, null, 6]>, arrayFormat: 'bracket-separator', arrayFormatSeparator: '|', skipNull: true>); //=> 'foo[]=1||3|6' queryString.stringify(foo: [1, 2, 3], bar: 'fluffy', baz: [4]>, arrayFormat: 'bracket-separator', arrayFormatSeparator: '|'>); //=> 'foo[]=1|2|3&bar=fluffy&baz[]=4'
  • ‘colon-list-separator’ : Serialize arrays with parameter names that are explicitly marked with :list :
import queryString from 'query-string'; queryString.stringify(foo: ['one', 'two']>, arrayFormat: 'colon-list-separator'>); //=> 'foo:list=one&foo:list=two'
import queryString from 'query-string'; queryString.stringify(foo: [1, 2, 3]>); //=> 'foo=1&foo=2&foo=3'
arrayFormatSeparator

The character used to separate array elements when using .

sort

Supports both Function as a custom sorting function or false to disable sorting.

import queryString from 'query-string'; const order = ['c', 'a', 'b']; queryString.stringify(a: 1, b: 2, c: 3>,  sort: (a, b) => order.indexOf(a) - order.indexOf(b) >); //=> 'c=3&a=1&b=2'
import queryString from '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

import queryString from 'query-string'; queryString.stringify(a: 1, b: undefined, c: null, d: 4>,  skipNull: true >); //=> 'a=1&d=4'
import queryString from 'query-string'; queryString.stringify(a: undefined, b: null>,  skipNull: true >); //=> ''
skipEmptyString

Skip keys with an empty string as the value.

Type: boolean
Default: false

import queryString from 'query-string'; queryString.stringify(a: 1, b: '', c: '', d: 4>,  skipEmptyString: true >); //=> 'a=1&d=4'
import queryString from 'query-string'; queryString.stringify(a: '', b: ''>,  skipEmptyString: 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.

Returns an object with a url and query property.

If the parseFragmentIdentifier option is true , the object will also contain a fragmentIdentifier property.

import queryString from 'query-string'; queryString.parseUrl('https://foo.bar?foo=bar'); //=> > queryString.parseUrl('https://foo.bar?foo=bar#xyz', parseFragmentIdentifier: true>); //=> , fragmentIdentifier: 'xyz'>

options

The options are the same as for .parse() .

Extra options are as below.

parseFragmentIdentifier

Parse the fragment identifier from the URL.

Type: boolean
Default: false

import queryString from 'query-string'; queryString.parseUrl('https://foo.bar?foo=bar#xyz', parseFragmentIdentifier: true>); //=> , fragmentIdentifier: 'xyz'>

.stringifyUrl(object, options?)

Stringify an object into a URL with a query string and sorting the keys. The inverse of .parseUrl()

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.

The fragmentIdentifier property overrides the fragment identifier in the url property.

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' queryString.stringifyUrl( url: 'https://foo.bar', query:  top: 'foo' >, fragmentIdentifier: 'bar' >); //=> 'https://foo.bar?top=foo#bar'

Источник

Node.js v20.5.0 documentation

The node:querystring module provides utilities for parsing and formatting URL query strings. It can be accessed using:

const querystring = require('node:querystring'); 

querystring.decode() #

The querystring.decode() function is an alias for querystring.parse() .

querystring.encode() #

The querystring.encode() function is an alias for querystring.stringify() .

querystring.escape(str) #

The querystring.escape() method performs URL percent-encoding on the given str in a manner that is optimized for the specific requirements of URL query strings.

The querystring.escape() method is used by querystring.stringify() and is generally not expected to be used directly. It is exported primarily to allow application code to provide a replacement percent-encoding implementation if necessary by assigning querystring.escape to an alternative function.

querystring.parse(str[, sep[, eq[, options]]]) #

Multiple empty entries are now parsed correctly (e.g. &=&= ).

The returned object no longer inherits from Object.prototype .

The eq parameter may now have a length of more than 1 .

The querystring.parse() method parses a URL query string ( str ) into a collection of key and value pairs.

For example, the query string ‘foo=bar&abc=xyz&abc=123’ is parsed into:

< foo: 'bar', abc: ['xyz', '123'] > 

The object returned by the querystring.parse() method does not prototypically inherit from the JavaScript Object . This means that typical Object methods such as obj.toString() , obj.hasOwnProperty() , and others are not defined and will not work.

By default, percent-encoded characters within the query string will be assumed to use UTF-8 encoding. If an alternative character encoding is used, then an alternative decodeURIComponent option will need to be specified:

// Assuming gbkDecodeURIComponent function already exists. querystring.parse('w=%D6%D0%CE%C4&foo=bar', null, null, < decodeURIComponent: gbkDecodeURIComponent >); 

querystring.stringify(obj[, sep[, eq[, options]]]) #

The querystring.stringify() method produces a URL query string from a given obj by iterating through the object’s «own properties».

querystring.stringify(< foo: 'bar', baz: ['qux', 'quux'], corge: '' >); // Returns 'foo=bar&baz=qux&baz=quux&corge=' querystring.stringify(< foo: 'bar', baz: 'qux' >, ';', ':'); // Returns 'foo:bar;baz:qux' 

By default, characters requiring percent-encoding within the query string will be encoded as UTF-8. If an alternative encoding is required, then an alternative encodeURIComponent option will need to be specified:

// Assuming gbkEncodeURIComponent function already exists, querystring.stringify(< w: '中文', foo: 'bar' >, null, null, < encodeURIComponent: gbkEncodeURIComponent >); 

querystring.unescape(str) #

The querystring.unescape() method performs decoding of URL percent-encoded characters on the given str .

The querystring.unescape() method is used by querystring.parse() and is generally not expected to be used directly. It is exported primarily to allow application code to provide a replacement decoding implementation if necessary by assigning querystring.unescape to an alternative function.

By default, the querystring.unescape() method will attempt to use the JavaScript built-in decodeURIComponent() method to decode. If that fails, a safer equivalent that does not throw on malformed URLs will be used.

Источник

Читайте также:  Php method call stack
Оцените статью