Ticket #221: rowgetter.2.patch

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

Adds rowgetter and leaves itemgetter alone. Adds tests for both.

  • doc/rst/MochiKit/Base.rst

    old new  
    12771277        Available in MochiKit 1.3.1+ 
    12781278 
    12791279 
     1280:mochidef:`rowgetter(names)`: 
     1281 
     1282    Returns a ``function(obj)`` that returns ``[obj[name1], obj[name2], ..., obj[nameN]]`` 
     1283    where ``names`` is the array ``[name1, name2, ..., nameN]`` 
     1284 
     1285    *Availability*: 
     1286        Available in MochiKit 1.4+ 
     1287 
     1288 
    12801289:mochidef:`serializeJSON(anObject)`: 
    12811290 
    12821291    Serialize ``anObject`` in the JSON [1]_ format, see `JSON 
  • MochiKit/Base.js

    old new  
    323323        }; 
    324324    }, 
    325325 
     326    /** @id MochiKit.Base.rowgetter */ 
     327    rowgetter: function (funcs) { 
     328        return function(arg) { 
     329            return map(function (f) { return arg[f]; }, funcs); 
     330        }; 
     331    }, 
     332 
    326333    /** @id MochiKit.Base.typeMatcher */ 
    327334    typeMatcher: function (/* typ */) { 
    328335        var types = {}; 
     
    12261233    "operator", 
    12271234    "forwardCall", 
    12281235    "itemgetter", 
     1236    "rowgetter", 
    12291237    "typeMatcher", 
    12301238    "isCallable", 
    12311239    "isUndefined", 
  • 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, "itemgetter, item exists" ); 
     505    t.is( itemgetter('x')(o), undefined, "itemgetter, item doesn't exist" ); 
     506 
     507    /* test rowgetter */ 
     508    var o = {a: 1, b: 2, c: 4, d: -1}; 
     509    t.is( repr(rowgetter(['a', 'c', 'd'])(o)), repr([1, 4, -1]), "rowgetter" ); 
     510    t.is( repr(rowgetter(['a', 'x', 'd'])(o)), repr([1, undefined, -1]), "rowgetter, nonexistent item" ); 
     511 
    502512};