JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 77
  • Score
    100M100P100Q65458F
  • License MIT

A query string encoder and decoder. Can be used to encode arrays and objects.

Package Exports

  • x-query-string
  • x-query-string/encode
  • x-query-string/encode.js
  • x-query-string/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (x-query-string) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

querystring.js

npm version Build Status install size npm downloads

A query string encoder and decoder. It works like the $.param(...) function of jQuery and has the ability to decode the query string. Can be used in Node.js and browser side.

Features:

  • Encode & Decode array (nested)
  • Encode & Decode object (nested)

API:

Install

If you are using npm, just install x-query-string as a dependency.

npm i x-query-string

Otherwise, you can import the bundle file with script tag directly.

<script scr="path/to/querystring.min.js"></script>

The bundle file also can be used as an AMD module, which means it can be loaded by require.js.

Example

var QS = require('x-query-string');

// a=1&b=2
QS.encode({ a: 1, b: 2 });

// a%5B%5D=1&a%5B%5D=2&a%5B%5D=3 (a[]=1&a[]=2&a[]=3)
QS.encode({ a: [1, 2, 3] });

// a%5Bb%5D%5Bc%5D=3 (a[b][c]=3)
QS.encode({ a: { b: { c: 3 } } });

For npm users, if you just want to include only the encode or decode function, you can do this as the following example.

// include encode only
var encode = require('x-query-string/encode');

Or

// include decode only
var decode = require('x-query-string/decode');

This is helpful when you want to reduce the bundle size of your application when you just use encode or decode.

API

QS.encode(object, [keepArrayIndex])

  • object {Object} The data to be encoded to query string
  • boolean {keepArrayIndex} Whether to always keep array index in the query string. If the array to be encoded just has one dimension, the index can be omitted. The default value is false.
  • Returns: {string} Returns the URI component encoded query string

Encode the data to query string.

var QS = require('x-query-string');

// a%5B%5D=1&a%5B%5D=2 (a[]=1&a[]=2)
QS.encode({ a: [1, 2] });

// a%5B0%5D=1&a%5B1%5D=2 (a[0]=1&a[1]=2)
QS.encode({ a: [1, 2] }, true);

QS.decode(string)

  • string {string} The query string to be decoded
  • Returns {Object} Returns the decoded data

Decode the query string to a data object. The values in the result data object is string or null. This method will NOT try to parse number or boolean values.

var QS = require('x-query-string');

QS.decode('a[]=1&a[]=2&b=false&c[d]=1&e=&f');
// or (The query string below is url-encoded)
QS.decode('a%5B%5D=1&a%5B%5D=2&b=false&c%5Bd%5D=1&e=&f');

result:

{
    "a": [
        "1",
        "2"
    ],
    "b": "false",
    "c": {
        "d": "1"
    },
    "e": "",
    "f": null
}

License

MIT