JavaScript解析URL引數

pengfoo發表於2014-10-14
/*!
	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.replace(/\+/g, ' ').split('=');
			var key = parts[0];
			var val = parts[1];

			key = decodeURIComponent(key);
			// missing `=` should be `null`:
			// http://w3.org/TR/2012/WD-url-20120524/#collect-url-parameters
			val = val === undefined ? null : decodeURIComponent(val);

			if (!ret.hasOwnProperty(key)) {
				ret[key] = val;
			} else if (Array.isArray(ret[key])) {
				ret[key].push(val);
			} else {
				ret[key] = [ret[key], val];
			}

			return ret;
		}, {});
	};

	queryString.stringify = function (obj) {
		return obj ? Object.keys(obj).map(function (key) {
			var val = obj[key];

			if (Array.isArray(val)) {
				return val.map(function (val2) {
					return encodeURIComponent(key) + '=' + encodeURIComponent(val2);
				}).join('&');
			}

			return encodeURIComponent(key) + '=' + encodeURIComponent(val);
		}).join('&') : '';
	};

	if (typeof define === 'function' && define.amd) {
		define(function() { return queryString; });
	} else if (typeof module !== 'undefined' && module.exports) {
		module.exports = queryString;
	} else {
		window.queryString = queryString;
	}
})();
'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 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('#'), {});
		assert.deepEqual(qs.parse(' '), {});
	});

	it('should handle `+` correctly', function () {
		assert.deepEqual(qs.parse('foo+faz=bar+baz++'), {'foo faz': 'bar baz  '});
	});

	it('should handle multiple of the same key', function () {
		assert.deepEqual(qs.parse('foo=bar&foo=baz'), {foo: ['bar', 'baz']});
	});
});

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');
	});

	it('should handle array value', function () {
		assert.strictEqual(qs.stringify({abc: 'abc', foo: ['bar', 'baz']}), 'abc=abc&foo=bar&foo=baz');
	});
});


相關文章