mirror of
https://github.com/nasa/openmct.git
synced 2025-06-01 23:20:50 +00:00
Merge pull request #2898 from nasa/fix-enum-comparison
Fixes enum comparisons
This commit is contained in:
commit
d5539c7ae4
@ -22,6 +22,18 @@
|
|||||||
|
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
|
|
||||||
|
const convertToNumbers = (input) => {
|
||||||
|
let numberInputs = [];
|
||||||
|
input.forEach(inputValue => numberInputs.push(Number(inputValue)));
|
||||||
|
return numberInputs;
|
||||||
|
};
|
||||||
|
|
||||||
|
const convertToStrings = (input) => {
|
||||||
|
let stringInputs = [];
|
||||||
|
input.forEach(inputValue => stringInputs.push(inputValue !== undefined ? inputValue.toString() : ''));
|
||||||
|
return stringInputs;
|
||||||
|
};
|
||||||
|
|
||||||
export const OPERATIONS = [
|
export const OPERATIONS = [
|
||||||
{
|
{
|
||||||
name: 'equalTo',
|
name: 'equalTo',
|
||||||
@ -98,8 +110,7 @@ export const OPERATIONS = [
|
|||||||
{
|
{
|
||||||
name: 'between',
|
name: 'between',
|
||||||
operation: function (input) {
|
operation: function (input) {
|
||||||
let numberInputs = [];
|
let numberInputs = convertToNumbers(input);
|
||||||
input.forEach(inputValue => numberInputs.push(Number(inputValue)));
|
|
||||||
let larger = Math.max(...numberInputs.slice(1,3));
|
let larger = Math.max(...numberInputs.slice(1,3));
|
||||||
let smaller = Math.min(...numberInputs.slice(1,3));
|
let smaller = Math.min(...numberInputs.slice(1,3));
|
||||||
return (numberInputs[0] > smaller) && (numberInputs[0] < larger);
|
return (numberInputs[0] > smaller) && (numberInputs[0] < larger);
|
||||||
@ -114,8 +125,7 @@ export const OPERATIONS = [
|
|||||||
{
|
{
|
||||||
name: 'notBetween',
|
name: 'notBetween',
|
||||||
operation: function (input) {
|
operation: function (input) {
|
||||||
let numberInputs = [];
|
let numberInputs = convertToNumbers(input);
|
||||||
input.forEach(inputValue => numberInputs.push(Number(inputValue)));
|
|
||||||
let larger = Math.max(...numberInputs.slice(1,3));
|
let larger = Math.max(...numberInputs.slice(1,3));
|
||||||
let smaller = Math.min(...numberInputs.slice(1,3));
|
let smaller = Math.min(...numberInputs.slice(1,3));
|
||||||
return (numberInputs[0] < smaller) || (numberInputs[0] > larger);
|
return (numberInputs[0] < smaller) || (numberInputs[0] > larger);
|
||||||
@ -214,7 +224,8 @@ export const OPERATIONS = [
|
|||||||
{
|
{
|
||||||
name: 'enumValueIs',
|
name: 'enumValueIs',
|
||||||
operation: function (input) {
|
operation: function (input) {
|
||||||
return input[0] === input[1];
|
let stringInputs = convertToStrings(input);
|
||||||
|
return stringInputs[0] === stringInputs[1];
|
||||||
},
|
},
|
||||||
text: 'is',
|
text: 'is',
|
||||||
appliesTo: ['enum'],
|
appliesTo: ['enum'],
|
||||||
@ -226,7 +237,8 @@ export const OPERATIONS = [
|
|||||||
{
|
{
|
||||||
name: 'enumValueIsNot',
|
name: 'enumValueIsNot',
|
||||||
operation: function (input) {
|
operation: function (input) {
|
||||||
return input[0] !== input[1];
|
let stringInputs = convertToStrings(input);
|
||||||
|
return stringInputs[0] !== stringInputs[1];
|
||||||
},
|
},
|
||||||
text: 'is not',
|
text: 'is not',
|
||||||
appliesTo: ['enum'],
|
appliesTo: ['enum'],
|
||||||
@ -238,9 +250,10 @@ export const OPERATIONS = [
|
|||||||
{
|
{
|
||||||
name: 'valueIs',
|
name: 'valueIs',
|
||||||
operation: function (input) {
|
operation: function (input) {
|
||||||
|
const lhsValue = input[0] !== undefined ? input[0].toString() : '';
|
||||||
if (input[1]) {
|
if (input[1]) {
|
||||||
const values = input[1].split(',');
|
const values = input[1].split(',');
|
||||||
return values.find((value) => input[0].toString() === _.trim(value.toString()));
|
return values.find((value) => lhsValue === _.trim(value.toString()));
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
},
|
},
|
||||||
@ -254,9 +267,10 @@ export const OPERATIONS = [
|
|||||||
{
|
{
|
||||||
name: 'valueIsNot',
|
name: 'valueIsNot',
|
||||||
operation: function (input) {
|
operation: function (input) {
|
||||||
|
const lhsValue = input[0] !== undefined ? input[0].toString() : '';
|
||||||
if (input[1]) {
|
if (input[1]) {
|
||||||
const values = input[1].split(',');
|
const values = input[1].split(',');
|
||||||
const found = values.find((value) => input[0].toString() === _.trim(value.toString()));
|
const found = values.find((value) => lhsValue === _.trim(value.toString()));
|
||||||
return !found;
|
return !found;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
@ -25,8 +25,10 @@ let isOneOfOperation = OPERATIONS.find((operation) => operation.name === 'valueI
|
|||||||
let isNotOneOfOperation = OPERATIONS.find((operation) => operation.name === 'valueIsNot');
|
let isNotOneOfOperation = OPERATIONS.find((operation) => operation.name === 'valueIsNot');
|
||||||
let isBetween = OPERATIONS.find((operation) => operation.name === 'between');
|
let isBetween = OPERATIONS.find((operation) => operation.name === 'between');
|
||||||
let isNotBetween = OPERATIONS.find((operation) => operation.name === 'notBetween');
|
let isNotBetween = OPERATIONS.find((operation) => operation.name === 'notBetween');
|
||||||
|
let enumIsOperation = OPERATIONS.find((operation) => operation.name === 'enumValueIs');
|
||||||
|
let enumIsNotOperation = OPERATIONS.find((operation) => operation.name === 'enumValueIsNot');
|
||||||
|
|
||||||
describe('Is one of and is not one of operations', function () {
|
describe('operations', function () {
|
||||||
|
|
||||||
it('should evaluate isOneOf to true for number inputs', () => {
|
it('should evaluate isOneOf to true for number inputs', () => {
|
||||||
const inputs = [45, "5,6,45,8"];
|
const inputs = [45, "5,6,45,8"];
|
||||||
@ -87,4 +89,54 @@ describe('Is one of and is not one of operations', function () {
|
|||||||
const inputs = ["45", "30", "50"];
|
const inputs = ["45", "30", "50"];
|
||||||
expect(!!isNotBetween.operation(inputs)).toBeFalse();
|
expect(!!isNotBetween.operation(inputs)).toBeFalse();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should evaluate enumValueIs to true for number inputs', () => {
|
||||||
|
const inputs = [1, "1"];
|
||||||
|
expect(!!enumIsOperation.operation(inputs)).toBeTrue();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should evaluate enumValueIs to true for string inputs', () => {
|
||||||
|
const inputs = ["45", "45"];
|
||||||
|
expect(!!enumIsOperation.operation(inputs)).toBeTrue();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should evaluate enumValueIsNot to true for number inputs', () => {
|
||||||
|
const inputs = [45, "46"];
|
||||||
|
expect(!!enumIsNotOperation.operation(inputs)).toBeTrue();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should evaluate enumValueIsNot to true for string inputs', () => {
|
||||||
|
const inputs = ["45", "46"];
|
||||||
|
expect(!!enumIsNotOperation.operation(inputs)).toBeTrue();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should evaluate enumValueIs to false for number inputs', () => {
|
||||||
|
const inputs = [1, "2"];
|
||||||
|
expect(!!enumIsOperation.operation(inputs)).toBeFalse();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should evaluate enumValueIs to false for string inputs', () => {
|
||||||
|
const inputs = ["45", "46"];
|
||||||
|
expect(!!enumIsOperation.operation(inputs)).toBeFalse();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should evaluate enumValueIsNot to false for number inputs', () => {
|
||||||
|
const inputs = [45, "45"];
|
||||||
|
expect(!!enumIsNotOperation.operation(inputs)).toBeFalse();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should evaluate enumValueIsNot to false for string inputs', () => {
|
||||||
|
const inputs = ["45", "45"];
|
||||||
|
expect(!!enumIsNotOperation.operation(inputs)).toBeFalse();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should evaluate enumValueIs to false for undefined input', () => {
|
||||||
|
const inputs = [undefined, "45"];
|
||||||
|
expect(!!enumIsOperation.operation(inputs)).toBeFalse();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should evaluate enumValueIsNot to true for undefined input', () => {
|
||||||
|
const inputs = [undefined, "45"];
|
||||||
|
expect(!!enumIsNotOperation.operation(inputs)).toBeTrue();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user