Add Hash.prototype.index which returns the first found property that has a specific value. Closes #8528. [Thomas Fuchs, slusarz, Mislav Marohnic]

This commit is contained in:
Thomas Fuchs 2007-06-10 11:09:06 +00:00
parent ae2ea8a2e9
commit 51bb86dc39
3 changed files with 26 additions and 1 deletions

View File

@ -1,5 +1,11 @@
*SVN*
* Add Hash.prototype.index which returns the first found property that has a specific value. Closes #8528. [Thomas Fuchs, slusarz, Mislav Marohnić]
Examples:
var hash = $H({a:1,b:'2'});
hash.index(1) // -> 'a'
hash.index('1') // -> undefined
* Ensure HTMLElement exists before creating Element.extend. [Tobie Langel]
* Add Number.prototype.round/ceil/floor/abs as an aliases to the respective methods in Math. Refactor to seperate number extensions from base.js. [Thomas Fuchs]

View File

@ -62,6 +62,13 @@ Object.extend(Hash.prototype, {
return this.pluck('value');
},
index: function(value) {
var match = this.detect(function(pair) {
return pair.value === value;
});
return match && match.key;
},
merge: function(hash) {
return $H(hash).inject(this, function(mergedHash, pair) {
mergedHash[pair.key] = pair.value;

View File

@ -89,7 +89,19 @@
$H(Fixtures.functions).values().map(function(i){ return typeof i }));
assertEqual(9, $H(Fixtures.functions).quad(3));
assertEqual(6, $H(Fixtures.functions).plus(3));
}},
}},
testIndex: function(){ with(this) {
assertUndefined($H().index('foo'));
assert('a', $H(Fixtures.one).index('A#'));
assert('a', $H(Fixtures.many).index('A'));
assertUndefined($H(Fixtures.many).index('Z'))
var hash = $H({a:1,b:'2',c:1});
assert(['a','c'].include(hash.index(1)));
assertUndefined(hash.index('1'));
}},
testMerge: function(){ with(this) {
assertHashEqual(Fixtures.many, $H(Fixtures.many).merge());