Skip to content
This repository

New methods for Hash. #71

Open
wants to merge 4 commits into from

2 participants

mike jslick
mike

I add some methods for Hash object. I think they will be useful.

 Hash#eachKey 
 Hash#eachValue
 Hash#eachPair
jslick

Looks like you removed a good line and added a whitespace error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
2  src/prototype/ajax/request.js
@@ -193,7 +193,7 @@ Ajax.Request = Class.create(Ajax.Base, {
193 193 // when GET, append parameters to URL
194 194 this.url += (this.url.include('?') ? '&' : '?') + params;
195 195 }
196   -
  196 +
197 197 this.parameters = params.toQueryParams();
198 198
199 199 try {
116 src/prototype/lang/hash.js
@@ -105,6 +105,67 @@ var Hash = Class.create(Enumerable, (function() {
105 105 }
106 106
107 107 /**
  108 + * Hash#eachKey(iterator[, context]) -> Hash
  109 + *
  110 + * Iterates over the keys in the hash.
  111 + *
  112 + * ##### Example
  113 + *
  114 + * var hash = $H({England: 'London', Poland: 'Warsaw'});
  115 + *
  116 + * h.eachKey(function(country) {
  117 + * alert(country);
  118 + * });
  119 + * // Alerts: England
  120 + * // Alerts: Poland
  121 + *
  122 + **/
  123 + function eachKey(iterator, context) {
  124 + this.keys().each(iterator, context);
  125 + }
  126 +
  127 + /**
  128 + * Hash#eachValue(iterator[, context]) -> Hash
  129 + *
  130 + * Iterates over the values in the hash.
  131 + *
  132 + * ##### Example
  133 + *
  134 + * var hash = $H({England: 'London', Poland: 'Warsaw'});
  135 + *
  136 + * h.eachValue(function(capital) {
  137 + * alert(capital);
  138 + * });
  139 + * // Alerts: London
  140 + * // Alerts: Warsaw
  141 + **/
  142 + function eachValue(iterator, context) {
  143 + this.values().each(iterator, context);
  144 + }
  145 +
  146 + /**
  147 + * Hash#eachPair(iterator[, context]) -> Hash
  148 + *
  149 + * Iterates over the key/value pairs in the hash.
  150 + *
  151 + * ##### Example
  152 + *
  153 + * var hash = $H({England: 'London', Poland: 'Warsaw'});
  154 + *
  155 + * h.eachPair(function(country, capital) {
  156 + * alert(capital + "is the capital of " + country);
  157 + * });
  158 + * //Alerts: London is the capital of England
  159 + * //Alerts: Warsaw is the capital of Poland
  160 + *
  161 + **/
  162 + function eachPair(iterator, context) {
  163 + this.each(function(pair) {
  164 + iterator.call(context, pair.key, pair.value)
  165 + });
  166 + }
  167 +
  168 + /**
108 169 * Hash#set(key, value) -> value
109 170 * - key (String): The key to use for this value.
110 171 * - value (?): The value to use for this key.
@@ -262,6 +323,27 @@ var Hash = Class.create(Enumerable, (function() {
262 323 }
263 324
264 325 /**
  326 + * Hash#mergeWith(object, func) -> Hash
  327 + * - object (Object | Hash): The object to merge with this hash to produce
  328 + * the resulting hash.
  329 + * - func (Function): The function that will be applied to the values of the
  330 + * same keys. Takes two arguments.
  331 + *
  332 + * ##### Example
  333 + *
  334 + * var hash1 = $H({a: 1, b: 2, c: 2});
  335 + * var func = function(v1, v2) { return v1 + v2; };
  336 + *
  337 + * var hash2 = hash.mergeWith({a: 3, b: 4, c: 5, d: 9}, func);
  338 + * // -> {a: 4, b: 6, c: 7, d: 9}
  339 + *
  340 + *
  341 + **/
  342 + function mergeWith(object, func) {
  343 + return this.clone().updateWith(object, func);
  344 + }
  345 +
  346 + /**
265 347 * Hash#update(object) -> Hash
266 348 * - object (Object | Hash): The object to merge with this hash to produce
267 349 * the resulting hash.
@@ -287,6 +369,35 @@ var Hash = Class.create(Enumerable, (function() {
287 369 });
288 370 }
289 371
  372 + /**
  373 + * Hash#updateWith(object, func) -> Hash
  374 + * - object (Object | Hash): The object to merge with this hash to produce
  375 + * the resulting hash.
  376 + * - func (Function): The function that will be applied to the values of the
  377 + * same keys. Takes two arguments.
  378 + *
  379 + * ##### Example
  380 + *
  381 + * var hash = $H({a: 1, b: 2, c: 2});
  382 + * var func = function(v1, v2) { return v1 + v2; };
  383 + * hash.updateWith({a: 3, b: 4, c: 5, d: 9}, func);
  384 + * // -> {a: 4, b: 6, c: 7, d: 9}
  385 + *
  386 + *
  387 + **/
  388 + function updateWith(object, func) {
  389 + if (!Object.isFunction(func))
  390 + throw new TypeError();
  391 +
  392 + var keys = this.keys();
  393 + var newHash = new Hash(object).inject(this, function(result, pair) {
  394 + var value = keys.include(pair.key)? func(this.get(pair.key), pair.value) : pair.value;
  395 + result.set(pair.key, value);
  396 + return result;
  397 + }.bind(this));
  398 + return newHash;
  399 + }
  400 +
290 401 // Private. No PDoc necessary.
291 402 function toQueryPair(key, value) {
292 403 if (Object.isUndefined(value)) return key;
@@ -388,6 +499,9 @@ var Hash = Class.create(Enumerable, (function() {
388 499 return {
389 500 initialize: initialize,
390 501 _each: _each,
  502 + eachKey: eachKey,
  503 + eachValue: eachValue,
  504 + eachPair: eachPair,
391 505 set: set,
392 506 get: get,
393 507 unset: unset,
@@ -397,7 +511,9 @@ var Hash = Class.create(Enumerable, (function() {
397 511 values: values,
398 512 index: index,
399 513 merge: merge,
  514 + mergeWith: mergeWith,
400 515 update: update,
  516 + updateWith: updateWith,
401 517 toQueryString: toQueryString,
402 518 inspect: inspect,
403 519 toJSON: toObject,
50 test/unit/hash_test.js
@@ -4,7 +4,6 @@ new Test.Unit.Runner({
4 4
5 5 this.assertEqual('B', h.set('b', 'B'));
6 6 this.assertHashEqual({a: 'A', b: 'B'}, h);
7   -
8 7 this.assertUndefined(h.set('c'));
9 8 this.assertHashEqual({a: 'A', b: 'B', c: undefined}, h);
10 9 },
@@ -102,6 +101,16 @@ new Test.Unit.Runner({
102 101 this.assertHashEqual({a:'A#', b:'B', c:'C', d:'D#' }, h.merge(Fixtures.one));
103 102 },
104 103
  104 + testMergeWith: function() {
  105 + var h1 = $H({a: 1, b: 2, c: 2});
  106 + var func = function(v1, v2) { return v1 + v2;};
  107 + var h2 = h1.mergeWith({a: 3, b: 4, c: 5, d: 9}, func);
  108 + this.assertHashEqual($H({'a': 4, 'b': 6, 'c': 7, 'd': 9}), h2);
  109 + //this.assertHashEqual($H({'a': 1, 'b': 2, 'c': 2}), h1);
  110 + /*TypeError when function not defined*/
  111 + this.assertRaise('TypeError', function(){h1.mergeWith({});});
  112 + },
  113 +
105 114 testUpdate: function() {
106 115 var h = $H(Fixtures.many);
107 116 this.assertIdentical(h, h.update());
@@ -112,6 +121,15 @@ new Test.Unit.Runner({
112 121 this.assertHashEqual({a:'A', b:'B', c:'C', d:'D#', aaa:'AAA' }, h.update({aaa: 'AAA'}));
113 122 this.assertHashEqual({a:'A#', b:'B', c:'C', d:'D#', aaa:'AAA' }, h.update(Fixtures.one));
114 123 },
  124 +
  125 + testUpdateWith: function() {
  126 + var h = $H({a: 1, b: 2, c: 2});
  127 + var func = function(v1, v2) { return v1 + v2;};
  128 + h.updateWith({a: 3, b: 4, c: 5, d: 9}, func);
  129 + this.assertHashEqual($H({'a': 4, 'b': 6, 'c': 7, 'd': 9}), h);
  130 + /*TypeError when function not defined*/
  131 + this.assertRaise('TypeError', function(){h.updateWith({});});
  132 + },
115 133
116 134 testToQueryString: function() {
117 135 this.assertEqual('', $H({}).toQueryString());
@@ -191,6 +209,36 @@ new Test.Unit.Runner({
191 209 result.push(i);
192 210 });
193 211 this.assertEnumEqual([0,1], result);
  212 + },
  213 +
  214 + testIterationWithEachKey: function() {
  215 + var hash = $H({a:1, b:2, c:3});
  216 + var keys = [];
  217 + hash.eachKey(function(key) {
  218 + keys.push(key);
  219 + });
  220 + this.assertEnumEqual(['a', 'b', 'c'], keys);
  221 + },
  222 +
  223 + testIterationWithEachValue: function() {
  224 + var hash = $H({a:1, b:2, c:3});
  225 + var values = [];
  226 + hash.eachValue(function(value) {
  227 + values.push(value);
  228 + });
  229 + this.assertEnumEqual([1, 2, 3], values);
  230 + },
  231 +
  232 + testIterationWithEachPair: function() {
  233 + var hash = $H({a:1, b:2, c:3});
  234 + var keys = [];
  235 + var values = [];
  236 + hash.eachPair(function(key, value) {
  237 + keys.push(key);
  238 + values.push(value);
  239 + });
  240 + this.assertEnumEqual([1, 2, 3], values);
  241 + this.assertEnumEqual(['a', 'b', 'c'], keys);
194 242 }
195 243
196 244 });

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.