Skip to content

Commit

Permalink
fix(is-valid-rgb): bring the behavior in line with isValidRGBA (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomaash committed Aug 9, 2019
1 parent 48ae980 commit 5dc3adb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/util.ts
Expand Up @@ -14,6 +14,7 @@ const ASPDateRegex = /^\/?Date\((-?\d+)/i
// Color REs
const fullHexRE = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i
const shortHexRE = /^#?([a-f\d])([a-f\d])([a-f\d])$/i
const rgbRE = /^rgb\( *(1?\d{1,2}|2[0-4]\d|25[0-5]) *, *(1?\d{1,2}|2[0-4]\d|25[0-5]) *, *(1?\d{1,2}|2[0-4]\d|25[0-5]) *\)$/i
const rgbaRE = /^rgba\( *(1?\d{1,2}|2[0-4]\d|25[0-5]) *, *(1?\d{1,2}|2[0-4]\d|25[0-5]) *, *(1?\d{1,2}|2[0-4]\d|25[0-5]) *, *([01]|0?\.\d+) *\)$/i

/**
Expand Down Expand Up @@ -1432,7 +1433,7 @@ export function isValidHex(hex: string): boolean {
* @returns True if the string is valid, false otherwise.
*/
export function isValidRGB(rgb: string): boolean {
return /^ *rgb\( *\d{1,3} *, *\d{1,3} *, *\d{1,3} *\) *$/i.test(rgb)
return rgbRE.test(rgb)
}

/**
Expand Down
15 changes: 12 additions & 3 deletions test/is-valid-rgb.test.ts
Expand Up @@ -4,34 +4,43 @@ import { isValidRGB } from '../src'

describe('isValidRGB', function(): void {
const valid = [
' Rgb(0,12,123) ',
' rGb(210, 50,220)',
'RGB(7, 200, 8)',
'RGb(255,255,255)',
'Rgb(0,12,123)',
'rGB(44, 7, 220)',
'rGb(210, 50,220) ',
'rGb(210, 50,220)',
'rgB(210,50, 220)',
'rgb( 72 , 11 , 123 )',
'rgb(0,0,0)',
]
const invalid = [
' ',
' Rgb(0,12,123) ',
' rGb(210, 50,220)',
'#000000',
'#abc',
'',
'(0,12,123)',
'0',
'0,12,123)',
'5,7,9',
'RGB(-1, 0, 0)',
'RGB(0, -1, 0)',
'RGB(0, 0, -1)',
'RGB(255, 255, 256)',
'RGB(255, 256, 255)',
'RGB(256, 255, 255)',
'RGBA(123, 147, 95, 1)',
'false',
'garbage',
'hi rgb(0,12,123)',
'orange',
'rGb(210, 50,220) ',
'rgb 7, 7, 7',
'rgb(0, 12, 123) :-)',
'rgb(0,12,123',
'rgb(4 4, 7, 220)',
'rgb(44, 7, 2 2 0)',
'rgba(7,8,9,0.3)',
'the color is #00AAAA',
'true',
Expand Down

0 comments on commit 5dc3adb

Please sign in to comment.