Skip to content

Commit

Permalink
Add 0 division check so we don't kill the universe
Browse files Browse the repository at this point in the history
  • Loading branch information
joemckie committed Apr 12, 2017
1 parent c0d38d8 commit 1311bc3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/replacers/__tests__/operation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ describe('operation', function () {
.toThrowError('Unknown operator: a');
expect(() => operation.exec({operator: '+', v1: '10', v2: 0.5}))
.toThrowError('Operation value should be number, you try: 10');
expect(() => operation.exec({operator: '/', v1: 10, v2: 0}))
.toThrowError('Operation divisor should not be zero');
expect(() => operation.exec({operator: '+', v1: 10, v2: null}))
.toThrowError('Operation value should be number, you try: null');
});
Expand Down
9 changes: 9 additions & 0 deletions src/replacers/operation.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ function exec(opInfo) {
assertOperator(opInfo.operator);
assertValue(opInfo.v1);
assertValue(opInfo.v2);
if (opInfo.operator === '/') {
assertDivisor(opInfo.v2);
}
let fn = operators[opInfo.operator];
return fn(opInfo.v1, opInfo.v2);
}
Expand All @@ -63,3 +66,9 @@ function assertValue(value) {
throw new Error('Operation value should be number, you try: ' + value);
}
}

function assertDivisor(divisor) {
if (divisor === 0) {
throw new Error('Operation divisor should not be zero');
}
}

0 comments on commit 1311bc3

Please sign in to comment.