From 86a9315be47e7447950e2aae9a8612ecca45875d Mon Sep 17 00:00:00 2001 From: Cameron DeCoster Date: Fri, 29 Aug 2025 17:23:38 -0600 Subject: [PATCH 1/6] Normalize string before checking it --- index.js | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index b98a278..01889b1 100644 --- a/index.js +++ b/index.js @@ -14,8 +14,9 @@ const cs = { get: {}, }; -cs.get = function (string) { - const prefix = string.slice(0, 3).toLowerCase(); +cs.get = function (string = '') { + string = normalizeString(string); + const prefix = string.slice(0, 3); let value; let model; switch (prefix) { @@ -45,10 +46,8 @@ cs.get = function (string) { return {model, value}; }; -cs.get.rgb = function (string) { - if (!string) { - return null; - } +cs.get.rgb = function (string = '') { + string = normalizeString(string); const abbr = /^#([a-f\d]{3,4})$/i; const hex = /^#([a-f\d]{6})([a-f\d]{2})?$/i; @@ -127,10 +126,8 @@ cs.get.rgb = function (string) { return rgb; }; -cs.get.hsl = function (string) { - if (!string) { - return null; - } +cs.get.hsl = function (string = '') { + string = normalizeString(string); const hsl = /^hsla?\(\s*([+-]?(?:\d{0,3}\.)?\d+)(?:deg)?\s*,?\s*([+-]?[\d.]+)%\s*,?\s*([+-]?[\d.]+)%\s*(?:[,|/]\s*([+-]?(?=\.\d|\d)(?:0|[1-9]\d*)?(?:\.\d*)?(?:[eE][+-]?\d+)?)\s*)?\)$/; const match = string.match(hsl); @@ -148,10 +145,8 @@ cs.get.hsl = function (string) { return null; }; -cs.get.hwb = function (string) { - if (!string) { - return null; - } +cs.get.hwb = function (string = '') { + string = normalizeString(string); const hwb = /^hwb\(\s*([+-]?\d{0,3}(?:\.\d+)?)(?:deg)?\s*,\s*([+-]?[\d.]+)%\s*,\s*([+-]?[\d.]+)%\s*(?:,\s*([+-]?(?=\.\d|\d)(?:0|[1-9]\d*)?(?:\.\d*)?(?:[eE][+-]?\d+)?)\s*)?\)$/; const match = string.match(hwb); @@ -227,4 +222,10 @@ function hexDouble(number_) { return (string_.length < 2) ? '0' + string_ : string_; } +function normalizeString(string) { + string = string.toLowerCase(); + + return string; +} + export default cs; From 2d1ec8f08a047e97492fb1e8b987169490fb5dca Mon Sep 17 00:00:00 2001 From: Cameron DeCoster Date: Fri, 29 Aug 2025 17:23:57 -0600 Subject: [PATCH 2/6] Add tests for uppercase letters --- test.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test.js b/test.js index 405664c..fc29bc8 100644 --- a/test.js +++ b/test.js @@ -18,6 +18,8 @@ assert.deepEqual(string.get.rgb('rgb(244 233 100)'), [244, 233, 100, 1]); assert.deepEqual(string.get.rgb('rgb(100%, 30%, 90%)'), [255, 77, 229, 1]); assert.deepEqual(string.get.rgb('rgb(100% 30% 90%)'), [255, 77, 229, 1]); assert.deepEqual(string.get.rgb('transparent'), [0, 0, 0, 0]); +assert.deepEqual(string.get.rgb('blue'), [0, 0, 255, 1]); +assert.deepEqual(string.get.rgb('BLUE'), [0, 0, 255, 1]); assert.deepEqual(string.get.hsl('hsl(240, 100%, 50.5%)'), [240, 100, 50.5, 1]); assert.deepEqual(string.get.hsl('hsl(240 100% 50.5%)'), [240, 100, 50.5, 1]); assert.deepEqual(string.get.hsl('hsl(240deg, 100%, 50.5%)'), [240, 100, 50.5, 1]); @@ -37,6 +39,8 @@ assert.deepEqual(string.get('rgb(244 233 100)'), {model: 'rgb', value: [244, 233 assert.deepEqual(string.get('rgb(100%, 30%, 90%)'), {model: 'rgb', value: [255, 77, 229, 1]}); assert.deepEqual(string.get('rgb(100% 30% 90%)'), {model: 'rgb', value: [255, 77, 229, 1]}); assert.deepEqual(string.get('transparent'), {model: 'rgb', value: [0, 0, 0, 0]}); +assert.deepEqual(string.get('blue'), {model: 'rgb', value: [0, 0, 255, 1]}); +assert.deepEqual(string.get('BLUE'), {model: 'rgb', value: [0, 0, 255, 1]}); assert.deepEqual(string.get('hsl(240, 100%, 50.5%)'), {model: 'hsl', value: [240, 100, 50.5, 1]}); assert.deepEqual(string.get('hsl(-480, 100%, 50.5%)'), {model: 'hsl', value: [240, 100, 50.5, 1]}); assert.deepEqual(string.get('hsl(240 100% 50.5%)'), {model: 'hsl', value: [240, 100, 50.5, 1]}); From 98664ad770beda42e6c1d63bb27114832b9f399d Mon Sep 17 00:00:00 2001 From: Cameron DeCoster Date: Tue, 2 Sep 2025 13:38:28 -0600 Subject: [PATCH 3/6] Revert "Normalize string before checking it" This reverts commit 86a9315be47e7447950e2aae9a8612ecca45875d. --- index.js | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/index.js b/index.js index 01889b1..b98a278 100644 --- a/index.js +++ b/index.js @@ -14,9 +14,8 @@ const cs = { get: {}, }; -cs.get = function (string = '') { - string = normalizeString(string); - const prefix = string.slice(0, 3); +cs.get = function (string) { + const prefix = string.slice(0, 3).toLowerCase(); let value; let model; switch (prefix) { @@ -46,8 +45,10 @@ cs.get = function (string = '') { return {model, value}; }; -cs.get.rgb = function (string = '') { - string = normalizeString(string); +cs.get.rgb = function (string) { + if (!string) { + return null; + } const abbr = /^#([a-f\d]{3,4})$/i; const hex = /^#([a-f\d]{6})([a-f\d]{2})?$/i; @@ -126,8 +127,10 @@ cs.get.rgb = function (string = '') { return rgb; }; -cs.get.hsl = function (string = '') { - string = normalizeString(string); +cs.get.hsl = function (string) { + if (!string) { + return null; + } const hsl = /^hsla?\(\s*([+-]?(?:\d{0,3}\.)?\d+)(?:deg)?\s*,?\s*([+-]?[\d.]+)%\s*,?\s*([+-]?[\d.]+)%\s*(?:[,|/]\s*([+-]?(?=\.\d|\d)(?:0|[1-9]\d*)?(?:\.\d*)?(?:[eE][+-]?\d+)?)\s*)?\)$/; const match = string.match(hsl); @@ -145,8 +148,10 @@ cs.get.hsl = function (string = '') { return null; }; -cs.get.hwb = function (string = '') { - string = normalizeString(string); +cs.get.hwb = function (string) { + if (!string) { + return null; + } const hwb = /^hwb\(\s*([+-]?\d{0,3}(?:\.\d+)?)(?:deg)?\s*,\s*([+-]?[\d.]+)%\s*,\s*([+-]?[\d.]+)%\s*(?:,\s*([+-]?(?=\.\d|\d)(?:0|[1-9]\d*)?(?:\.\d*)?(?:[eE][+-]?\d+)?)\s*)?\)$/; const match = string.match(hwb); @@ -222,10 +227,4 @@ function hexDouble(number_) { return (string_.length < 2) ? '0' + string_ : string_; } -function normalizeString(string) { - string = string.toLowerCase(); - - return string; -} - export default cs; From d030d315edf29a2dbc42865fd46383ad1bb8d57e Mon Sep 17 00:00:00 2001 From: Cameron DeCoster Date: Tue, 2 Sep 2025 13:58:05 -0600 Subject: [PATCH 4/6] Make regex patterns case insensitive, transform keyword string to lowercase --- index.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index b98a278..a299e37 100644 --- a/index.js +++ b/index.js @@ -52,8 +52,8 @@ cs.get.rgb = function (string) { const abbr = /^#([a-f\d]{3,4})$/i; const hex = /^#([a-f\d]{6})([a-f\d]{2})?$/i; - const rgba = /^rgba?\(\s*([+-]?\d+)(?=[\s,])\s*(?:,\s*)?([+-]?\d+)(?=[\s,])\s*(?:,\s*)?([+-]?\d+)\s*(?:[\s,|/]\s*([+-]?[\d.]+)(%?)\s*)?\)$/; - const per = /^rgba?\(\s*([+-]?[\d.]+)%\s*,?\s*([+-]?[\d.]+)%\s*,?\s*([+-]?[\d.]+)%\s*(?:[\s,|/]\s*([+-]?[\d.]+)(%?)\s*)?\)$/; + const rgba = /^rgba?\(\s*([+-]?\d+)(?=[\s,])\s*(?:,\s*)?([+-]?\d+)(?=[\s,])\s*(?:,\s*)?([+-]?\d+)\s*(?:[\s,|/]\s*([+-]?[\d.]+)(%?)\s*)?\)$/i; + const per = /^rgba?\(\s*([+-]?[\d.]+)%\s*,?\s*([+-]?[\d.]+)%\s*,?\s*([+-]?[\d.]+)%\s*(?:[\s,|/]\s*([+-]?[\d.]+)(%?)\s*)?\)$/i; const keyword = /^(\w+)$/; let rgb = [0, 0, 0, 1]; @@ -101,7 +101,7 @@ cs.get.rgb = function (string) { if (match[4]) { rgb[3] = match[5] ? Number.parseFloat(match[4]) * 0.01 : Number.parseFloat(match[4]); } - } else if (match = string.match(keyword)) { + } else if (match = string.toLowerCase().match(keyword)) { if (match[1] === 'transparent') { return [0, 0, 0, 0]; } @@ -132,7 +132,7 @@ cs.get.hsl = function (string) { return null; } - const hsl = /^hsla?\(\s*([+-]?(?:\d{0,3}\.)?\d+)(?:deg)?\s*,?\s*([+-]?[\d.]+)%\s*,?\s*([+-]?[\d.]+)%\s*(?:[,|/]\s*([+-]?(?=\.\d|\d)(?:0|[1-9]\d*)?(?:\.\d*)?(?:[eE][+-]?\d+)?)\s*)?\)$/; + const hsl = /^hsla?\(\s*([+-]?(?:\d{0,3}\.)?\d+)(?:deg)?\s*,?\s*([+-]?[\d.]+)%\s*,?\s*([+-]?[\d.]+)%\s*(?:[,|/]\s*([+-]?(?=\.\d|\d)(?:0|[1-9]\d*)?(?:\.\d*)?(?:[eE][+-]?\d+)?)\s*)?\)$/i; const match = string.match(hsl); if (match) { @@ -153,7 +153,7 @@ cs.get.hwb = function (string) { return null; } - const hwb = /^hwb\(\s*([+-]?\d{0,3}(?:\.\d+)?)(?:deg)?\s*,\s*([+-]?[\d.]+)%\s*,\s*([+-]?[\d.]+)%\s*(?:,\s*([+-]?(?=\.\d|\d)(?:0|[1-9]\d*)?(?:\.\d*)?(?:[eE][+-]?\d+)?)\s*)?\)$/; + const hwb = /^hwb\(\s*([+-]?\d{0,3}(?:\.\d+)?)(?:deg)?\s*,\s*([+-]?[\d.]+)%\s*,\s*([+-]?[\d.]+)%\s*(?:,\s*([+-]?(?=\.\d|\d)(?:0|[1-9]\d*)?(?:\.\d*)?(?:[eE][+-]?\d+)?)\s*)?\)$/i; const match = string.match(hwb); if (match) { From cd57e3521f4ed9808db4789ad10ed2b245bde683 Mon Sep 17 00:00:00 2001 From: Cameron DeCoster Date: Tue, 2 Sep 2025 14:03:01 -0600 Subject: [PATCH 5/6] Update tests to include case sensitivity checks --- test.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test.js b/test.js index fc29bc8..2e90169 100644 --- a/test.js +++ b/test.js @@ -14,18 +14,18 @@ function normalizeAlpha(result) { assert.deepEqual(string.get.rgb('#fef'), [255, 238, 255, 1]); assert.deepEqual(string.get.rgb('#fffFEF'), [255, 255, 239, 1]); assert.deepEqual(string.get.rgb('rgb(244, 233, 100)'), [244, 233, 100, 1]); -assert.deepEqual(string.get.rgb('rgb(244 233 100)'), [244, 233, 100, 1]); +assert.deepEqual(string.get.rgb('RGB(244 233 100)'), [244, 233, 100, 1]); assert.deepEqual(string.get.rgb('rgb(100%, 30%, 90%)'), [255, 77, 229, 1]); -assert.deepEqual(string.get.rgb('rgb(100% 30% 90%)'), [255, 77, 229, 1]); +assert.deepEqual(string.get.rgb('RGB(100% 30% 90%)'), [255, 77, 229, 1]); assert.deepEqual(string.get.rgb('transparent'), [0, 0, 0, 0]); assert.deepEqual(string.get.rgb('blue'), [0, 0, 255, 1]); assert.deepEqual(string.get.rgb('BLUE'), [0, 0, 255, 1]); assert.deepEqual(string.get.hsl('hsl(240, 100%, 50.5%)'), [240, 100, 50.5, 1]); -assert.deepEqual(string.get.hsl('hsl(240 100% 50.5%)'), [240, 100, 50.5, 1]); +assert.deepEqual(string.get.hsl('HSL(240 100% 50.5%)'), [240, 100, 50.5, 1]); assert.deepEqual(string.get.hsl('hsl(240deg, 100%, 50.5%)'), [240, 100, 50.5, 1]); -assert.deepEqual(string.get.hsl('hsl(240deg 100% 50.5%)'), [240, 100, 50.5, 1]); +assert.deepEqual(string.get.hsl('hsl(240DEG 100% 50.5%)'), [240, 100, 50.5, 1]); assert.deepEqual(string.get.hwb('hwb(240, 100%, 50.5%)'), [240, 100, 50.5, 1]); -assert.deepEqual(string.get.hwb('hwb(240deg, 100%, 50.5%)'), [240, 100, 50.5, 1]); +assert.deepEqual(string.get.hwb('HWB(240DEG, 100%, 50.5%)'), [240, 100, 50.5, 1]); // Generic .get() assert.deepEqual(string.get('invalid'), null); From 030c1cb9eed7ebe4b73b86aaf9b1df50a2d6bef9 Mon Sep 17 00:00:00 2001 From: Cameron DeCoster Date: Sat, 18 Oct 2025 07:10:44 -0600 Subject: [PATCH 6/6] Simplify regex with case insensitivity --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 1b28876..05047ab 100644 --- a/index.js +++ b/index.js @@ -133,7 +133,7 @@ cs.get.hsl = function (string) { return null; } - const hsl = /^hsla?\(\s*([+-]?(?:\d{0,3}\.)?\d+)(?:deg)?\s*,?\s*([+-]?[\d.]+)%\s*,?\s*([+-]?[\d.]+)%\s*(?:[,|/]\s*([+-]?(?=\.\d|\d)(?:0|[1-9]\d*)?(?:\.\d*)?(?:[eE][+-]?\d+)?)\s*)?\)$/i; + const hsl = /^hsla?\(\s*([+-]?(?:\d{0,3}\.)?\d+)(?:deg)?\s*,?\s*([+-]?[\d.]+)%\s*,?\s*([+-]?[\d.]+)%\s*(?:[,|/]\s*([+-]?(?=\.\d|\d)(?:0|[1-9]\d*)?(?:\.\d*)?(?:e[+-]?\d+)?)\s*)?\)$/i; const match = string.match(hsl); if (match) { @@ -154,7 +154,7 @@ cs.get.hwb = function (string) { return null; } - const hwb = /^hwb\(\s*([+-]?\d{0,3}(?:\.\d+)?)(?:deg)?\s*[\s,]\s*([+-]?[\d.]+)%\s*[\s,]\s*([+-]?[\d.]+)%\s*(?:[\s,]\s*([+-]?(?=\.\d|\d)(?:0|[1-9]\d*)?(?:\.\d*)?(?:[eE][+-]?\d+)?)\s*)?\)$/i; + const hwb = /^hwb\(\s*([+-]?\d{0,3}(?:\.\d+)?)(?:deg)?\s*[\s,]\s*([+-]?[\d.]+)%\s*[\s,]\s*([+-]?[\d.]+)%\s*(?:[\s,]\s*([+-]?(?=\.\d|\d)(?:0|[1-9]\d*)?(?:\.\d*)?(?:e[+-]?\d+)?)\s*)?\)$/i; const match = string.match(hwb); if (match) {