From 0723fd83cca78bd5709d08f6c9aee13f65a40747 Mon Sep 17 00:00:00 2001 From: Julien ROUSSEAU Date: Wed, 26 Apr 2017 00:47:25 +0200 Subject: [PATCH] Allow multiple key query parameters with same key, fix #79 --- iron-query-params.html | 31 ++++++++++++++++++++----------- test/integration.html | 5 +++-- test/iron-query-params.html | 18 +++++++++++------- 3 files changed, 34 insertions(+), 20 deletions(-) diff --git a/iron-query-params.html b/iron-query-params.html index edcc3e1..4a723c9 100644 --- a/iron-query-params.html +++ b/iron-query-params.html @@ -55,15 +55,21 @@ _encodeParams: function(params) { var encodedParams = []; for (var key in params) { - var value = params[key]; - if (value === '') { - encodedParams.push(encodeURIComponent(key)); - } else if (value) { - encodedParams.push( - encodeURIComponent(key) + - '=' + - encodeURIComponent(value.toString()) - ); + var values = params[key]; + + if (values) { + values.forEach(value => { + if (value === '') { + encodedParams.push(encodeURIComponent(key)); + } else if (value.length > 0) { + + encodedParams.push( + encodeURIComponent(key) + + '=' + + encodeURIComponent(value.toString()) + ); + } + }); } } return encodedParams.join('&'); @@ -79,8 +85,11 @@ for (var i = 0; i < paramList.length; i++) { var param = paramList[i].split('='); if (param[0]) { - params[decodeURIComponent(param[0])] = - decodeURIComponent(param[1] || ''); + var param0 = decodeURIComponent(param[0]); + if (!params[param0]) { + params[param0] = []; + } + params[param0].push(decodeURIComponent(param[1] || '')); } } return params; diff --git a/test/integration.html b/test/integration.html index 9518d17..8dce1a7 100644 --- a/test/integration.html +++ b/test/integration.html @@ -92,7 +92,8 @@ expect(ironLocationQuery).to.contain(ironLocation.query); expect(ironLocationQuery).to.contain(ironQueryParams.paramsString); if (plainTextQuery) { - expect('').to.be.equal(ironQueryParams.paramsObject[plainTextQuery]) + expect(1).to.be.equal(ironQueryParams.paramsObject[plainTextQuery].length) + expect('').to.be.equal(ironQueryParams.paramsObject[plainTextQuery][0]) } else { expect(ironQueryParams.paramsObject[plainTextQuery]).to.be.undefined; } @@ -122,7 +123,7 @@ } var newParamsObject = {}; - newParamsObject[plainTextQuery] = ''; + newParamsObject[plainTextQuery] = ['']; ironQueryParams.paramsObject = newParamsObject; expect(encodedQueries).to.contain(window.location.search); diff --git a/test/iron-query-params.html b/test/iron-query-params.html index 8b27642..1992d6b 100644 --- a/test/iron-query-params.html +++ b/test/iron-query-params.html @@ -45,10 +45,10 @@ // Check the mapping from paramsString to paramsObject. paramsElem.paramsString = 'greeting=hello&target=world'; expect(paramsElem.paramsObject).to.deep.equal( - {greeting: 'hello', target: 'world'}); + {greeting: ['hello'], target: ['world']}); // Check the mapping from paramsObject back to paramsString. - paramsElem.set('paramsObject.another', 'key'); + paramsElem.set('paramsObject.another', ['key']); expect(paramsElem.paramsString).to.be.equal( 'greeting=hello&target=world&another=key'); }); @@ -56,19 +56,23 @@ var mappings = [ { string: 'a=b', - object: {'a': 'b'} + object: {'a': ['b']} + }, + { + string: 'a=b&a=c', + object: {'a': ['b','c']} }, { string: 'debug&ok', - object: {'debug': '', 'ok': ''} + object: {'debug': [''], 'ok': ['']} }, { string: 'monster%20kid%3A=%F0%9F%98%BF"es=%27%27', - object: {'monster kid:': '😿', 'quotes': '\'\''} + object: {'monster kid:': ['😿'], 'quotes': ['\'\'']} }, { string: 'yes%2C%20ok?%20what%20is%20up%20with%20%CB%9Athiiis%CB%9A=%E2%98%83', - object: {'yes, ok? what is up with ˚thiiis˚': '☃'} + object: {'yes, ok? what is up with ˚thiiis˚': ['☃']} }, ]; @@ -94,7 +98,7 @@ // Check the mapping for querystrings with + instead of %20. paramsElem.paramsString = 'key=value+with+spaces'; expect(paramsElem.paramsObject).to.deep.equal( - {key: 'value with spaces'}); + {key: ['value with spaces']}); }); });