Mithril 2.0.0-rc.6

parseQueryString(string)


Description

Turns a string of the form ?a=1&b=2 to an object

var object = m.parseQueryString("a=1&b=2")
// {a: "1", b: "2"}

Signature

object = m.parseQueryString(string, object)

Argument Type Required Description
string String Yes A querystring
object Object No An existing key-value map to merge values into, potentially from a previous m.parseQueryString call
returns Object A key-value map, object if provided

How to read signatures


How it works

The m.parseQueryString method creates an object from a querystring. It is useful for handling data from URL

var data = m.parseQueryString("a=hello&b=world")

// data is {a: "hello", b: "world"}

Boolean type casting

This method attempts to cast boolean values if possible. This helps prevents bugs related to loose truthiness and unintended type casts.

var data = m.parseQueryString("a=true&b=false")

// data is {a: true, b: false}

Leading question mark tolerance

For convenience, the m.parseQueryString method ignores a leading question mark, if present:

var data = m.parseQueryString("?a=hello&b=world")

// data is {a: "hello", b: "world"}

Deep data structures

Querystrings that contain bracket notation are correctly parsed into deep data structures

m.parseQueryString("a[0]=hello&a[1]=world")

// data is {a: ["hello", "world"]}

License: MIT. © Leo Horie.