Skip to content

Commit bfca688

Browse files
committed
changes strict to allowSpaces
1 parent dbd5260 commit bfca688

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ Validator | Description
152152
**isPort(str)** | check if the string is a valid port number.
153153
**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 locales match. Locale list is `validator.isPostalCodeLocales`.
154154
**isRFC3339(str)** | check if the string is a valid [RFC 3339][RFC 3339] date.
155-
**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)`.
155+
**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)`.
156156
**isSemVer(str)** | check if the string is a Semantic Versioning Specification (SemVer).
157157
**isSurrogatePair(str)** | check if the string contains any surrogate pairs chars.
158158
**isUppercase(str)** | check if the string is uppercase.

src/lib/isRgbColor.js

Lines changed: 5 additions & 5 deletions
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.test.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4701,10 +4701,10 @@ describe('Validators', () => {
47014701
],
47024702
});
47034703

4704-
// test where percent value and strict are false as part of options object
4704+
// test where percent value is false and allowSpaces is true as part of options object
47054705
test({
47064706
validator: 'isRgbColor',
4707-
args: [{ includePercentValues: false, strict: false }],
4707+
args: [{ includePercentValues: false, allowSpaces: true }],
47084708
valid: [
47094709
'rgb(5,5,5)',
47104710
'rgba(5,5,5,.3)',
@@ -4728,10 +4728,34 @@ describe('Validators', () => {
47284728

47294729
});
47304730

4731-
// test where strict is true as part of options object
4731+
// test where both are true as part of options object
47324732
test({
47334733
validator: 'isRgbColor',
4734-
args: [{ includePercentValues: true, strict: true }],
4734+
args: [{ includePercentValues: true, allowSpaces: true }],
4735+
valid: [
4736+
'rgb( 5, 5, 5)',
4737+
'rgba(5, 5, 5, .3)',
4738+
'rgb(0, 0, 0)',
4739+
'rgb(255, 255, 255)',
4740+
'rgba(0, 0, 0, 0)',
4741+
'rgba(255, 255, 255, 1)',
4742+
'rgba(255, 255, 255, .1)',
4743+
'rgba(255, 255, 255, 0.1)',
4744+
'rgb(5% ,5% ,5%)',
4745+
'rgba(5%,5%,5%, .3)',
4746+
],
4747+
invalid: [
4748+
'r g b( 0, 251, 222 )',
4749+
'rgb(4,4,5%)',
4750+
'rgb(101%,101%,101%)',
4751+
4752+
],
4753+
});
4754+
4755+
// test where allowSpaces is false as part of options object
4756+
test({
4757+
validator: 'isRgbColor',
4758+
args: [{ includePercentValues: true, allowSpaces: false }],
47354759
valid: [
47364760
'rgb(5,5,5)',
47374761
'rgba(5,5,5,.3)',

0 commit comments

Comments
 (0)