Skip to content

Commit 301e707

Browse files
committed
implements optional options param for isRgbColor
1 parent 154c5bf commit 301e707

File tree

3 files changed

+115
-2
lines changed

3 files changed

+115
-2
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ Validator | Description
158158
**isPort(str)** | check if the string is a valid port number.
159159
**isPostalCode(str, locale)** | check if the string is a postal code,<br/><br/>(locale is one of `[ 'AD', 'AT', 'AU', 'AZ', 'BA', 'BE', 'BG', 'BR', 'BY', 'CA', 'CH', 'CN', 'CZ', 'DE', 'DK', 'DO', 'DZ', 'EE', 'ES', 'FI', 'FR', 'GB', 'GR', 'HR', 'HT', 'HU', 'ID', 'IE' 'IL', 'IN', 'IR', 'IS', 'IT', 'JP', 'KE', 'KR', 'LI', 'LK', 'LT', 'LU', 'LV', 'MG', 'MT', 'MX', 'MY', 'NL', 'NO', 'NP', 'NZ', 'PL', 'PR', 'PT', 'RO', 'RU', 'SA', 'SE', 'SG', 'SI', 'SK', 'TH', 'TN', 'TW', 'UA', 'US', 'ZA', 'ZM' ]` OR 'any'. If 'any' is used, function will check if any of the locals match. Locale list is `validator.isPostalCodeLocales`.).
160160
**isRFC3339(str)** | check if the string is a valid [RFC 3339](https://tools.ietf.org/html/rfc3339) date.
161-
**isRgbColor(str [, includePercentValues, strict])** | check if the string is a rgb or rgba color.<br/><br/>`includePercentValues` defaults to `true`. If you don't want to allow to set `rgb` or `rgba` values with percents, like `rgb(5%,5%,5%)`, or `rgba(90%,90%,90%,.3)`, then set it to false.<br/><br/>`strict` defaults to true, if set to false ignores spaces between params i.e rgb(255, 255, 255) would pass with strict = false but fails if true.
161+
**isRgbColor(str [, includePercentValues, strict])** | check if the string is a rgb or rgba color.<br/><br/>`includePercentValues` defaults to `true`. If you don't want to allow to set `rgb` or `rgba` values with percents, like `rgb(5%,5%,5%)`, or `rgba(90%,90%,90%,.3)`, then set it to false.<br/><br/>`strict` defaults to true, if set to false ignores spaces between params i.e rgb(255, 255, 255) would pass with strict = false but fails if true.<br/><br/>Alternatively an options argument can be passed like so `isRgbColor(str, [options])` where options has `strict` and `includePercentValues` properties with the same semantics as above.
162162
**isSemVer(str)** | check if the string is a Semantic Versioning Specification (SemVer).
163163
**isSurrogatePair(str)** | check if the string contains any surrogate pairs chars.
164164
**isUppercase(str)** | check if the string is uppercase.

src/lib/isRgbColor.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable prefer-rest-params */
12
import assertString from './util/assertString';
23

34
const rgbColor = /^rgb\((([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]),){2}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\)$/;
@@ -6,9 +7,27 @@ const rgbColorPercent = /^rgb\((([0-9]%|[1-9][0-9]%|100%),){2}([0-9]%|[1-9][0-9]
67
const rgbaColorPercent = /^rgba\((([0-9]%|[1-9][0-9]%|100%),){3}(0?\.\d|1(\.0)?|0(\.0)?)\)/;
78
const startsWithRgb = /^rgba?/;
89

9-
export default function isRgbColor(str, includePercentValues = true, strict = true) {
10+
export default function isRgbColor(str, options) {
1011
assertString(str);
1112
// remove spaces
13+
let strict, includePercentValues;
14+
if (typeof options === 'object') {
15+
strict = options.strict !== undefined ? options.strict : true;
16+
includePercentValues = options.includePercentValues !== undefined ?
17+
options.includePercentValues : true;
18+
} else {
19+
// backward compaitable behaviour
20+
// Defaults
21+
strict = true;
22+
includePercentValues = true;
23+
if (arguments.length >= 2) {
24+
includePercentValues = arguments[1];
25+
}
26+
if (arguments.length >= 3) {
27+
strict = arguments[2];
28+
}
29+
}
30+
1231
if (!strict) {
1332
// make sure it starts with continous rgba? without spaces before stripping
1433
if (!startsWithRgb.test(str)) {

test/validators.js

+94
Original file line numberDiff line numberDiff line change
@@ -4301,6 +4301,43 @@ describe('Validators', () => {
43014301
],
43024302
});
43034303

4304+
// test empty options object
4305+
test({
4306+
validator: 'isRgbColor',
4307+
args: [{}],
4308+
valid: [
4309+
'rgb(0,0,0)',
4310+
'rgb(255,255,255)',
4311+
'rgba(0,0,0,0)',
4312+
'rgba(255,255,255,1)',
4313+
'rgba(255,255,255,.1)',
4314+
'rgba(255,255,255,0.1)',
4315+
'rgb(5%,5%,5%)',
4316+
'rgba(5%,5%,5%,.3)',
4317+
],
4318+
invalid: [
4319+
'rgb(0,0,0,)',
4320+
'rgb(0,0,)',
4321+
'rgb(0,0,256)',
4322+
'rgb()',
4323+
'rgba(0,0,0)',
4324+
'rgba(255,255,255,2)',
4325+
'rgba(255,255,255,.12)',
4326+
'rgba(255,255,256,0.1)',
4327+
'rgb(4,4,5%)',
4328+
'rgba(5%,5%,5%)',
4329+
'rgba(3,3,3%,.3)',
4330+
'rgb(101%,101%,101%)',
4331+
'rgba(3%,3%,101%,0.3)',
4332+
'r g b( 0, 251, 222 )',
4333+
'r g ba( 0, 251, 222 )',
4334+
'rg ba(0, 251, 22, 0.5)',
4335+
'rgb( 255,255 ,255)',
4336+
'rgba(255, 255, 255, 0.5)',
4337+
'rgba(255, 255, 255, 0.5)',
4338+
'rgb(5%, 5%, 5%)',
4339+
],
4340+
});
43044341
// test where includePercentValues is given as false
43054342
test({
43064343
validator: 'isRgbColor',
@@ -4317,6 +4354,22 @@ describe('Validators', () => {
43174354
],
43184355
});
43194356

4357+
// test where includePercentValues is given as false as part of options object
4358+
test({
4359+
validator: 'isRgbColor',
4360+
args: [{ includePercentValues: false }],
4361+
valid: [
4362+
'rgb(5,5,5)',
4363+
'rgba(5,5,5,.3)',
4364+
],
4365+
invalid: [
4366+
'rgb(4,4,5%)',
4367+
'rgba(5%,5%,5%)',
4368+
'r g b( 0, 251, 222 )',
4369+
'r g ba( 0, 251, 222 )',
4370+
],
4371+
});
4372+
43204373
// test where strict is false
43214374
test({
43224375
validator: 'isRgbColor',
@@ -4357,6 +4410,47 @@ describe('Validators', () => {
43574410
'rgba(3%,3%,101%,0.3)',
43584411
],
43594412
});
4413+
4414+
// test where strict is false as part of options object
4415+
test({
4416+
validator: 'isRgbColor',
4417+
args: [{ includePercentValues: true, strict: false }],
4418+
valid: [
4419+
'rgb(5,5,5)',
4420+
'rgba(5,5,5,.3)',
4421+
'rgb(0,0,0)',
4422+
'rgb(255,255,255)',
4423+
'rgba(0,0,0,0)',
4424+
'rgba(255,255,255,1)',
4425+
'rgba(255,255,255,.1)',
4426+
'rgba(255,255,255,0.1)',
4427+
'rgb(5%,5%,5%)',
4428+
'rgba(5%,5%,5%,.3)',
4429+
'rgb( 255,255 ,255)',
4430+
'rgba(255, 255, 255, 0.5)',
4431+
'rgb(5%, 5%, 5%)',
4432+
'rgba(255, 255, 255, 0.5)',
4433+
],
4434+
invalid: [
4435+
'rgb(4,4,5%)',
4436+
'rgba(5%,5%,5%)',
4437+
'r g b( 0, 251, 222 )',
4438+
'r g ba( 0, 251, 222 )',
4439+
'rgb(0,0,0,)',
4440+
'rgb(0,0,)',
4441+
'rgb(0,0,256)',
4442+
'rgb()',
4443+
'rgba(0,0,0)',
4444+
'rgba(255,255,255,2)',
4445+
'rgba(255,255,255,.12)',
4446+
'rgba(255,255,256,0.1)',
4447+
'rgb(4,4,5%)',
4448+
'rgba(5%,5%,5%)',
4449+
'rgba(3,3,3%,.3)',
4450+
'rgb(101%,101%,101%)',
4451+
'rgba(3%,3%,101%,0.3)',
4452+
],
4453+
});
43604454
});
43614455

43624456
it('should validate ISRC code strings', () => {

0 commit comments

Comments
 (0)