Sha256: f060605988483a695ccc2102a8555154feece14111157724c4fa9590cb441025

Contents?: true

Size: 1.81 KB

Versions: 76

Compression:

Stored size: 1.81 KB

Contents

var reduce = require('../');
var test = require('tape');

test('numeric reduces', function (t) {
    t.plan(6);
    
    var xs = [ 1, 2, 3, 4 ];
    t.equal(
        reduce(xs, function (acc, x) { return acc + x }, 0),
        10
    );
    t.equal(
        reduce(xs, function (acc, x) { return acc + x }, 100),
        110
    );
    t.equal(
        reduce(xs, function (acc, x) { return acc + x }),
        10
    );
    
    var ys = cripple([ 1, 2, 3, 4 ]);
    t.equal(
        reduce(ys, function (acc, x) { return acc + x }, 0),
        10
    );
    t.equal(
        reduce(ys, function (acc, x) { return acc + x }, 100),
        110
    );
    t.equal(
        reduce(ys, function (acc, x) { return acc + x }),
        10
    );
});

test('holes', function (t) {
    t.plan(4);
    
    var xs = Array(10);
    xs[2] = 5; xs[4] = 6; xs[8] = 4;
    t.equal(
        reduce(xs, function (acc, x) { return acc + x }),
        15
    );
    t.equal(
        reduce(xs, function (acc, x) { return acc + x }, 100),
        115
    );
    
    var ys = cripple(Array(10));
    ys[2] = 5; ys[4] = 6; ys[8] = 4;
    t.equal(
        reduce(ys, function (acc, x) { return acc + x }),
        15
    );
    t.equal(
        reduce(ys, function (acc, x) { return acc + x }, 100),
        115
    );
});

test('object', function (t) {
    t.plan(1);
    var obj = { a: 3, b: 4, c: 5 };
    var res = reduce(objectKeys(obj), function (acc, key) {
        acc[key.toUpperCase()] = obj[key] * 111;
        return acc;
    }, {});
    t.deepEqual(res, { A: 333, B: 444, C: 555 });
});

function cripple (xs) {
    xs.reduce = undefined;
    return xs;
}

var objectKeys = function (obj) {
    var keys = [];
    for (var key in obj) {
        if (hasOwn.call(obj, key)) keys.push(key);
    }
    return keys;
};

var hasOwn = Object.prototype.hasOwnProperty;

Version data entries

76 entries across 76 versions & 4 rubygems

Version Path
trusty-festivity-extension-2.3.28 node_modules/bower/node_modules/shell-quote/node_modules/array-reduce/test/reduce.js
trusty-festivity-extension-2.3.27 node_modules/bower/node_modules/shell-quote/node_modules/array-reduce/test/reduce.js
trusty-festivity-extension-2.3.26 node_modules/bower/node_modules/shell-quote/node_modules/array-reduce/test/reduce.js
trusty-festivity-extension-2.3.25 node_modules/bower/node_modules/shell-quote/node_modules/array-reduce/test/reduce.js
trusty-festivity-extension-2.3.24 node_modules/bower/node_modules/shell-quote/node_modules/array-reduce/test/reduce.js
trusty-festivity-extension-2.3.23 node_modules/bower/node_modules/shell-quote/node_modules/array-reduce/test/reduce.js
trusty-festivity-extension-2.3.22 node_modules/bower/node_modules/shell-quote/node_modules/array-reduce/test/reduce.js
trusty-festivity-extension-2.3.21 node_modules/bower/node_modules/shell-quote/node_modules/array-reduce/test/reduce.js
trusty-festivity-extension-2.3.20 node_modules/bower/node_modules/shell-quote/node_modules/array-reduce/test/reduce.js
trusty-festivity-extension-2.3.19 node_modules/bower/node_modules/shell-quote/node_modules/array-reduce/test/reduce.js
trusty-festivity-extension-2.3.18 node_modules/bower/node_modules/shell-quote/node_modules/array-reduce/test/reduce.js
trusty-festivity-extension-2.3.17 node_modules/bower/node_modules/shell-quote/node_modules/array-reduce/test/reduce.js
trusty-festivity-extension-2.3.16 node_modules/bower/node_modules/shell-quote/node_modules/array-reduce/test/reduce.js
trusty-festivity-extension-2.3.15 node_modules/bower/node_modules/shell-quote/node_modules/array-reduce/test/reduce.js
trusty-festivity-extension-2.3.14 node_modules/bower/node_modules/shell-quote/node_modules/array-reduce/test/reduce.js
trusty-festivity-extension-2.3.13 node_modules/bower/node_modules/shell-quote/node_modules/array-reduce/test/reduce.js
trusty-festivity-extension-2.3.12 node_modules/bower/node_modules/shell-quote/node_modules/array-reduce/test/reduce.js
trusty-festivity-extension-2.3.11g node_modules/bower/node_modules/shell-quote/node_modules/array-reduce/test/reduce.js
trusty-festivity-extension-2.3.10 node_modules/bower/node_modules/shell-quote/node_modules/array-reduce/test/reduce.js
trusty-festivity-extension-2.3.9 node_modules/bower/node_modules/shell-quote/node_modules/array-reduce/test/reduce.js