pax_global_header00006660000000000000000000000064137165445040014523gustar00rootroot0000000000000052 comment=73db95a504f988dce3f790e174e298ceb2b46a8e querystringify-2.2.0/000077500000000000000000000000001371654450400146305ustar00rootroot00000000000000querystringify-2.2.0/.gitignore000066400000000000000000000000451371654450400166170ustar00rootroot00000000000000node_modules/ .nyc_output/ coverage/ querystringify-2.2.0/.npmignore000066400000000000000000000000251371654450400166240ustar00rootroot00000000000000coverage/ test.js .* querystringify-2.2.0/.npmrc000066400000000000000000000000231371654450400157430ustar00rootroot00000000000000package-lock=false querystringify-2.2.0/.travis.yml000066400000000000000000000004061371654450400167410ustar00rootroot00000000000000language: node_js node_js: - "10" - "12" - "14" after_success: - nyc report --reporter=text-lcov | coveralls matrix: fast_finish: true notifications: irc: channels: - "irc.freenode.org#unshift" on_success: change on_failure: change querystringify-2.2.0/LICENSE000066400000000000000000000021331371654450400156340ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2015 Unshift.io, Arnout Kazemier, the Contributors. 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. querystringify-2.2.0/README.md000066400000000000000000000045411371654450400161130ustar00rootroot00000000000000# querystringify [![Version npm](http://img.shields.io/npm/v/querystringify.svg?style=flat-square)](https://www.npmjs.com/package/querystringify)[![Build Status](http://img.shields.io/travis/unshiftio/querystringify/master.svg?style=flat-square)](https://travis-ci.org/unshiftio/querystringify)[![Dependencies](https://img.shields.io/david/unshiftio/querystringify.svg?style=flat-square)](https://david-dm.org/unshiftio/querystringify)[![Coverage Status](http://img.shields.io/coveralls/unshiftio/querystringify/master.svg?style=flat-square)](https://coveralls.io/r/unshiftio/querystringify?branch=master) A somewhat JSON compatible interface for query string parsing. This query string parser is dumb, don't expect to much from it as it only wants to parse simple query strings. If you want to parse complex, multi level and deeply nested query strings then you should ask your self. WTF am I doing? ## Installation This module is released in npm as `querystringify`. It's also compatible with `browserify` so it can be used on the server as well as on the client. To install it simply run the following command from your CLI: ``` npm install --save querystringify ``` ## Usage In the following examples we assume that you've already required the library as: ```js 'use strict'; var qs = require('querystringify'); ``` ### qs.parse() The parse method transforms a given query string in to an object. Parameters without values are set to empty strings. It does not care if your query string is prefixed with a `?`, a `#`, or not prefixed. It just extracts the parts between the `=` and `&`: ```js qs.parse('?foo=bar'); // { foo: 'bar' } qs.parse('#foo=bar'); // { foo: 'bar' } qs.parse('foo=bar'); // { foo: 'bar' } qs.parse('foo=bar&bar=foo'); // { foo: 'bar', bar: 'foo' } qs.parse('foo&bar=foo'); // { foo: '', bar: 'foo' } ``` ### qs.stringify() This transforms a given object in to a query string. By default we return the query string without a `?` prefix. If you want to prefix it by default simply supply `true` as second argument. If it should be prefixed by something else simply supply a string with the prefix value as second argument: ```js qs.stringify({ foo: bar }); // foo=bar qs.stringify({ foo: bar }, true); // ?foo=bar qs.stringify({ foo: bar }, '#'); // #foo=bar qs.stringify({ foo: '' }, '&'); // &foo= ``` ## License MIT querystringify-2.2.0/index.js000066400000000000000000000050041371654450400162740ustar00rootroot00000000000000'use strict'; var has = Object.prototype.hasOwnProperty , undef; /** * Decode a URI encoded string. * * @param {String} input The URI encoded string. * @returns {String|Null} The decoded string. * @api private */ function decode(input) { try { return decodeURIComponent(input.replace(/\+/g, ' ')); } catch (e) { return null; } } /** * Attempts to encode a given input. * * @param {String} input The string that needs to be encoded. * @returns {String|Null} The encoded string. * @api private */ function encode(input) { try { return encodeURIComponent(input); } catch (e) { return null; } } /** * Simple query string parser. * * @param {String} query The query string that needs to be parsed. * @returns {Object} * @api public */ function querystring(query) { var parser = /([^=?#&]+)=?([^&]*)/g , result = {} , part; while (part = parser.exec(query)) { var key = decode(part[1]) , value = decode(part[2]); // // Prevent overriding of existing properties. This ensures that build-in // methods like `toString` or __proto__ are not overriden by malicious // querystrings. // // In the case if failed decoding, we want to omit the key/value pairs // from the result. // if (key === null || value === null || key in result) continue; result[key] = value; } return result; } /** * Transform a query string to an object. * * @param {Object} obj Object that should be transformed. * @param {String} prefix Optional prefix. * @returns {String} * @api public */ function querystringify(obj, prefix) { prefix = prefix || ''; var pairs = [] , value , key; // // Optionally prefix with a '?' if needed // if ('string' !== typeof prefix) prefix = '?'; for (key in obj) { if (has.call(obj, key)) { value = obj[key]; // // Edge cases where we actually want to encode the value to an empty // string instead of the stringified value. // if (!value && (value === null || value === undef || isNaN(value))) { value = ''; } key = encode(key); value = encode(value); // // If we failed to encode the strings, we should bail out as we don't // want to add invalid strings to the query. // if (key === null || value === null) continue; pairs.push(key +'='+ value); } } return pairs.length ? prefix + pairs.join('&') : ''; } // // Expose the module. // exports.stringify = querystringify; exports.parse = querystring; querystringify-2.2.0/package.json000066400000000000000000000015571371654450400171260ustar00rootroot00000000000000{ "name": "querystringify", "version": "2.2.0", "description": "Querystringify - Small, simple but powerful query string parser.", "main": "index.js", "scripts": { "test": "nyc --reporter=html --reporter=text mocha test.js", "watch": "mocha --watch test.js" }, "repository": { "type": "git", "url": "https://github.com/unshiftio/querystringify" }, "keywords": [ "query", "string", "query-string", "querystring", "qs", "stringify", "parse", "decode", "encode" ], "author": "Arnout Kazemier", "license": "MIT", "bugs": { "url": "https://github.com/unshiftio/querystringify/issues" }, "homepage": "https://github.com/unshiftio/querystringify", "devDependencies": { "assume": "^2.1.0", "coveralls": "^3.1.0", "mocha": "^8.1.1", "nyc": "^15.1.0", "pre-commit": "^1.2.2" } } querystringify-2.2.0/test.js000066400000000000000000000071131371654450400161470ustar00rootroot00000000000000describe('querystringify', function () { 'use strict'; var querystring = require('querystring') , assume = require('assume') , qs = require('./'); describe('#stringify', function () { var obj = { foo: 'bar', bar: 'foo' }; it('is exposed as method', function () { assume(qs.stringify).is.a('function'); }); it('transforms an object', function () { assume(qs.stringify(obj)).equals('foo=bar&bar=foo'); }); it('can optionally prefix', function () { assume(qs.stringify(obj, true)).equals('?foo=bar&bar=foo'); }); it('can prefix with custom things', function () { assume(qs.stringify(obj, '&')).equals('&foo=bar&bar=foo'); }); it('doesnt prefix empty query strings', function () { assume(qs.stringify({}, true)).equals(''); assume(qs.stringify({})).equals(''); }); it('works with object keys with empty string values', function () { assume(qs.stringify({ foo: '' })).equals('foo='); }); it('works with nulled objects', function () { var obj = Object.create(null); obj.foo='bar'; assume(qs.stringify(obj)).equals('foo=bar'); }); it('transforms `undefined` into nothing', function () { assume(qs.stringify({ foo: undefined })).equals('foo='); }); it('transforms `NaN` into nothing', function () { assume(qs.stringify({ foo: NaN })).equals('foo='); }); it('transforms `null` into nothing', function () { assume(qs.stringify({ foo: null })).equals('foo='); }); }); describe('#parse', function () { it('is exposed as method', function () { assume(qs.parse).is.a('function'); }); it('will parse a querystring to an object', function () { var obj = qs.parse('foo=bar'); assume(obj).is.a('object'); assume(obj.foo).equals('bar'); }); it('will also work if querystring is prefixed with ?', function () { var obj = qs.parse('?foo=bar&shizzle=mynizzle'); assume(obj).is.a('object'); assume(obj.foo).equals('bar'); assume(obj.shizzle).equals('mynizzle'); }); it('will also work if querystring is prefixed with #', function () { var obj = qs.parse('#foo=bar&shizzle=mynizzle'); assume(obj).is.a('object'); assume(obj.foo).equals('bar'); assume(obj.shizzle).equals('mynizzle'); }); it('does not overide prototypes', function () { var obj = qs.parse('?toString&__proto__=lol'); assume(obj).is.a('object'); assume(obj.toString).is.a('function'); assume(obj.__proto__).does.not.equals('lol'); }); it('works with querystring parameters without values', function () { var obj = qs.parse('?foo&bar=&shizzle=mynizzle'); assume(obj).is.a('object'); assume(obj.foo).equals(''); assume(obj.bar).equals(''); assume(obj.shizzle).equals('mynizzle'); }); it('first value wins', function () { var obj = qs.parse('foo=1&foo=2'); assume(obj.foo).equals('1'); }); it('decodes plus signs', function () { var obj = qs.parse('foo+bar=baz+qux'); assume(obj).is.a('object'); assume(obj['foo bar']).equals('baz qux'); obj = qs.parse('foo+bar=baz%2Bqux'); assume(obj).is.a('object'); assume(obj['foo bar']).equals('baz+qux'); }); it('does not throw on invalid input', function () { var obj = qs.parse('?%&'); assume(obj).is.a('object'); }); it('does not include invalid output', function () { var obj = qs.parse('?%&'); assume(obj).is.a('object'); assume(obj).is.length(0); }); }); });