Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ cs.get = function (string) {
};

cs.get.rgb = function (string) {
if (!string) {
if (!isValidString(string)) {
return null;
}

Expand All @@ -61,6 +61,7 @@ cs.get.rgb = function (string) {
let i;
let hexAlpha;

string = normalizeSpace(string);
if (match = string.match(hex)) {
hexAlpha = match[2];
match = match[1];
Expand Down Expand Up @@ -128,12 +129,12 @@ cs.get.rgb = function (string) {
};

cs.get.hsl = function (string) {
if (!string) {
if (!isValidString(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);
const match = normalizeSpace(string).match(hsl);

if (match) {
const alpha = Number.parseFloat(match[4]);
Expand All @@ -149,12 +150,12 @@ cs.get.hsl = function (string) {
};

cs.get.hwb = function (string) {
if (!string) {
if (!isValidString(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*)?\)$/;
const match = string.match(hwb);
const match = normalizeSpace(string).match(hwb);

if (match) {
const alpha = Number.parseFloat(match[4]);
Expand Down Expand Up @@ -227,4 +228,12 @@ function hexDouble(number_) {
return (string_.length < 2) ? '0' + string_ : string_;
}

function isValidString(string) {
return typeof string === 'string' && string.length >= 4 && string.length <= 200;
}

function normalizeSpace(string) {
return string.split(/\s+/).join(' ').trim();
}

export default cs;