forked from jonschlinkert/object.reduce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
57 lines (47 loc) · 1.44 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*!
* object.reduce <https://github.com/jonschlinkert/object.reduce>
*
* Copyright (c) 2014-2015, Jon Schlinkert.
* Licensed under the MIT License.
*/
'use strict';
require('mocha');
var assert = require('assert');
var reduce = require('./');
describe('.reduce()', function() {
it('should use an initializer for the accumulator', function() {
var a = {a: 'foo', b: 'bar', c: {}};
var init = {};
reduce(a, function(acc, value, key, orig) {
if (typeof value === 'object') {
acc[key] = {what: 'who?'};
} else {
acc[key] = value.toUpperCase();
}
return acc;
}, init);
assert.deepEqual(init, {a: 'FOO', b: 'BAR', c: {what: 'who?'}});
});
it('should take a context as the last argument', function() {
var a = {a: 'foo', b: 'bar', c: {}};
var ctx = {a: 'x', b: 'y', c: 'z'};
var init = {};
reduce(a, function(acc, value, key, orig) {
acc[key] = this[key];
return acc;
}, init, ctx);
assert.deepEqual(init, {a: 'x', b: 'y', c: 'z'});
});
it('should run each property in the obj through the callback.', function() {
var a = {a: 'foo', b: 'bar', c: {}};
var obj = reduce(a, function(acc, value, key, orig) {
if (typeof value === 'object') {
acc[key] = {what: 'who?'};
} else {
acc[key] = value.toUpperCase();
}
return acc;
}, {});
assert.deepEqual(obj, {a: 'FOO', b: 'BAR', c: {what: 'who?'}});
});
});