From 7f8dbbfa87f65c4a7abf9a03842eb5edd1e41205 Mon Sep 17 00:00:00 2001 From: Carlos Valente Date: Thu, 23 Jan 2025 21:07:23 +0100 Subject: [PATCH] refactor: simplify string comparisons --- .../automation/__tests__/automation.service.test.ts | 10 ++++++++++ .../src/api-data/automation/automation.service.ts | 5 ++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/apps/server/src/api-data/automation/__tests__/automation.service.test.ts b/apps/server/src/api-data/automation/__tests__/automation.service.test.ts index 031cf2a8c..d00661f70 100644 --- a/apps/server/src/api-data/automation/__tests__/automation.service.test.ts +++ b/apps/server/src/api-data/automation/__tests__/automation.service.test.ts @@ -107,6 +107,16 @@ describe('testConditions()', () => { expect(result).toBe(true); }); + it('string comparisons should be case insensitive', () => { + const mockStore = makeRuntimeStateData({ eventNow: { title: 'Title' } as PlayableEvent }); + const result = testConditions( + [{ field: 'eventNow.title', operator: 'equals', value: 'title' }], + 'all', + mockStore, + ); + expect(result).toBe(true); + }); + it('should check if a value does not exist', () => { const mockStore = makeRuntimeStateData({ eventNow: null }); const result = testConditions([{ field: 'eventNow.title', operator: 'equals', value: '' }], 'all', mockStore); diff --git a/apps/server/src/api-data/automation/automation.service.ts b/apps/server/src/api-data/automation/automation.service.ts index c5fbffec2..ff49481ae 100644 --- a/apps/server/src/api-data/automation/automation.service.ts +++ b/apps/server/src/api-data/automation/automation.service.ts @@ -82,10 +82,13 @@ export function testConditions( switch (operator) { case 'equals': // handle the case where we are comparing boolean strings - if (typeof fieldValue === 'boolean') { return isBooleanEquals(fieldValue, lowerCasedValue); } + // make string comparisons case insensitive + if (typeof fieldValue === 'string') { + return fieldValue.toLowerCase() === lowerCasedValue; + } // overload the edge case where we use empty string to check if a value does not exist if (value === '' && fieldValue === undefined) { return true;