Skip to content

Instantly share code, notes, and snippets.

@prantlf
Last active September 2, 2018 00:12
Show Gist options
  • Select an option

  • Save prantlf/061e3911cd450491f84aac40292b7e7c to your computer and use it in GitHub Desktop.

Select an option

Save prantlf/061e3911cd450491f84aac40292b7e7c to your computer and use it in GitHub Desktop.
Reverses the $.param effect - parses the URL query part into an object
// jQuery.parseParams 0.1.0
// https://gist.github.com/prantlf/061e3911cd450491f84aac40292b7e7c
//
// Copyright (c) 2015-2017 Ferdinand Prantl
// Licensed under the MIT license.
//
// Reverses the $.param effect - parses the URL query part into an object
// with parameter names as keys pointing to parameter values; normalizes
// all parameter names to lower-case and saves multiple parameters with
// the same name to arrays
(function ($) {
'use strict';
$.parseParams = function (query) {
var result = {};
query || (query = '');
// Make passing of location.search easier, which includes the leading "?"
if (query.charAt(0) === '?') {
query = query.substr(1);
}
// $.param additionally replaces "%20" by "+" after encoding a value
query = query.replace(/\+/g, ' ');
$.each(query.split('&'), function (index, parameter) {
var keyAndValue = parameter.split('='),
name = decodeURIComponent(keyAndValue[0]).toLowerCase(),
value = keyAndValue.length > 1 &&
decodeURIComponent(keyAndValue[1]) || '',
values = result[name];
if (name) {
if (values !== undefined) {
if (Array.isArray(values)) {
values.push(value);
} else {
result[name] = [values, value];
}
} else {
result[name] = value;
}
}
});
return result;
};
}(jQuery));
// jQuery.parseParams 0.1.0
// https://gist.github.com/prantlf/061e3911cd450491f84aac40292b7e7c
//
// Copyright (c) 2015-2017 Ferdinand Prantl
// Licensed under the MIT license.
(function ($) {
'use strict';
describe('jQuery.parseParams', function () {
describe('returns empty object', function () {
it('for undefined', function () {
expect($.parseParams(undefined)).to.deep.equal({});
});
it('for null', function () {
expect($.parseParams(null)).to.deep.equal({});
});
it('for empty string', function () {
expect($.parseParams('')).to.deep.equal({});
});
it('for single question mark', function () {
expect($.parseParams('?')).to.deep.equal({});
});
});
describe('parses single parameter', function () {
it('without value', function () {
expect($.parseParams('a=')).to.deep.equal({a: ''});
});
it('with value', function () {
expect($.parseParams('a=1')).to.deep.equal({a: '1'});
});
});
describe('parses two parameters with different names', function () {
it('without values', function () {
expect($.parseParams('a=&b=')).to.deep.equal({a: '', b: ''});
});
it('with values', function () {
expect($.parseParams('a=1&b=2')).to.deep.equal({a: '1', b: '2'});
});
});
describe('parses two parameters with the same name', function () {
it('without values', function () {
expect($.parseParams('a=&a=')).to.deep.equal({a: ['', '']});
});
it('with values', function () {
expect($.parseParams('a=1&a=2')).to.deep.equal({a: ['1', '2']});
});
});
describe('supports URL encoding', function () {
it('in parameter name', function () {
expect($.parseParams('a%2F=1')).to.deep.equal({'a/': '1'});
});
it('in parameter value', function () {
expect($.parseParams('a=%2F')).to.deep.equal({a: '/'});
});
it('with space as "%20"', function () {
expect($.parseParams('a=%20')).to.deep.equal({a: ' '});
});
it('with space as "+"', function () {
expect($.parseParams('a=+')).to.deep.equal({a: ' '});
});
});
it('skips parameters without name', function () {
expect($.parseParams('=1')).to.deep.equal({});
});
it('ignores leading question mark', function () {
expect($.parseParams('?a=1')).to.deep.equal({a: '1'});
});
});
}(jQuery));
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery.parseParams</title>
</head>
<body>
<div id="mocha"></div>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/mocha/3.5.0/mocha.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/mocha/3.5.0/mocha.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chai/4.1.1/chai.min.js"></script>
<script>
mocha.setup('bdd');
mocha.traceIgnores = ['mocha.min.js', 'chai.min.js'];
expect = chai.expect;
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="jquery.parseParams.js"></script>
<script src="jquery.parseParams.spec.js"></script>
<script>
mocha.run();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment