Skip to content

Commit 97c1f70

Browse files
committed
changes strict to allowSpaces
1 parent 5ed5b36 commit 97c1f70

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
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 [,options])** | check if the string is a rgb or rgba color.<br/></br>`options` is an object with the following properties<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`, which prohibits whitespace. If set to false, whitespace between color values is allowed, such as `rgb(255, 255, 255)` or even `rgba(255, 128, 0, 0.7)`.
161+
**isRgbColor(str [,options])** | check if the string is a rgb or rgba color.<br/></br>`options` is an object with the following properties<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/>`allowSpaces` defaults to `true`, which prohibits whitespace. If set to false, whitespace between color values is allowed, such as `rgb(255, 255, 255)` or even `rgba(255, 128, 0, 0.7)`.
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

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@ const startsWithRgb = /^rgba?/;
99

1010
export default function isRgbColor(str, options) {
1111
assertString(str);
12-
// default options to true
13-
let strict = true;
12+
// default options to true for percent and false for spaces
13+
let allowSpaces = false;
1414
let includePercentValues = true;
1515
if (typeof options !== 'object') {
1616
if (arguments.length >= 2) {
1717
includePercentValues = arguments[1];
1818
}
1919
} else {
20-
strict = options.strict !== undefined ? options.strict : true;
20+
allowSpaces = options.allowSpaces !== undefined ? options.allowSpaces : allowSpaces;
2121
includePercentValues = options.includePercentValues !== undefined ?
22-
options.includePercentValues : true;
22+
options.includePercentValues : includePercentValues;
2323
}
2424

25-
if (!strict) {
25+
if (allowSpaces) {
2626
// make sure it starts with continous rgba? without spaces before stripping
2727
if (!startsWithRgb.test(str)) {
2828
return false;

test/validators.js

+28-4
Original file line numberDiff line numberDiff line change
@@ -4412,10 +4412,10 @@ describe('Validators', () => {
44124412
],
44134413
});
44144414

4415-
// test where percent value and strict are false as part of options object
4415+
// test where percent value is false and allowSpaces is true as part of options object
44164416
test({
44174417
validator: 'isRgbColor',
4418-
args: [{ includePercentValues: false, strict: false }],
4418+
args: [{ includePercentValues: false, allowSpaces: true }],
44194419
valid: [
44204420
'rgb(5,5,5)',
44214421
'rgba(5,5,5,.3)',
@@ -4439,10 +4439,34 @@ describe('Validators', () => {
44394439

44404440
});
44414441

4442-
// test where strict is true as part of options object
4442+
// test where both are true as part of options object
44434443
test({
44444444
validator: 'isRgbColor',
4445-
args: [{ includePercentValues: true, strict: true }],
4445+
args: [{ includePercentValues: true, allowSpaces: true }],
4446+
valid: [
4447+
'rgb( 5, 5, 5)',
4448+
'rgba(5, 5, 5, .3)',
4449+
'rgb(0, 0, 0)',
4450+
'rgb(255, 255, 255)',
4451+
'rgba(0, 0, 0, 0)',
4452+
'rgba(255, 255, 255, 1)',
4453+
'rgba(255, 255, 255, .1)',
4454+
'rgba(255, 255, 255, 0.1)',
4455+
'rgb(5% ,5% ,5%)',
4456+
'rgba(5%,5%,5%, .3)',
4457+
],
4458+
invalid: [
4459+
'r g b( 0, 251, 222 )',
4460+
'rgb(4,4,5%)',
4461+
'rgb(101%,101%,101%)',
4462+
4463+
],
4464+
});
4465+
4466+
// test where allowSpaces is false as part of options object
4467+
test({
4468+
validator: 'isRgbColor',
4469+
args: [{ includePercentValues: true, allowSpaces: false }],
44464470
valid: [
44474471
'rgb(5,5,5)',
44484472
'rgba(5,5,5,.3)',

0 commit comments

Comments
 (0)