Ticket #7421: forinloop.diff
| File forinloop.diff, 2.6 kB (added by Tobie, 2 years ago) |
|---|
-
test/unit/hash.html
old new 104 104 assertEqual("color=", $H(Fixtures.multiple_all_nil).toQueryString()) 105 105 assertEqual("color=", $H(Fixtures.multiple_empty).toQueryString()) 106 106 107 assertEqual("_each=E&map=M&keys=K&values=V&collect=C&inject=I", Hash.toQueryString(Fixtures.dangerous)) 107 assertEnumEqual($w("_each=E map=M keys=K values=V collect=C inject=I").sort(), 108 Hash.toQueryString(Fixtures.dangerous).split('&').sort()); 109 assertEnumEqual($w('_each=E map=M keys=K values=V collect=C inject=I').sort(), 110 $H(Fixtures.dangerous).toQueryString().split('&').sort()); 108 111 }}, 109 112 110 113 testInspect: function(){ with(this) { -
src/hash.js
old new 6 6 toQueryString: function(obj) { 7 7 var parts = []; 8 8 9 this.prototype._each.call(obj, function(pair) {9 this.prototype._each.call(obj, function(pair) { 10 10 if (!pair.key) return; 11 11 12 12 if (pair.value && pair.value.constructor == Array) { 13 13 var values = pair.value.compact(); 14 14 if (values.length < 2) pair.value = values.reduce(); 15 15 else { 16 key = encodeURIComponent(pair.key);16 key = encodeURIComponent(pair.key); 17 17 values.each(function(value) { 18 value = value != undefined ? encodeURIComponent(value): '';18 value = value != undefined ? value : ''; 19 19 parts.push(key + '=' + encodeURIComponent(value)); 20 20 }); 21 21 return; … … 23 23 } 24 24 if (pair.value == undefined) pair[1] = ''; 25 25 parts.push(pair.map(encodeURIComponent).join('=')); 26 });26 }); 27 27 28 28 return parts.join('&'); 29 29 } … … 89 89 if (object && object.constructor == Hash) return object; 90 90 return new Hash(object); 91 91 }; 92 93 94 if(function() { 95 var TestObject = function(value) { 96 this.key = value; 97 }; 98 TestObject.prototype.key = 'foo'; 99 var instance = new TestObject('bar'); 100 var i = 0; 101 for (var property in instance) i++; 102 return (i != 1); 103 }()) Hash.prototype._each = function(iterator) { 104 var cache = []; 105 for (var key in this) { 106 var value = this[key]; 107 if ((value && value == Hash.prototype[key]) || cache.include(key)) continue; 108 cache.push(key); 109 var pair = [key, value]; 110 pair.key = key; 111 pair.value = value; 112 iterator(pair); 113 } 114 };