Ticket #221: itemgetter_array.patch

File itemgetter_array.patch, 1.2 kB (added by arnarbi@gmail.com, 2 years ago)

Patch to make itemgetter accept array-like param and tests

  • MochiKit/Base.js

    old new  
    318318 
    319319    /** @id MochiKit.Base.itemgetter */ 
    320320    itemgetter: function (func) { 
    321         return function (arg) { 
    322             return arg[func]; 
    323         }; 
     321        if (MochiKit.Base.isArrayLike(func)) 
     322            return function(arg) { 
     323                return map(function (f) { return arg[f]; }, func); 
     324            } 
     325        else 
     326            return function (arg) { 
     327                return arg[func]; 
     328            }; 
    324329    }, 
    325330 
    326331    /** @id MochiKit.Base.typeMatcher */ 
  • tests/test_Base.js

    old new  
    499499 
    500500    t.is( queryString([["foo", "bar"], ["baz", "wibble"]]), "foo=baz&bar=wibble" ); 
    501501 
     502    /* test itemgetter */ 
     503    var o = {a: 1, b: 2, c: 4, d: -1}; 
     504    t.is( itemgetter('c')(o), 4 ); 
     505    t.is( itemgetter('x')(o), undefined ); 
     506    t.is( repr(itemgetter(['a', 'c', 'd'])(o)), repr([1, 4, -1]) ); 
     507    t.is( repr(itemgetter(['a', 'x', 'd'])(o)), repr([1, undefined, -1]) ); 
     508 
    502509};