rackstring

1.0.9 • Public • Published

rackstring

A drop-in replacement for Node's querystring module that parses query strings as Rack does, with support for nested objects and nested arrays.

Why?

There are a couple of popular alternatives to Node's querystring module, namely qs and query-string. Unfortunately:

At Code Climate, we use Rack (vis-a-vis Rails) to handle web requests. Many of our client-to-server transactions rely on conforming query string formats, and neither alternative module supports parsing complex query strings in a Rack-compliant way. I explored forking qs and adding an option to support this sort of parsing, but adding this support proved to be non-trivial. Hence, rackstring was born.

Installation

npm install rackstring ---save

Usage

Parsing query strings

const querystring = require("rackstring")
 
const obj = querystring.parse("foo=bar")
assert.deepEqual(obj, { foo: "bar" })

As promised, rackstring supports parsing bracket-formatted arrays:

const obj = querystring.parse("foo[]=bar&foo[]=baz")
assert.deepEqual(obj, { foo: ["bar", "baz"] })

Additionally, rackstring parses complex arrays:

const obj = querystring.parse("foo[][a]=bar&foo[][b]=baz&foo[a]=meow&foo[b]=ruff")
assert.deepEqual(obj, { foo: [{ a: "bar", b: "baz"}, {a: "meow", b: "ruff"}] })

Stringifying objects

const str = querystring.stringify({ foo: [{ a: "bar", b: "baz"}, {a: "meow", b: "ruff"}] })
assert.equal(str, "foo[][a]=bar&foo[][b]=baz&foo[a]=meow&foo[b]=ruff")

Optionally, you can URL-encode keys with the encodeKeys option:

const str = querystring.stringify({ foo: [{ a: "bar", b: "baz"}, {a: "meow", b: "ruff"}] }, { encodeKeys: true })
assert.equal(str, "foo%5B%5D%5Ba%5D=bar&foo%5B%5D%5Bb%5D=baz&foo%5B%5D%5Ba%5D=meow&foo%5B%5D%5Bb%5D=ruff")

License

See LICENSE.

Package Sidebar

Install

npm i rackstring

Weekly Downloads

29

Version

1.0.9

License

MIT

Unpacked Size

18.5 kB

Total Files

15

Last publish

Collaborators

  • nporteschaikin