From 583e09fe3a0dbdcfe958cc1ed89e554ec3a100e0 Mon Sep 17 00:00:00 2001 From: alexandercerutti Date: Tue, 21 Aug 2018 14:57:01 +0200 Subject: [PATCH] Added isValidRGB function --- index.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/index.js b/index.js index de19a63..3f9c74b 100644 --- a/index.js +++ b/index.js @@ -503,6 +503,24 @@ class Pass { } } +/** + * Checks if an rgb value is compliant with CSS-like syntax + * + * @function isValidRGB + * @params {String} value - string to analyze + * @returns {Boolean} True if valid rgb, false otherwise + */ + +function isValidRGB(value) { + let rgb = value.match(/^rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)/); + + if (!rgb) { + return false; + } + + return rgb.slice(1,4).every(v => Math.abs(Number(v)) <= 255); +} + /** * Converts a date to W3C Standard format *