Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New methods for Hash. #71

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/prototype/ajax/request.js
Expand Up @@ -193,7 +193,7 @@ Ajax.Request = Class.create(Ajax.Base, {
// when GET, append parameters to URL // when GET, append parameters to URL
this.url += (this.url.include('?') ? '&' : '?') + params; this.url += (this.url.include('?') ? '&' : '?') + params;
} }

this.parameters = params.toQueryParams(); this.parameters = params.toQueryParams();


try { try {
Expand Down
116 changes: 116 additions & 0 deletions src/prototype/lang/hash.js
Expand Up @@ -104,6 +104,67 @@ var Hash = Class.create(Enumerable, (function() {
} }
} }


/**
* Hash#eachKey(iterator[, context]) -> Hash
*
* Iterates over the keys in the hash.
*
* ##### Example
*
* var hash = $H({England: 'London', Poland: 'Warsaw'});
*
* h.eachKey(function(country) {
* alert(country);
* });
* // Alerts: England
* // Alerts: Poland
*
**/
function eachKey(iterator, context) {
this.keys().each(iterator, context);
}

/**
* Hash#eachValue(iterator[, context]) -> Hash
*
* Iterates over the values in the hash.
*
* ##### Example
*
* var hash = $H({England: 'London', Poland: 'Warsaw'});
*
* h.eachValue(function(capital) {
* alert(capital);
* });
* // Alerts: London
* // Alerts: Warsaw
**/
function eachValue(iterator, context) {
this.values().each(iterator, context);
}

/**
* Hash#eachPair(iterator[, context]) -> Hash
*
* Iterates over the key/value pairs in the hash.
*
* ##### Example
*
* var hash = $H({England: 'London', Poland: 'Warsaw'});
*
* h.eachPair(function(country, capital) {
* alert(capital + "is the capital of " + country);
* });
* //Alerts: London is the capital of England
* //Alerts: Warsaw is the capital of Poland
*
**/
function eachPair(iterator, context) {
this.each(function(pair) {
iterator.call(context, pair.key, pair.value)
});
}

/** /**
* Hash#set(key, value) -> value * Hash#set(key, value) -> value
* - key (String): The key to use for this value. * - key (String): The key to use for this value.
Expand Down Expand Up @@ -261,6 +322,27 @@ var Hash = Class.create(Enumerable, (function() {
return this.clone().update(object); return this.clone().update(object);
} }


/**
* Hash#mergeWith(object, func) -> Hash
* - object (Object | Hash): The object to merge with this hash to produce
* the resulting hash.
* - func (Function): The function that will be applied to the values of the
* same keys. Takes two arguments.
*
* ##### Example
*
* var hash1 = $H({a: 1, b: 2, c: 2});
* var func = function(v1, v2) { return v1 + v2; };
*
* var hash2 = hash.mergeWith({a: 3, b: 4, c: 5, d: 9}, func);
* // -> {a: 4, b: 6, c: 7, d: 9}
*
*
**/
function mergeWith(object, func) {
return this.clone().updateWith(object, func);
}

/** /**
* Hash#update(object) -> Hash * Hash#update(object) -> Hash
* - object (Object | Hash): The object to merge with this hash to produce * - object (Object | Hash): The object to merge with this hash to produce
Expand All @@ -287,6 +369,35 @@ var Hash = Class.create(Enumerable, (function() {
}); });
} }


/**
* Hash#updateWith(object, func) -> Hash
* - object (Object | Hash): The object to merge with this hash to produce
* the resulting hash.
* - func (Function): The function that will be applied to the values of the
* same keys. Takes two arguments.
*
* ##### Example
*
* var hash = $H({a: 1, b: 2, c: 2});
* var func = function(v1, v2) { return v1 + v2; };
* hash.updateWith({a: 3, b: 4, c: 5, d: 9}, func);
* // -> {a: 4, b: 6, c: 7, d: 9}
*
*
**/
function updateWith(object, func) {
if (!Object.isFunction(func))
throw new TypeError();

var keys = this.keys();
var newHash = new Hash(object).inject(this, function(result, pair) {
var value = keys.include(pair.key)? func(this.get(pair.key), pair.value) : pair.value;
result.set(pair.key, value);
return result;
}.bind(this));
return newHash;
}

// Private. No PDoc necessary. // Private. No PDoc necessary.
function toQueryPair(key, value) { function toQueryPair(key, value) {
if (Object.isUndefined(value)) return key; if (Object.isUndefined(value)) return key;
Expand Down Expand Up @@ -388,6 +499,9 @@ var Hash = Class.create(Enumerable, (function() {
return { return {
initialize: initialize, initialize: initialize,
_each: _each, _each: _each,
eachKey: eachKey,
eachValue: eachValue,
eachPair: eachPair,
set: set, set: set,
get: get, get: get,
unset: unset, unset: unset,
Expand All @@ -397,7 +511,9 @@ var Hash = Class.create(Enumerable, (function() {
values: values, values: values,
index: index, index: index,
merge: merge, merge: merge,
mergeWith: mergeWith,
update: update, update: update,
updateWith: updateWith,
toQueryString: toQueryString, toQueryString: toQueryString,
inspect: inspect, inspect: inspect,
toJSON: toObject, toJSON: toObject,
Expand Down
50 changes: 49 additions & 1 deletion test/unit/hash_test.js
Expand Up @@ -4,7 +4,6 @@ new Test.Unit.Runner({


this.assertEqual('B', h.set('b', 'B')); this.assertEqual('B', h.set('b', 'B'));
this.assertHashEqual({a: 'A', b: 'B'}, h); this.assertHashEqual({a: 'A', b: 'B'}, h);

this.assertUndefined(h.set('c')); this.assertUndefined(h.set('c'));
this.assertHashEqual({a: 'A', b: 'B', c: undefined}, h); this.assertHashEqual({a: 'A', b: 'B', c: undefined}, h);
}, },
Expand Down Expand Up @@ -102,6 +101,16 @@ new Test.Unit.Runner({
this.assertHashEqual({a:'A#', b:'B', c:'C', d:'D#' }, h.merge(Fixtures.one)); this.assertHashEqual({a:'A#', b:'B', c:'C', d:'D#' }, h.merge(Fixtures.one));
}, },


testMergeWith: function() {
var h1 = $H({a: 1, b: 2, c: 2});
var func = function(v1, v2) { return v1 + v2;};
var h2 = h1.mergeWith({a: 3, b: 4, c: 5, d: 9}, func);
this.assertHashEqual($H({'a': 4, 'b': 6, 'c': 7, 'd': 9}), h2);
//this.assertHashEqual($H({'a': 1, 'b': 2, 'c': 2}), h1);
/*TypeError when function not defined*/
this.assertRaise('TypeError', function(){h1.mergeWith({});});
},

testUpdate: function() { testUpdate: function() {
var h = $H(Fixtures.many); var h = $H(Fixtures.many);
this.assertIdentical(h, h.update()); this.assertIdentical(h, h.update());
Expand All @@ -112,6 +121,15 @@ new Test.Unit.Runner({
this.assertHashEqual({a:'A', b:'B', c:'C', d:'D#', aaa:'AAA' }, h.update({aaa: 'AAA'})); this.assertHashEqual({a:'A', b:'B', c:'C', d:'D#', aaa:'AAA' }, h.update({aaa: 'AAA'}));
this.assertHashEqual({a:'A#', b:'B', c:'C', d:'D#', aaa:'AAA' }, h.update(Fixtures.one)); this.assertHashEqual({a:'A#', b:'B', c:'C', d:'D#', aaa:'AAA' }, h.update(Fixtures.one));
}, },

testUpdateWith: function() {
var h = $H({a: 1, b: 2, c: 2});
var func = function(v1, v2) { return v1 + v2;};
h.updateWith({a: 3, b: 4, c: 5, d: 9}, func);
this.assertHashEqual($H({'a': 4, 'b': 6, 'c': 7, 'd': 9}), h);
/*TypeError when function not defined*/
this.assertRaise('TypeError', function(){h.updateWith({});});
},


testToQueryString: function() { testToQueryString: function() {
this.assertEqual('', $H({}).toQueryString()); this.assertEqual('', $H({}).toQueryString());
Expand Down Expand Up @@ -191,6 +209,36 @@ new Test.Unit.Runner({
result.push(i); result.push(i);
}); });
this.assertEnumEqual([0,1], result); this.assertEnumEqual([0,1], result);
},

testIterationWithEachKey: function() {
var hash = $H({a:1, b:2, c:3});
var keys = [];
hash.eachKey(function(key) {
keys.push(key);
});
this.assertEnumEqual(['a', 'b', 'c'], keys);
},

testIterationWithEachValue: function() {
var hash = $H({a:1, b:2, c:3});
var values = [];
hash.eachValue(function(value) {
values.push(value);
});
this.assertEnumEqual([1, 2, 3], values);
},

testIterationWithEachPair: function() {
var hash = $H({a:1, b:2, c:3});
var keys = [];
var values = [];
hash.eachPair(function(key, value) {
keys.push(key);
values.push(value);
});
this.assertEnumEqual([1, 2, 3], values);
this.assertEnumEqual(['a', 'b', 'c'], keys);
} }


}); });