can-deparam
Deserialize a query string into an array or object.
deparam(params)
Parameters
- params
{String}
:A form-urlencoded string of key-value pairs.
- valueDeserializer
{function}
:A function that decodes the string values. For example, using can-string-to-any will convert
"null"
tonull
like:import stringToAny from "can-string-to-any"; deparam("value=null", stringToAny) //-> {value: null}
Returns
{Object}
:
The params formatted into an object
Takes a string of name value pairs and returns a Object literal that represents those params.
var deparam = require("can-deparam");
console.log(JSON.stringify(deparam("?foo=bar&number=1234"))); // -> '{"foo" : "bar", "number": 1234}'
console.log(JSON.stringify(deparam("#foo[]=bar&foo[]=baz"))); // -> '{"foo" : ["bar", "baz"]}'
console.log(JSON.stringify(deparam("foo=bar%20%26%20baz"))); // -> '{"foo" : "bar & baz"}'
Try it
Run the following example on CodePen to play around with this package:
<script type="module">
import { deparam } from "can";
var queryString = 'canjs=awesome%20%26%20great';
var parameters = deparam(queryString);
console.log(parameters);
</script>