[Pkg-javascript-commits] [node-querystringify] 01/03: Imported Upstream version 0.0.3
Thorsten Alteholz
alteholz at moszumanska.debian.org
Tue Feb 2 21:07:04 UTC 2016
This is an automated email from the git hooks/post-receive script.
alteholz pushed a commit to branch master
in repository node-querystringify.
commit 93552a925f2bccbcb06085a9cfadd51a398bd439
Author: Thorsten Alteholz <debian at alteholz.de>
Date: Tue Feb 2 22:06:44 2016 +0100
Imported Upstream version 0.0.3
---
.gitignore | 3 +++
.travis.yml | 28 +++++++++++++++++++++++++++
LICENSE | 22 +++++++++++++++++++++
README.md | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
index.js | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
package.json | 39 ++++++++++++++++++++++++++++++++++++++
test.js | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
7 files changed, 272 insertions(+)
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..648ea07
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+node_modules
+coverage
+npm-debug.log
diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 0000000..22ebb02
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,28 @@
+language: node_js
+node_js:
+ - "0.12"
+ - "0.11"
+ - "0.10"
+ - "0.9"
+ - "0.8"
+ - "iojs-v1.1"
+ - "iojs-v1.0"
+before_install:
+ - "npm install -g npm at 1.4.x"
+script:
+ - "npm run test-travis"
+after_script:
+ - "npm install coveralls at 2.11.x && cat coverage/lcov.info | coveralls"
+matrix:
+ fast_finish: true
+ allow_failures:
+ - node_js: "0.11"
+ - node_js: "0.9"
+ - node_js: "iojs-v1.1"
+ - node_js: "iojs-v1.0"
+notifications:
+ irc:
+ channels:
+ - "irc.freenode.org#unshift"
+ on_success: change
+ on_failure: change
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..6dc9316
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,22 @@
+The 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.
+
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..a3c6471
--- /dev/null
+++ b/README.md
@@ -0,0 +1,57 @@
+# querystringify
+
+[![Made by unshift](https://img.shields.io/badge/made%20by-unshift-00ffcc.svg?style=flat-square)](http://unshift.io)[![Version npm](http://img.shields.io/npm/v/querystringify.svg?style=flat-square)](http://browsenpm.org/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 [...]
+
+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. It does not
+care if your query string if prefixed with a `?` or not. 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&bar=foo'); // { foo: 'bar', 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
+```
+
+## License
+
+MIT
diff --git a/index.js b/index.js
new file mode 100644
index 0000000..1610c2f
--- /dev/null
+++ b/index.js
@@ -0,0 +1,61 @@
+'use strict';
+
+var has = Object.prototype.hasOwnProperty;
+
+/**
+ * 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;
+
+ //
+ // Little nifty parsing hack, leverage the fact that RegExp.exec increments
+ // the lastIndex property so we can continue executing this loop until we've
+ // parsed all results.
+ //
+ for (;
+ part = parser.exec(query);
+ result[decodeURIComponent(part[1])] = decodeURIComponent(part[2])
+ );
+
+ 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 = [];
+
+ //
+ // Optionally prefix with a '?' if needed
+ //
+ if ('string' !== typeof prefix) prefix = '?';
+
+ for (var key in obj) {
+ if (has.call(obj, key)) {
+ pairs.push(encodeURIComponent(key) +'='+ encodeURIComponent(obj[key]));
+ }
+ }
+
+ return pairs.length ? prefix + pairs.join('&') : '';
+}
+
+//
+// Expose the module.
+//
+exports.stringify = querystringify;
+exports.parse = querystring;
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..fb91ebf
--- /dev/null
+++ b/package.json
@@ -0,0 +1,39 @@
+{
+ "name": "querystringify",
+ "version": "0.0.3",
+ "description": "Querystringify - Small, simple but powerful query string parser.",
+ "main": "index.js",
+ "scripts": {
+ "test": "mocha --reporter spec --ui bdd test.js",
+ "watch": "mocha --watch --reporter spec --ui bdd test.js",
+ "coverage": "istanbul cover ./node_modules/.bin/_mocha -- --reporter spec --ui bdd test.js",
+ "test-travis": "istanbul cover node_modules/.bin/_mocha --report lcovonly -- --reporter spec --ui bdd 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": "1.2.x",
+ "istanbul": "0.3.x",
+ "mocha": "2.2.x",
+ "pre-commit": "1.0.x"
+ }
+}
diff --git a/test.js b/test.js
new file mode 100644
index 0000000..3778168
--- /dev/null
+++ b/test.js
@@ -0,0 +1,62 @@
+describe('querystringify', function () {
+ 'use strict';
+
+ var 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 nulled objects', function () {
+ var obj = Object.create(null);
+
+ obj.foo='bar';
+ assume(qs.stringify(obj)).equals('foo=bar');
+ });
+ });
+
+ 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');
+ });
+ });
+});
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-querystringify.git
More information about the Pkg-javascript-commits
mailing list