From e1dc94f392384f95279459554ec354ffd6dd877e Mon Sep 17 00:00:00 2001 From: Inuka Wijerathna Date: Sat, 27 Jun 2026 10:33:07 +0530 Subject: [PATCH] fix: rgbToHsl no longer mutates its input array rgbToHsl aliased its argument with colorHsl = colorRgb instead of copying it, then overwrote colorHsl in place and returned that same array, destroying the caller's original RGB values. Copy the input with [...colorRgb] instead, and add a regression test asserting the input is left unchanged. --- Conversions/RgbHslConversion.js | 2 +- Conversions/test/RgbHslConversion.test.js | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/Conversions/RgbHslConversion.js b/Conversions/RgbHslConversion.js index 7e014f1318..c5cb562f78 100644 --- a/Conversions/RgbHslConversion.js +++ b/Conversions/RgbHslConversion.js @@ -22,7 +22,7 @@ const rgbToHsl = (colorRgb) => { throw new Error('Input is not a valid RGB color.') } - let colorHsl = colorRgb + let colorHsl = [...colorRgb] let red = Math.round(colorRgb[0]) let green = Math.round(colorRgb[1]) diff --git a/Conversions/test/RgbHslConversion.test.js b/Conversions/test/RgbHslConversion.test.js index 5dec4835cd..e63a6bcea7 100644 --- a/Conversions/test/RgbHslConversion.test.js +++ b/Conversions/test/RgbHslConversion.test.js @@ -40,4 +40,11 @@ describe('RgbHslConversion', () => { ])('Should return the error message.', (colorRgb, expected) => { expect(() => rgbToHsl(colorRgb)).toThrowError(expected) }) + + test('Should not mutate the input array.', () => { + const colorRgb = [24, 98, 118] + const result = rgbToHsl(colorRgb) + expect(colorRgb).toEqual([24, 98, 118]) + expect(result).not.toBe(colorRgb) + }) })