Skip to content

Commit

Permalink
refactor: improve sanitizeValue function for readability
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
chessurisme committed Jul 31, 2024
1 parent 8c31332 commit a2bf6e4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/utilities/__tests__/sanitize-value.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
)
})

Expand All @@ -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.'
)
})

Expand Down
18 changes: 10 additions & 8 deletions src/utilities/sanitize-value.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.'],
Expand All @@ -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
}
})
Expand Down

0 comments on commit a2bf6e4

Please sign in to comment.