Skip to content

Commit

Permalink
Fix terminate invocation in flatten and flattenDeep
Browse files Browse the repository at this point in the history
Closes #1
  • Loading branch information
B3rn475 committed Nov 15, 2017
1 parent 29322c0 commit f09a5fa
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,17 @@ function reduceBy(key, policy) {
}

function _flattenUnpack(status) {
return status && status.accumulated;
if (status.started) {
return status && status.accumulated;
}
}

function _flattenTerminate(policy) {
if (policy.hasOwnProperty('terminate')) {
return function (status) {
return policy.terminate(_flattenUnpack(status));
if (status.started) {
return policy.terminate(status && status.accumulated);
}
};
}
return _flattenUnpack;
Expand Down
34 changes: 34 additions & 0 deletions test/reducer/flatten.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,40 @@ describe('flatten', function () {
assert.equal(invoke([[first], []], reduce), result);
assert.ok(accumulate.calledOnce);
});
it('should not invoke terminate if no elements are passed', function () {
var terminate = sinon.spy(),
policy = r.reduce(_.noop, undefined, terminate),
reduce = r.flatten(policy);
assert.equal(invoke([], reduce), undefined);
assert.ok(!terminate.called);
});
it('should not invoke terminate if no elements are passed one level deep', function () {
var terminate = sinon.spy(),
policy = r.reduce(_.noop, undefined, terminate),
reduce = r.flatten(policy);
assert.equal(invoke([[], []], reduce), undefined);
assert.ok(!terminate.called);
});
it('should invoke terminate if no elements are passed two level deep', function () {
var terminate = sinon.spy(),
policy = r.reduce(_.noop, undefined, terminate),
reduce = r.flatten(policy);
assert.equal(invoke([[[]], []], reduce), undefined);
assert.ok(terminate.calledOnce);
});
it('should return terminate result', function () {
var result = {},
accumulator = {accumulator: true},
terminate = sinon.spy(function (accumulated) {
assert.notEqual(accumulated, accumulator);
assert.deepEqual(accumulated, accumulator);
return result;
}),
policy = r.reduce(_.identity, accumulator, terminate),
reduce = r.flatten(policy);
assert.equal(invoke([{}], reduce), result);
assert.ok(terminate.calledOnce);
});
it('should concat by default', function () {
var first = {},
second = {},
Expand Down
34 changes: 34 additions & 0 deletions test/reducer/flattenDeep.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,40 @@ describe('flattenDeep', function () {
assert.equal(invoke([[[first]], []], reduce), result);
assert.ok(accumulate.calledOnce);
});
it('should not invoke terminate if no elements are passed', function () {
var terminate = sinon.spy(),
policy = r.reduce(_.noop, undefined, terminate),
reduce = r.flatten(policy);
assert.equal(invoke([], reduce), undefined);
assert.ok(!terminate.called);
});
it('should not invoke terminate if no elements are passed one level deep', function () {
var terminate = sinon.spy(),
policy = r.reduce(_.noop, undefined, terminate),
reduce = r.flattenDeep(policy);
assert.equal(invoke([[], []], reduce), undefined);
assert.ok(!terminate.called);
});
it('should not invoke terminate if no elements are passed two level deep', function () {
var terminate = sinon.spy(),
policy = r.reduce(_.noop, undefined, terminate),
reduce = r.flattenDeep(policy);
assert.equal(invoke([[[]], []], reduce), undefined);
assert.ok(!terminate.called);
});
it('should return terminate result', function () {
var result = {},
accumulator = {accumulator: true},
terminate = sinon.spy(function (accumulated) {
assert.notEqual(accumulated, accumulator);
assert.deepEqual(accumulated, accumulator);
return result;
}),
policy = r.reduce(_.identity, accumulator, terminate),
reduce = r.flattenDeep(policy);
assert.equal(invoke([{}], reduce), result);
assert.ok(terminate.calledOnce);
});
it('should concat by default', function () {
var first = {},
second = {},
Expand Down
17 changes: 17 additions & 0 deletions test/reducer/reduce.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,21 @@ describe('reduce', function () {
assert.equal(invoke([first], reduce), result);
assert.ok(accumulate.calledOnce);
});
it('should invoke terminate even if no elements are passed', function () {
var terminate = sinon.spy(),
reduce = r.reduce(_.noop, undefined, terminate);
assert.equal(invoke([], reduce), undefined);
assert.ok(terminate.calledOnce);
});
it('should return terminate result', function () {
var result = [],
accumulator = {},
terminate = sinon.spy(function (accumulated) {
assert.notEqual(accumulated, accumulator);
assert.deepEqual(accumulated, accumulator);
return result;
}),
reduce = r.reduce(_.noop, accumulator, terminate);
assert.equal(invoke([], reduce), result);
});
});

0 comments on commit f09a5fa

Please sign in to comment.