Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Nov 13, 2013
0 parents commit ddd0bd1
Show file tree
Hide file tree
Showing 11 changed files with 278 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# editorconfig.org
root = true

[*]
indent_style = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
20 changes: 20 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"node": true,
"esnext": true,
"bitwise": true,
"camelcase": true,
"curly": true,
"eqeqeq": true,
"immed": true,
"indent": 4,
"latedef": true,
"newcap": true,
"noarg": true,
"quotmark": "single",
"regexp": true,
"undef": true,
"unused": true,
"strict": true,
"trailing": true,
"smarttabs": true
}
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
language: node_js
node_js:
- '0.10'
- '0.8'
26 changes: 26 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "query-string",
"version": "0.0.0",
"description": "Parse and stringify URL query strings",
"main": "query-string.js",
"keywords": [
"querystring",
"query",
"string",
"qs",
"param",
"parameter",
"url",
"uri",
"parse",
"stringify",
"encode",
"decode"
],
"ignore": [
".*",
"test.js",
"component.json",
"package.json"
]
}
23 changes: 23 additions & 0 deletions component.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "query-string",
"version": "0.0.0",
"description": "Parse and stringify URL query strings",
"repo": "sindresorhus/query-string",
"keywords": [
"querystring",
"query",
"string",
"qs",
"param",
"parameter",
"url",
"uri",
"parse",
"stringify",
"encode",
"decode"
],
"main": "query-string.js",
"scripts": ["query-string.js"],
"license": "MIT"
}
43 changes: 43 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "query-string",
"version": "0.0.0",
"description": "Parse and stringify URL query strings",
"keywords": [
"browser",
"querystring",
"query",
"string",
"qs",
"param",
"parameter",
"url",
"uri",
"parse",
"stringify",
"encode",
"decode"
],
"license": "MIT",
"author": {
"name": "Sindre Sorhus",
"email": "[email protected]",
"url": "http://sindresorhus.com"
},
"files": [
"query-string.js"
],
"main": "query-string",
"repository": {
"type": "git",
"url": "git://github.com/sindresorhus/query-string.git"
},
"scripts": {
"test": "mocha"
},
"devDependencies": {
"mocha": "~1.14.0"
},
"engines": {
"node": ">=0.8.0"
}
}
43 changes: 43 additions & 0 deletions query-string.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*!
query-string
Parse and stringify URL query strings
https://github.com/sindresorhus/query-string
by Sindre Sorhus
MIT License
*/
(function () {
'use strict';
var queryString = {};

queryString.parse = function (str) {
if (typeof str !== 'string') {
return {};
}

str = str.trim().replace(/^\?/, '');

if (!str) {
return {};
}

return str.trim().split('&').reduce(function (ret, param) {
var parts = param.split('=');
// missing `=` should be `null`:
// http://w3.org/TR/2012/WD-url-20120524/#collect-url-parameters
ret[parts[0]] = parts[1] === undefined ? null : decodeURIComponent(parts[1]);
return ret;
}, {});
};

queryString.stringify = function (obj) {
return obj ? Object.keys(obj).map(function (key) {
return encodeURIComponent(key) + '=' + encodeURIComponent(obj[key]);
}).join('&') : '';
};

if (typeof module !== 'undefined' && module.exports) {
module.exports = queryString;
} else {
window.queryString = queryString;
}
})();
59 changes: 59 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# query-string [![Build Status](https://secure.travis-ci.org/sindresorhus/query-string.png?branch=master)](http://travis-ci.org/sindresorhus/query-string)

> Parse and stringify URL [query strings](http://en.wikipedia.org/wiki/Query_string)

## Install

Download [manually](https://github.com/sindresorhus/query-string/releases) or with a package-manager.

#### [npm](https://npmjs.org/package/query-string)

```
npm install --save query-string
```

#### [Bower](http://bower.io)

```
bower install --save query-string
```

#### [Component](https://github.com/component/component)

```
component install sindresorhus/query-string
```


## Example using Node.js

```js
var queryString = require('query-string');

var url = 'http://sindresorhus.com?foo=bar'.split('?');
var parsed = queryString.parse(url[1]);
console.log(parsed);
// {foo: 'bar'}

parsed.foo = 'unicorn';
parsed.ilike = 'pizza';
console.log(url[0] + '?' + queryString.stringify(parsed));
// http://sindresorhus.com?foo=unicorn&ilike=pizza
```


## API

### queryString.parse()

Parse a query string into an object.

### queryString.stringify()

Stringify an object into a query string.


## License

MIT © [Sindre Sorhus](http://sindresorhus.com)
46 changes: 46 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*global describe, it */
'use strict';
var assert = require('assert');
var qs = require('./query-string');


describe('.parse()', function () {
it('should handle query strings starting with a `?`', function () {
assert.deepEqual(qs.parse('?foo=bar'), {foo: 'bar'});
});

it('should parse a qseter', function () {
assert.deepEqual(qs.parse('foo=bar'), {foo: 'bar'});
});

it('should parse multiple qseters', function () {
assert.deepEqual(qs.parse('foo=bar&key=val'), {foo: 'bar', key: 'val'});
});

it('should parse qseters without a value', function () {
assert.deepEqual(qs.parse('foo'), {foo: null});
assert.deepEqual(qs.parse('foo&key'), {foo: null, key: null});
assert.deepEqual(qs.parse('foo=bar&key'), {foo: 'bar', key: null});
});

it('should return empty object if no qss can be found', function () {
assert.deepEqual(qs.parse('?'), {});
assert.deepEqual(qs.parse(' '), {});
});
});

describe('.stringify()', function () {
it('should stringify', function () {
assert.strictEqual(qs.stringify({foo: 'bar'}), 'foo=bar');
assert.strictEqual(qs.stringify({foo: 'bar', bar: 'baz'}), 'foo=bar&bar=baz');
});

it('should handle different types', function () {
assert.strictEqual(qs.stringify(), '');
assert.strictEqual(qs.stringify(0), '');
});

it('should URI encode', function () {
assert.strictEqual(qs.stringify({'foo bar': 'baz faz'}), 'foo%20bar=baz%20faz');
});
});

0 comments on commit ddd0bd1

Please sign in to comment.