Skip to content

Commit dedbb42

Browse files
authored
feat: Make string checks case insensitive (#81)
* Normalize string before checking it * Add tests for uppercase letters * Revert "Normalize string before checking it" This reverts commit 86a9315. * Make regex patterns case insensitive, transform keyword string to lowercase * Update tests to include case sensitivity checks * Simplify regex with case insensitivity
1 parent bb67f9f commit dedbb42

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ cs.get.rgb = function (string) {
5353
const abbr = /^#([a-f\d]{3,4})$/i;
5454
const hex = /^#([a-f\d]{6})([a-f\d]{2})?$/i;
5555
const rgba = /^rgba?\(\s*([+-]?(?:\d*\.)?\d+(?:e\d+)?)(?=[\s,])\s*(?:,\s*)?([+-]?(?:\d*\.)?\d+(?:e\d+)?)(?=[\s,])\s*(?:,\s*)?([+-]?(?:\d*\.)?\d+(?:e\d+)?)\s*(?:[\s,|/]\s*([+-]?(?:\d*\.)?\d+(?:e\d+)?)(%?)\s*)?\)$/i;
56-
const per = /^rgba?\(\s*([+-]?[\d.]+)%\s*,?\s*([+-]?[\d.]+)%\s*,?\s*([+-]?[\d.]+)%\s*(?:[\s,|/]\s*([+-]?[\d.]+)(%?)\s*)?\)$/;
56+
const per = /^rgba?\(\s*([+-]?[\d.]+)%\s*,?\s*([+-]?[\d.]+)%\s*,?\s*([+-]?[\d.]+)%\s*(?:[\s,|/]\s*([+-]?[\d.]+)(%?)\s*)?\)$/i;
5757
const keyword = /^(\w+)$/;
5858

5959
let rgb = [0, 0, 0, 1];
@@ -101,7 +101,7 @@ cs.get.rgb = function (string) {
101101
if (match[4]) {
102102
rgb[3] = match[5] ? Number.parseFloat(match[4]) * 0.01 : Number.parseFloat(match[4]);
103103
}
104-
} else if (match = string.match(keyword)) {
104+
} else if (match = string.toLowerCase().match(keyword)) {
105105
if (match[1] === 'transparent') {
106106
return [0, 0, 0, 0];
107107
}
@@ -133,7 +133,7 @@ cs.get.hsl = function (string) {
133133
return null;
134134
}
135135

136-
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*)?\)$/;
136+
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;
137137
const match = string.match(hsl);
138138

139139
if (match) {
@@ -154,7 +154,7 @@ cs.get.hwb = function (string) {
154154
return null;
155155
}
156156

157-
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*)?\)$/;
157+
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;
158158
const match = string.match(hwb);
159159

160160
if (match) {

test.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,22 @@ function normalizeAlpha(result) {
1414
assert.deepEqual(string.get.rgb('#fef'), [255, 238, 255, 1]);
1515
assert.deepEqual(string.get.rgb('#fffFEF'), [255, 255, 239, 1]);
1616
assert.deepEqual(string.get.rgb('rgb(244, 233, 100)'), [244, 233, 100, 1]);
17-
assert.deepEqual(string.get.rgb('rgb(244 233 100)'), [244, 233, 100, 1]);
17+
assert.deepEqual(string.get.rgb('RGB(244 233 100)'), [244, 233, 100, 1]);
1818
assert.deepEqual(string.get.rgb('rgb(244.5, 233.5, 100.5)'), [244.5, 233.5, 100.5, 1]);
1919
assert.deepEqual(string.get.rgb('rgb(244.5 233.5 100.5)'), [244.5, 233.5, 100.5, 1]);
2020
assert.deepEqual(string.get.rgb('rgb(2.44E2, 2.33e2, 1.00E2)'), [244, 233, 100, 1]);
2121
assert.deepEqual(string.get.rgb('rgb(2.44E2 2.33e2 1.00E2)'), [244, 233, 100, 1]);
2222
assert.deepEqual(string.get.rgb('rgb(100%, 30%, 90%)'), [255, 77, 229, 1]);
23-
assert.deepEqual(string.get.rgb('rgb(100% 30% 90%)'), [255, 77, 229, 1]);
23+
assert.deepEqual(string.get.rgb('RGB(100% 30% 90%)'), [255, 77, 229, 1]);
2424
assert.deepEqual(string.get.rgb('transparent'), [0, 0, 0, 0]);
25+
assert.deepEqual(string.get.rgb('blue'), [0, 0, 255, 1]);
26+
assert.deepEqual(string.get.rgb('BLUE'), [0, 0, 255, 1]);
2527
assert.deepEqual(string.get.hsl('hsl(240, 100%, 50.5%)'), [240, 100, 50.5, 1]);
26-
assert.deepEqual(string.get.hsl('hsl(240 100% 50.5%)'), [240, 100, 50.5, 1]);
28+
assert.deepEqual(string.get.hsl('HSL(240 100% 50.5%)'), [240, 100, 50.5, 1]);
2729
assert.deepEqual(string.get.hsl('hsl(240deg, 100%, 50.5%)'), [240, 100, 50.5, 1]);
28-
assert.deepEqual(string.get.hsl('hsl(240deg 100% 50.5%)'), [240, 100, 50.5, 1]);
30+
assert.deepEqual(string.get.hsl('hsl(240DEG 100% 50.5%)'), [240, 100, 50.5, 1]);
2931
assert.deepEqual(string.get.hwb('hwb(240, 100%, 50.5%)'), [240, 100, 50.5, 1]);
30-
assert.deepEqual(string.get.hwb('hwb(240deg, 100%, 50.5%)'), [240, 100, 50.5, 1]);
32+
assert.deepEqual(string.get.hwb('HWB(240DEG, 100%, 50.5%)'), [240, 100, 50.5, 1]);
3133
assert.deepEqual(string.get.hwb('hwb(12 50% 0%)'), [12, 50, 0, 1]);
3234

3335
// Generic .get()
@@ -46,6 +48,8 @@ assert.deepEqual(string.get('rgb(2.44E2 2.33e2 1.00E2)'), {model: 'rgb', value:
4648
assert.deepEqual(string.get('rgb(100%, 30%, 90%)'), {model: 'rgb', value: [255, 77, 229, 1]});
4749
assert.deepEqual(string.get('rgb(100% 30% 90%)'), {model: 'rgb', value: [255, 77, 229, 1]});
4850
assert.deepEqual(string.get('transparent'), {model: 'rgb', value: [0, 0, 0, 0]});
51+
assert.deepEqual(string.get('blue'), {model: 'rgb', value: [0, 0, 255, 1]});
52+
assert.deepEqual(string.get('BLUE'), {model: 'rgb', value: [0, 0, 255, 1]});
4953
assert.deepEqual(string.get('hsl(240, 100%, 50.5%)'), {model: 'hsl', value: [240, 100, 50.5, 1]});
5054
assert.deepEqual(string.get('hsl(-480, 100%, 50.5%)'), {model: 'hsl', value: [240, 100, 50.5, 1]});
5155
assert.deepEqual(string.get('hsl(240 100% 50.5%)'), {model: 'hsl', value: [240, 100, 50.5, 1]});

0 commit comments

Comments
 (0)