Skip to content

Commit

Permalink
changes strict to allowSpaces
Browse files Browse the repository at this point in the history
  • Loading branch information
a-h-i committed May 14, 2024
1 parent dbd5260 commit bfca688
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ Validator | Description
**isPort(str)** | check if the string is a valid port number.
**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`.
**isRFC3339(str)** | check if the string is a valid [RFC 3339][RFC 3339] date.
**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)`.
**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)`.
**isSemVer(str)** | check if the string is a Semantic Versioning Specification (SemVer).
**isSurrogatePair(str)** | check if the string contains any surrogate pairs chars.
**isUppercase(str)** | check if the string is uppercase.
Expand Down
10 changes: 5 additions & 5 deletions src/lib/isRgbColor.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ const startsWithRgb = /^rgba?/;

export default function isRgbColor(str, options) {
assertString(str);
// default options to true
let strict = true;
// default options to true for percent and false for spaces
let allowSpaces = false;
let includePercentValues = true;
if (typeof options !== 'object') {
if (arguments.length >= 2) {
includePercentValues = arguments[1];
}
} else {
strict = options.strict !== undefined ? options.strict : true;
allowSpaces = options.allowSpaces !== undefined ? options.allowSpaces : allowSpaces;
includePercentValues = options.includePercentValues !== undefined ?
options.includePercentValues : true;
options.includePercentValues : includePercentValues;
}

if (!strict) {
if (allowSpaces) {
// make sure it starts with continous rgba? without spaces before stripping
if (!startsWithRgb.test(str)) {
return false;
Expand Down
32 changes: 28 additions & 4 deletions test/validators.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4701,10 +4701,10 @@ describe('Validators', () => {
],
});

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

});

// test where strict is true as part of options object
// test where both are true as part of options object
test({
validator: 'isRgbColor',
args: [{ includePercentValues: true, strict: true }],
args: [{ includePercentValues: true, allowSpaces: true }],
valid: [
'rgb( 5, 5, 5)',
'rgba(5, 5, 5, .3)',
'rgb(0, 0, 0)',
'rgb(255, 255, 255)',
'rgba(0, 0, 0, 0)',
'rgba(255, 255, 255, 1)',
'rgba(255, 255, 255, .1)',
'rgba(255, 255, 255, 0.1)',
'rgb(5% ,5% ,5%)',
'rgba(5%,5%,5%, .3)',
],
invalid: [
'r g b( 0, 251, 222 )',
'rgb(4,4,5%)',
'rgb(101%,101%,101%)',

],
});

// test where allowSpaces is false as part of options object
test({
validator: 'isRgbColor',
args: [{ includePercentValues: true, allowSpaces: false }],
valid: [
'rgb(5,5,5)',
'rgba(5,5,5,.3)',
Expand Down

0 comments on commit bfca688

Please sign in to comment.