#539 bug
romain.dorgueil (at symfony-project)

Enumerable.collect does not work on Hash objects

Reported by romain.dorgueil (at symfony-project) | February 5th, 2009 @ 03:33 PM

Hash.prototype.collect does not follow the "Hash" logic. It uses Enumerable.collect which does not keep keys but instead recreate numeric keys.

I'm pretty new to prototype though so i can be missing «the way» to achieve this.

Bellow is some code that shows what behavior i was expecting, and the difference between result of the two implementations. It uses FireBug to dump the Hashes


/**
 * collect - implements Enumerable.collect for hash
 */
Hash.prototype.collectme = function(iterator, context) {
  iterator = iterator || Prototype.K;
  var results = new Hash();
  this.each(function(value, index) {
    results.set(value[0], iterator.call(context, value[1], value[0]));
  });
  return results;
};

/*
 * Test our collect() version and compare it against original prototype Hash.collect()
 */
var testHash = $H({key1:'val1', key2:'val2'});

var testCollect = function (value, index) {
  return 'get('+index+') => '+value;
}

console.dir({
  originalcollect: testHash.collect(testCollect),
  mycollect: testHash.collectme(testCollect)
});

Comments and changes to this ticket

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

The Prototype JavaScript library.

Shared Ticket Bins

Pages