Skip to content

Commit 86a9315

Browse files
committed
Normalize string before checking it
1 parent 5afeccc commit 86a9315

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

index.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ const cs = {
1414
get: {},
1515
};
1616

17-
cs.get = function (string) {
18-
const prefix = string.slice(0, 3).toLowerCase();
17+
cs.get = function (string = '') {
18+
string = normalizeString(string);
19+
const prefix = string.slice(0, 3);
1920
let value;
2021
let model;
2122
switch (prefix) {
@@ -45,10 +46,8 @@ cs.get = function (string) {
4546
return {model, value};
4647
};
4748

48-
cs.get.rgb = function (string) {
49-
if (!string) {
50-
return null;
51-
}
49+
cs.get.rgb = function (string = '') {
50+
string = normalizeString(string);
5251

5352
const abbr = /^#([a-f\d]{3,4})$/i;
5453
const hex = /^#([a-f\d]{6})([a-f\d]{2})?$/i;
@@ -127,10 +126,8 @@ cs.get.rgb = function (string) {
127126
return rgb;
128127
};
129128

130-
cs.get.hsl = function (string) {
131-
if (!string) {
132-
return null;
133-
}
129+
cs.get.hsl = function (string = '') {
130+
string = normalizeString(string);
134131

135132
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*)?\)$/;
136133
const match = string.match(hsl);
@@ -148,10 +145,8 @@ cs.get.hsl = function (string) {
148145
return null;
149146
};
150147

151-
cs.get.hwb = function (string) {
152-
if (!string) {
153-
return null;
154-
}
148+
cs.get.hwb = function (string = '') {
149+
string = normalizeString(string);
155150

156151
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*)?\)$/;
157152
const match = string.match(hwb);
@@ -227,4 +222,10 @@ function hexDouble(number_) {
227222
return (string_.length < 2) ? '0' + string_ : string_;
228223
}
229224

225+
function normalizeString(string) {
226+
string = string.toLowerCase();
227+
228+
return string;
229+
}
230+
230231
export default cs;

0 commit comments

Comments
 (0)