Cast number inputs to a Number so that operations evaluate correctly (#2799)

* Cast number inputs to a Number so that operations evaluate correctly
This commit is contained in:
Shefali Joshi 2020-03-30 10:37:55 -07:00 committed by GitHub
parent d00e8b68a5
commit 84874f22e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 8 deletions

View File

@ -4,7 +4,7 @@ export const OPERATIONS = [
{
name: 'equalTo',
operation: function (input) {
return input[0] === parseInt(input[1]);
return Number(input[0]) === Number(input[1]);
},
text: 'is equal to',
appliesTo: ['number'],
@ -16,7 +16,7 @@ export const OPERATIONS = [
{
name: 'notEqualTo',
operation: function (input) {
return input[0] !== parseInt(input[1]);
return Number(input[0]) !== Number(input[1]);
},
text: 'is not equal to',
appliesTo: ['number'],
@ -28,7 +28,7 @@ export const OPERATIONS = [
{
name: 'greaterThan',
operation: function (input) {
return input[0] > parseInt(input[1]);
return Number(input[0]) > Number(input[1]);
},
text: 'is greater than',
appliesTo: ['number'],
@ -40,7 +40,7 @@ export const OPERATIONS = [
{
name: 'lessThan',
operation: function (input) {
return input[0] < parseInt(input[1]);
return Number(input[0]) < Number(input[1]);
},
text: 'is less than',
appliesTo: ['number'],
@ -52,7 +52,7 @@ export const OPERATIONS = [
{
name: 'greaterThanOrEq',
operation: function (input) {
return input[0] >= parseInt(input[1]);
return Number(input[0]) >= Number(input[1]);
},
text: 'is greater than or equal to',
appliesTo: ['number'],
@ -64,7 +64,7 @@ export const OPERATIONS = [
{
name: 'lessThanOrEq',
operation: function (input) {
return input[0] <= parseInt(input[1]);
return Number(input[0]) <= Number(input[1]);
},
text: 'is less than or equal to',
appliesTo: ['number'],
@ -76,7 +76,11 @@ export const OPERATIONS = [
{
name: 'between',
operation: function (input) {
return input[0] > parseInt(input[1]) && input[0] < parseInt(input[2]);
let numberInputs = [];
input.forEach(inputValue => numberInputs.push(Number(inputValue)));
let larger = Math.max(...numberInputs.slice(1,3));
let smaller = Math.min(...numberInputs.slice(1,3));
return (numberInputs[0] > smaller) && (numberInputs[0] < larger);
},
text: 'is between',
appliesTo: ['number'],
@ -88,7 +92,11 @@ export const OPERATIONS = [
{
name: 'notBetween',
operation: function (input) {
return input[0] < parseInt(input[1]) || input[0] > parseInt(input[2]);
let numberInputs = [];
input.forEach(inputValue => numberInputs.push(Number(inputValue)));
let larger = Math.max(...numberInputs.slice(1,3));
let smaller = Math.min(...numberInputs.slice(1,3));
return (numberInputs[0] < smaller) || (numberInputs[0] > larger);
},
text: 'is not between',
appliesTo: ['number'],

View File

@ -23,6 +23,8 @@
import { OPERATIONS } from "./operations";
let isOneOfOperation = OPERATIONS.find((operation) => operation.name === 'valueIs');
let isNotOneOfOperation = OPERATIONS.find((operation) => operation.name === 'valueIsNot');
let isBetween = OPERATIONS.find((operation) => operation.name === 'between');
let isNotBetween = OPERATIONS.find((operation) => operation.name === 'notBetween');
describe('Is one of and is not one of operations', function () {
@ -65,4 +67,24 @@ describe('Is one of and is not one of operations', function () {
const inputs = ["4", "5,46,4,8"];
expect(!!isNotOneOfOperation.operation(inputs)).toBeFalse();
});
it('should evaluate isBetween to true', () => {
const inputs = ["4", "3", "89"];
expect(!!isBetween.operation(inputs)).toBeTrue();
});
it('should evaluate isNotBetween to true', () => {
const inputs = ["45", "100", "89"];
expect(!!isNotBetween.operation(inputs)).toBeTrue();
});
it('should evaluate isBetween to false', () => {
const inputs = ["4", "100", "89"];
expect(!!isBetween.operation(inputs)).toBeFalse();
});
it('should evaluate isNotBetween to false', () => {
const inputs = ["45", "30", "50"];
expect(!!isNotBetween.operation(inputs)).toBeFalse();
});
});