Skip to content

Commit

Permalink
I N I T I A L I Z A T I O N
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanong committed Apr 16, 2015
0 parents commit 8e13b6e
Show file tree
Hide file tree
Showing 7 changed files with 245 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
@@ -0,0 +1,7 @@

.DS_Store*
*.log
*.gz

node_modules
coverage
8 changes: 8 additions & 0 deletions .travis.yml
@@ -0,0 +1,8 @@
node_js:
- "0.10"
- "0.12"
- "iojs"
language: node_js
sudo: false
script: "npm run test-ci"
after_script: "npm install coveralls@2 && cat ./coverage/lcov.info | coveralls"
22 changes: 22 additions & 0 deletions LICENSE
@@ -0,0 +1,22 @@

The MIT License (MIT)

Copyright (c) 2015 Jonathan Ong me@jongleberry.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
38 changes: 38 additions & 0 deletions README.md
@@ -0,0 +1,38 @@

# qs-strict

[![NPM version][npm-image]][npm-url]
[![Build status][travis-image]][travis-url]
[![Test coverage][coveralls-image]][coveralls-url]
[![Dependency Status][david-image]][david-url]
[![License][license-image]][license-url]
[![Downloads][downloads-image]][downloads-url]

A querystring parser that strictly uses strings.
It never returns `Array`s or `Object`s,
so you never have to check its type.

## API

### var qs = require('qs-strict')

A drop-in implementation for `require('querystring')`.

### qs.stringify()

### qs.parse()

[npm-image]: https://img.shields.io/npm/v/qs-strict.svg?style=flat-square
[npm-url]: https://npmjs.org/package/qs-strict
[github-tag]: http://img.shields.io/github/tag/pillarjs/qs-strict.svg?style=flat-square
[github-url]: https://github.com/pillarjs/qs-strict/tags
[travis-image]: https://img.shields.io/travis/pillarjs/qs-strict.svg?style=flat-square
[travis-url]: https://travis-ci.org/pillarjs/qs-strict
[coveralls-image]: https://img.shields.io/coveralls/pillarjs/qs-strict.svg?style=flat-square
[coveralls-url]: https://coveralls.io/r/pillarjs/qs-strict
[david-image]: http://img.shields.io/david/pillarjs/qs-strict.svg?style=flat-square
[david-url]: https://david-dm.org/pillarjs/qs-strict
[license-image]: http://img.shields.io/npm/l/qs-strict.svg?style=flat-square
[license-url]: LICENSE
[downloads-image]: http://img.shields.io/npm/dm/qs-strict.svg?style=flat-square
[downloads-url]: https://npmjs.org/package/qs-strict
119 changes: 119 additions & 0 deletions index.js
@@ -0,0 +1,119 @@
'use strict';

var QueryString = exports;

// use the native querystring implementation for encoding and decoding
var qs = require('querystring');
var methods = [
'unescapeBuffer',
'unescape',
'escape',
];

methods.forEach(function (method) {
QueryString[method] = qs[method];
});

/**
* Everything below this comment is basically copied from io.js
* As of version: https://github.com/iojs/io.js/blob/10e31ba56c676bdcad39ccad22ea9117733b8eb5/lib/querystring.js
*/

var stringifyPrimitive = function(v) {
if (typeof v === 'string')
return v;
if (typeof v === 'number' && isFinite(v))
return '' + v;
if (typeof v === 'boolean')
return v ? 'true' : 'false';
return '';
};

QueryString.stringify = QueryString.encode = function(obj, sep, eq, options) {
sep = sep || '&';
eq = eq || '=';

var encode = QueryString.escape;
if (options && typeof options.encodeURIComponent === 'function') {
encode = options.encodeURIComponent;
}

if (obj !== null && typeof obj === 'object') {
var keys = Object.keys(obj);
var len = keys.length;
var flast = len - 1;
var fields = '';
for (var i = 0; i < len; ++i) {
var k = keys[i];
var v = obj[k];
var ks = encode(stringifyPrimitive(k)) + eq;

if (Array.isArray(v))
v = v.join(',');

fields += ks + encode(stringifyPrimitive(v));
if (i < flast)
fields += sep;
}
return fields;
}
return '';
};

// Parse a key=val string.
QueryString.parse = QueryString.decode = function(qs, sep, eq, options) {
sep = sep || '&';
eq = eq || '=';
var obj = {};

if (typeof qs !== 'string' || qs.length === 0) {
return obj;
}

var regexp = /\+/g;
qs = qs.split(sep);

var maxKeys = 1000;
if (options && typeof options.maxKeys === 'number') {
maxKeys = options.maxKeys;
}

var len = qs.length;
// maxKeys <= 0 means that we should not limit keys count
if (maxKeys > 0 && len > maxKeys) {
len = maxKeys;
}

var decode = QueryString.unescape;
if (options && typeof options.decodeURIComponent === 'function') {
decode = options.decodeURIComponent;
}

var keys = [];
for (var i = 0; i < len; ++i) {
var x = qs[i].replace(regexp, '%20'),
idx = x.indexOf(eq),
k, v;

if (idx >= 0) {
k = decodeStr(x.substring(0, idx), decode);
v = decodeStr(x.substring(idx + 1), decode);
} else {
k = decodeStr(x, decode);
v = '';
}

obj[k] = v;
}

return obj;
};


function decodeStr(s, decoder) {
try {
return decoder(s);
} catch (e) {
return QueryString.unescape(s, true);
}
}
25 changes: 25 additions & 0 deletions package.json
@@ -0,0 +1,25 @@
{
"name": "qs-strict",
"description": "QueryString utility that always returns strings",
"version": "0.0.0",
"author": "Jonathan Ong <me@jongleberry.com> (http://jongleberry.com)",
"license": "MIT",
"repository": "pillarjs/qs-strict",
"devDependencies": {
"istanbul": "0",
"mocha": "2"
},
"scripts": {
"test": "mocha",
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot",
"test-ci": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter dot"
},
"keywords": [
"qs",
"querystring",
"strict'"
],
"files": [
"index.js"
]
}
26 changes: 26 additions & 0 deletions test/test.js
@@ -0,0 +1,26 @@
'use strict';

var assert = require('assert');

var qs = require('..');

describe('.stringify()', function () {
it('should .join(";") arrays', function () {
var string = qs.stringify({
value: [1, 2]
});

assert.equal(string, 'value=' + qs.escape('1,2'));
})
})

describe('.parse()', function () {
describe('when there are duplicate keys', function () {
it('should only use the last value', function () {
var obj = qs.parse('key=1&key=2&key=3');
assert.deepEqual(obj, {
key: '3'
});
})
})
})

0 comments on commit 8e13b6e

Please sign in to comment.