Often I have to select some parts of a Hash, like

   $H({a: 1, b: "xxx", c: 99}.findAll(function(item) {
      return Object.isNumber(item.value);
   });

The returned result is an Array of Arrays/Objects,

   -> [['a', 1], ['c', 99]] or, equivalently [{key: 'a', value: 1},
{key: ā€˜cā€˜, value: 99}]

When working with Hashes I often needed a way to turn these results
into hashes again.
I therefore added a 'hashify' method to the Array class:

   (function() {
      var hashify = (function(array) {
         var hash = $H();
         array.each(function(item) {
            if (!Object.isUndefined(item.key)) hash.set(item.key,
item.value);
            else if (Object.isArray(item) && item.size()>0) hash.set
(item[0], item[1]);
         });
         return hash;
      }).methodize();
      Object.extend(Array.prototype, {hashify: hashify, $H: hashify});
   })();

With this addition you may write

   $H({...}).findAll(function(item) {...}).hashify().get('c');    ->
99

One could also think of adding this behaviour to the Hash constructor,
e.g. for 1.6.1

   Object.extend(Hash.prototype, {
      initialize: function(object) {
         if (Object.isHash(object)) this._object = object.toObject();
         else if (Object.isArray(object)) this._object = object.hashify
().toObject();
         else this._object = Object.clone(object);
      }
   });

This way you would be able to write

   $H([{key: 'c', value: 99}].get('c');    ->  99

I guess this could be a useful core behaviour, what do you think?
- Julian
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype: Core" group.
To post to this group, send email to prototype-core@googlegroups.com
To unsubscribe from this group, send email to 
prototype-core-unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-core?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to