From 7e62e3e0130ab0b7a384dd3a3439556d4342b3df Mon Sep 17 00:00:00 2001 From: Valerio Proietti Date: Thu, 11 Jun 2009 03:54:05 +0200 Subject: [PATCH] Revert "New toQueryString implementation with less recursive functions" This reverts commit 9931ac97f1ce6a3fcdb3d90a5db76a25d2a4e2e8. --- Source/Native/Hash.js | 39 ++++++++++++++++++++------------------- Specs/Native/Hash.js | 14 -------------- 2 files changed, 20 insertions(+), 33 deletions(-) diff --git a/Source/Native/Hash.js b/Source/Native/Hash.js index 60054405..375fac63 100644 --- a/Source/Native/Hash.js +++ b/Source/Native/Hash.js @@ -107,25 +107,26 @@ Hash.implement({ return values; }, - toQueryString: function(format){ - if($type(format) == 'string') format = { base: format }; - format = $extend({ property: '[#]', array: '[#]', separator: '&' }, format); - - var queryString = [], base = format.base; - var pushItem = function(value, key){ - format.base = base.replace(/\#/, key); - queryString.push(Hash.toQueryString(value, format)); - }; - if ($type(this).test(/object|hash/)){ - base = base ? base + format.property : '#'; - Hash.each(this, pushItem); - } else if($type(this) == 'array'){ - base = base + format.array; - this.each(pushItem); - } else { - return base + '=' + encodeURIComponent(this); - } - return queryString.join(format.separator); + toQueryString: function(base){ + var queryString = []; + Hash.each(this, function(value, key){ + if (base) key = base + '[' + key + ']'; + var result; + switch ($type(value)){ + case 'object': result = Hash.toQueryString(value, key); break; + case 'array': + var qs = {}; + value.each(function(val, i){ + qs[i] = val; + }); + result = Hash.toQueryString(qs, key); + break; + default: result = key + '=' + encodeURIComponent(value); + } + if (value != undefined) queryString.push(result); + }); + + return queryString.join('&'); } }); diff --git a/Specs/Native/Hash.js b/Specs/Native/Hash.js index 0a8f8434..ff75a33b 100644 --- a/Specs/Native/Hash.js +++ b/Specs/Native/Hash.js @@ -196,20 +196,6 @@ describe("Hash Methods", { var myHash3 = new Hash({fruits: {apple: ['red', 'yellow'], lemon: ['green', 'yellow']}}); value_of(myHash3.toQueryString()).should_be('fruits[apple][0]=red&fruits[apple][1]=yellow&fruits[lemon][0]=green&fruits[lemon][1]=yellow'); - - value_of(myHash3.toQueryString({ property: '.#', array: '' })).should_be('fruits.apple=red&fruits.apple=yellow&fruits.lemon=green&fruits.lemon=yellow'); - - -var obj = { - first: ['A', 'B'], - second: { a: 1, b: 2 }, - third: [{a: 1, b: 2}, {c:3}] -}; - -value_of(Hash.toQueryString(obj)).should_be('first[0]=A&first[1]=B&second[a]=1&second[b]=2&third[0][a]=1&third[0][b]=2&third[1][c]=3'); -value_of(Hash.toQueryString(obj, { property: '.#', array: '[]' })).should_be('first[]=A&first[]=B&second.a=1&second.b=2&third[].a=1&third[].b=2&third[].c=3'); -value_of(Hash.toQueryString(obj, { property: '.#', array: '' })).should_be('first=A&first=B&second.a=1&second.b=2&third.a=1&third.b=2&third.c=3'); - } }); -- 2.11.4.GIT