Neutral Political Machine

    obj-transform

    0.0.3 • Public • Published

    obj-transform

    Generates transform functions to apply to each property of an object

    Usage

    // commonJS...
    var transform = require('obj-transform');
    // ...or es2015
    import transform from 'obj-transform';
     
    // an incrementing transformer
    const increment = transform(x => x + 1);
    // enlarge a rectangle
    increment({height: 4, width: 3}); // {height: 5, width: 4}
    // move south-east
    increment({x: 0, y: 0}); // {x: 1, y: 1}
    // looping transform
    while (inbounds(ball)) {
      increment(ball);
    }  
     
    // ad-hoc transform
    transform(x => x * x)({a: 1, b: 2, c: 3}); // {a: 1, b: 4, c: 9}
     
    // a mixin transformer
    var mixin = transform(function(value, key) {return this[key] || value});
    mixin({a: 3, b: 9, c: 12}, {a: 1, c: 3}); // { a: 1, b: 9, c: 3 }

    Install

    npm install obj-transform
    

    API

    Creating a transform function

    transform(_callback_);

    callback is a function that can accept up to three arguments:

    value
    The value for each property in baseObj
    key (optional)
    The key for each property in baseObj
    baseObj (optional)
    The baseObj

    Using a transform function

    transformFunction(_object_[, _thisValue_]);

    Tests

    npm test
    

    Install

    npm i obj-transform

    DownloadsWeekly Downloads

    5

    Version

    0.0.3

    License

    MIT

    Last publish

    Collaborators

    • angus-c