Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Eran Hammer committed Aug 4, 2014
1 parent f00bff7 commit 2007b86
Show file tree
Hide file tree
Showing 10 changed files with 3,415 additions and 176 deletions.
11 changes: 5 additions & 6 deletions LICENSE
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
Copyright (c) 2014 Nathan LaFreniere and other contributors

Copyright (c) 2014 Nathan LaFreniere and other contributors.
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand All @@ -9,9 +8,9 @@ modification, are permitted provided that the following conditions are met:
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of Walmart nor the names of any contributors may be
used to endorse or promote products derived from this software without
specific prior written permission.
* The names of any contributors may not be used to endorse or promote
products derived from this software without specific prior written
permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
Expand All @@ -26,4 +25,4 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

* * *

The complete list of contributors can be found at: https://github.com/hapijs/riddler/graphs/contributors
The complete list of contributors can be found at: https://github.com/hapijs/qs/graphs/contributors
70 changes: 36 additions & 34 deletions README.md
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
<a href="https://github.com/hapijs"><img src="https://raw.github.com/spumko/spumko/master/images/from.png" align="right" /></a>
# qs

Riddler is a querystring parsing and stringifying library with some added security
A querystring parsing and stringifying library with some added security.

[![Build Status](https://secure.travis-ci.org/hapijs/riddler.svg)](http://travis-ci.org/hapijs/riddler)
[![Build Status](https://secure.travis-ci.org/hapijs/qs.svg)](http://travis-ci.org/hapijs/qs)

Lead Maintainer: [Nathan LaFreniere](https://github.com/nlf)

The **qs** module was original created and maintained by [TJ Holowaychuk](https://github.com/visionmedia/node-querystring).

## Usage

```javascript
var Riddler = require('riddler');
var Qs = require('qs');

var obj = Riddler.parse('a=c');
console.log(obj); // { a: 'c' }

var str = Riddler.stringify(obj);
console.log(str); // 'a=c'
var obj = Qs.parse('a=c'); // { a: 'c' }
var str = Qs.stringify(obj); // 'a=c'
```

### Objects

Riddler allows you to create nested objects within your query strings, by surrounding the name of sub-keys with square brackets `[]`.

For example, in order to create an object:
**qs** allows you to create nested objects within your query strings, by surrounding the name of sub-keys with square brackets `[]`.
For example, the string `'foo[bar]=baz'` converts to:

```javascript
{
Expand All @@ -33,16 +30,20 @@ For example, in order to create an object:
}
```

One would use the string `foo[bar]=baz`.

You can also nest your objects:
You can also nest your objects, like `'foo[bar][baz]=foobarbaz'`:

```javascript
var obj = Riddler.parse('foo[bar][baz]=foobarbaz');
// obj = { foo: { bar: { baz: 'foobarbaz' } } }
{
foo: {
bar: {
baz: 'foobarbaz'
}
}
}
```

By default, when nesting objects riddler will only parse up to 5 children deep. This means if you attempt to parse a string like `a[b][c][d][e][f][g][h][i]=j` your resulting object will be:
By default, when nesting objects **qs** will only parse up to 5 children deep. This means if you attempt to parse a string like
`'a[b][c][d][e][f][g][h][i]=j'` your resulting object will be:

```javascript
{
Expand All @@ -62,57 +63,58 @@ By default, when nesting objects riddler will only parse up to 5 children deep.
}
```

This depth can be overridden by passing a `depth` parameter to `riddler.parse()`:
This depth can be overridden by passing a `depth` option to `Qs.parse(string, depth)`:

```javascript
Riddler.parse('a[b][c][d][e][f][g][h][i]=j', 1);
Qs.parse('a[b][c][d][e][f][g][h][i]=j', 1);
// { a: { b: { '[c][d][e][f][g][h][i]': 'j' } } }
```

Having this limit helps mitigate abuse when riddler is used to parse user input, and it is recommended to keep it a reasonably small number.
The depth limit mitigate abuse when **qs** is used to parse user input, and it is recommended to keep it a reasonably small number.

### Arrays

Riddler can also parse arrays using a similar `[]` notation:
**qs** can also parse arrays using a similar `[]` notation:

```javascript
Riddler.parse('a[]=b&a[]=c');
Qs.parse('a[]=b&a[]=c');
// { a: ['b', 'c'] }
```

You may specify an index as well:

```javascript
Riddler.parse('a[1]=c&a[0]=b');
Qs.parse('a[1]=c&a[0]=b');
// { a: ['b', 'c'] }
```

Note that the only difference between an index in an array and a key in an object is that the value between the brackets must be a number to create an array.

When creating arrays with specific indices, riddler will compact a sparse array to only the existing values preserving their order:
Note that the only difference between an index in an array and a key in an object is that the value between the brackets must be a number
to create an array. When creating arrays with specific indices, **qs** will compact a sparse array to only the existing values preserving
their order:

```javascript
Riddler.parse('a[1]=b&a[15]=c');
Qs.parse('a[1]=b&a[15]=c');
// { a: ['b', 'c'] }
```

Riddler will also limit specifying indices in an array to a maximum index of `20`. Any array members with an index of greater than `20` will instead be converted to an object with the index as the key:
**qs** will also limit specifying indices in an array to a maximum index of `20`. Any array members with an index of greater than `20` will
instead be converted to an object with the index as the key:

```javascript
Riddler.parse('a[100]=b');
Qs.parse('a[100]=b');
// { a: { '100': 'b' } }
```

If you mix notations, riddler will merge the two items into an object:
If you mix notations, **qs** will merge the two items into an object:

```javascript
Riddler.parse('a[0]=b&a[b]=c');
Qs.parse('a[0]=b&a[b]=c');
// { a: { '0': 'b', b: 'c' } }
```

You can also create arrays of objects:

```javascript
Riddler.parse('a[][b]=c');
Qs.parse('a[][b]=c');
// { a: [{ b: 'c' }] }
```
```
Loading

0 comments on commit 2007b86

Please sign in to comment.