Hi Julian,
So I have a couple comments. First, toHash would probably fit prototype's
style more. Second, in the implementation for hashify:

else if (Object.isArray(item) && item.size()>0) hash.set(item[0], item[1]);

This is disturbing to me, because if the array is 3 elements or more, you
essentially discard any values after the first two.

Lastly, I think the biggest problem is that there are a thousand different
ways to convert an array to a hash and each person would find a different
one useful. For example in one of my own projects I do this:

Object.extend(Array.prototype, {
  toHash: function(){
    var hash = $H();
    this.each(function(value, i){
      hash.set(i, value);
    });
    return hash;
  }
}

IMHO, this doesn't belong in core.

Allen Madsen
http://www.allenmadsen.com


On Tue, Sep 8, 2009 at 6:03 AM, Green <julian.dreis...@googlemail.com>wrote:

>
> 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