sabberworm / deparam.js

Deparam converts a querystring into a JavaScript object

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

GitHub package.json version GitHub Build Status

Deparam.js

Deparam.js is a lightweight query string to JavaScript object converter

Install

npm install --save deparam.js

Usage

ES6

import deparam from 'deparam.js';
deparam(...);

CommonJS

const deparam = require('deparam.js');
deparam(...);

Browser

<script src="deparam.js"></script>
<script>
  deparam(...);
</script>

How it works?

Deparam can convert simple as well as complex query strings to regular JavaScript objects. Examples are shown below:

Simple string

deparam('?a=10&b=helloworld'); // --> { a: 10, b: 'helloworld'}

Complex string

deparam('a=10&a=20&b=test&c=test2&x[]=45&x[]=99&y[a]=22&y[b]=33'); 

// --> { a: [10, 20], b: 'test', c: 'test2', x: [99, 22], y: { a: 22, b: 33 } }

Disable type coercion

Deparam enables type coercion by default. However this can have performance implications and it's better to disable it. You can do so by passing a flag.

deparam('a=10&b=20', false); // --> { a: '10', b: '20' }

About

Deparam converts a querystring into a JavaScript object

License:MIT License


Languages

Language:JavaScript 100.0%