From a2bf6e40649a4675922bf6af59bb3b042a0c8fe5 Mon Sep 17 00:00:00 2001 From: Kyle Angelo Galendez Date: Wed, 31 Jul 2024 21:02:30 +0800 Subject: [PATCH] refactor: improve sanitizeValue function for readability Refactored the `sanitizeValue` function to enhance readability and maintainability. Changes include: Renamed `conditions` to `conditionList` for clarity. Adjusted formatting for better readability. Updated error messages for consistency and clarity. The function continues to validate the `value` and `type` parameters, logging errors and returning `null` if validations fail. --- src/utilities/__tests__/sanitize-value.test.js | 4 ++-- src/utilities/sanitize-value.js | 18 ++++++++++-------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/utilities/__tests__/sanitize-value.test.js b/src/utilities/__tests__/sanitize-value.test.js index 0451e14..5567753 100644 --- a/src/utilities/__tests__/sanitize-value.test.js +++ b/src/utilities/__tests__/sanitize-value.test.js @@ -33,7 +33,7 @@ describe('sanitizeValue', () => { it('should return null and log an error when value is of an invalid type', () => { expect(sanitizeValue(new Date(), 'string')).toBeNull() expect(console.error).toHaveBeenCalledWith( - 'Notice: The value and type are not equal. value is of type object, while type is string.' + 'Notice: The value and type are not equal. Value is of type object, while type is string.' ) }) @@ -45,7 +45,7 @@ describe('sanitizeValue', () => { it('should return null and log an error when value does not match the specified type', () => { expect(sanitizeValue('test', 'number')).toBeNull() expect(console.error).toHaveBeenCalledWith( - 'Notice: The value and type are not equal. value is of type string, while type is number.' + 'Notice: The value and type are not equal. Value is of type string, while type is number.' ) }) diff --git a/src/utilities/sanitize-value.js b/src/utilities/sanitize-value.js index dcbe7f8..c5a10db 100644 --- a/src/utilities/sanitize-value.js +++ b/src/utilities/sanitize-value.js @@ -10,13 +10,15 @@ */ function sanitizeValue(value, type) { let flag = false - const conditions = [ + const conditionList = [ [value === undefined || value === null, 'Invalid input: value is required.'], [!type, 'Invalid input: type is required.'], [ - !['boolean', 'string', 'object', 'number', 'bigint', 'symbol', 'function'].includes(typeof value) && - !Array.isArray(value) && - !(value instanceof HTMLElement), + !['boolean', 'string', 'object', 'number', 'bigint', 'symbol', 'function'].includes( + typeof value + ) && + !Array.isArray(value) && + !(value instanceof HTMLElement), 'Invalid input: value must be of a valid type (boolean, string, array, object, number, HTMLElement, bigint, symbol, or function).' ], [typeof type !== 'string', 'Invalid input: type must be a string.'], @@ -32,13 +34,13 @@ function sanitizeValue(value, type) { (type === 'null' && value === null) || type === typeof value ), - `Notice: The value and type are not equal. value is of type ${typeof value}, while type is ${type}.` + `Notice: The value and type are not equal. Value is of type ${typeof value}, while type is ${type}.` ] - ]; + ] conditionList.forEach((condition) => { - if (condition.condition) { - console.error(condition.message) + if (condition[0]) { + console.error(condition[1]) flag = true } })