Skip to content

Commit

Permalink
implements optional options param for isRgbColor
Browse files Browse the repository at this point in the history
  • Loading branch information
a-h-i committed Aug 19, 2022
1 parent 154c5bf commit 301e707
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,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 locals match. Locale list is `validator.isPostalCodeLocales`.).
**isRFC3339(str)** | check if the string is a valid [RFC 3339](https://tools.ietf.org/html/rfc3339) date.
**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.
**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.
**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
21 changes: 20 additions & 1 deletion src/lib/isRgbColor.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable prefer-rest-params */
import assertString from './util/assertString';

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])\)$/;
Expand All @@ -6,9 +7,27 @@ const rgbColorPercent = /^rgb\((([0-9]%|[1-9][0-9]%|100%),){2}([0-9]%|[1-9][0-9]
const rgbaColorPercent = /^rgba\((([0-9]%|[1-9][0-9]%|100%),){3}(0?\.\d|1(\.0)?|0(\.0)?)\)/;
const startsWithRgb = /^rgba?/;

export default function isRgbColor(str, includePercentValues = true, strict = true) {
export default function isRgbColor(str, options) {
assertString(str);
// remove spaces
let strict, includePercentValues;
if (typeof options === 'object') {
strict = options.strict !== undefined ? options.strict : true;
includePercentValues = options.includePercentValues !== undefined ?
options.includePercentValues : true;
} else {
// backward compaitable behaviour
// Defaults
strict = true;
includePercentValues = true;
if (arguments.length >= 2) {
includePercentValues = arguments[1];
}
if (arguments.length >= 3) {
strict = arguments[2];
}
}

if (!strict) {
// make sure it starts with continous rgba? without spaces before stripping
if (!startsWithRgb.test(str)) {
Expand Down
94 changes: 94 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -4301,6 +4301,43 @@ describe('Validators', () => {
],
});

// test empty options object
test({
validator: 'isRgbColor',
args: [{}],
valid: [
'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: [
'rgb(0,0,0,)',
'rgb(0,0,)',
'rgb(0,0,256)',
'rgb()',
'rgba(0,0,0)',
'rgba(255,255,255,2)',
'rgba(255,255,255,.12)',
'rgba(255,255,256,0.1)',
'rgb(4,4,5%)',
'rgba(5%,5%,5%)',
'rgba(3,3,3%,.3)',
'rgb(101%,101%,101%)',
'rgba(3%,3%,101%,0.3)',
'r g b( 0, 251, 222 )',
'r g ba( 0, 251, 222 )',
'rg ba(0, 251, 22, 0.5)',
'rgb( 255,255 ,255)',
'rgba(255, 255, 255, 0.5)',
'rgba(255, 255, 255, 0.5)',
'rgb(5%, 5%, 5%)',
],
});
// test where includePercentValues is given as false
test({
validator: 'isRgbColor',
Expand All @@ -4317,6 +4354,22 @@ describe('Validators', () => {
],
});

// test where includePercentValues is given as false as part of options object
test({
validator: 'isRgbColor',
args: [{ includePercentValues: false }],
valid: [
'rgb(5,5,5)',
'rgba(5,5,5,.3)',
],
invalid: [
'rgb(4,4,5%)',
'rgba(5%,5%,5%)',
'r g b( 0, 251, 222 )',
'r g ba( 0, 251, 222 )',
],
});

// test where strict is false
test({
validator: 'isRgbColor',
Expand Down Expand Up @@ -4357,6 +4410,47 @@ describe('Validators', () => {
'rgba(3%,3%,101%,0.3)',
],
});

// test where strict is false as part of options object
test({
validator: 'isRgbColor',
args: [{ includePercentValues: true, strict: false }],
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)',
'rgb( 255,255 ,255)',
'rgba(255, 255, 255, 0.5)',
'rgb(5%, 5%, 5%)',
'rgba(255, 255, 255, 0.5)',
],
invalid: [
'rgb(4,4,5%)',
'rgba(5%,5%,5%)',
'r g b( 0, 251, 222 )',
'r g ba( 0, 251, 222 )',
'rgb(0,0,0,)',
'rgb(0,0,)',
'rgb(0,0,256)',
'rgb()',
'rgba(0,0,0)',
'rgba(255,255,255,2)',
'rgba(255,255,255,.12)',
'rgba(255,255,256,0.1)',
'rgb(4,4,5%)',
'rgba(5%,5%,5%)',
'rgba(3,3,3%,.3)',
'rgb(101%,101%,101%)',
'rgba(3%,3%,101%,0.3)',
],
});
});

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

0 comments on commit 301e707

Please sign in to comment.