Skip to content

Commit

Permalink
Add isRgbColor validator
Browse files Browse the repository at this point in the history
  • Loading branch information
pawelkleczek10c committed Oct 28, 2019
1 parent b5a1d1f commit ba5326e
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ Validator | Description
**isHash(str, algorithm)** | check if the string is a hash of type algorithm.<br/><br/>Algorithm is one of `['md4', 'md5', 'sha1', 'sha256', 'sha384', 'sha512', 'ripemd128', 'ripemd160', 'tiger128', 'tiger160', 'tiger192', 'crc32', 'crc32b']`
**isHexadecimal(str)** | check if the string is a hexadecimal number.
**isHexColor(str)** | check if the string is a hexadecimal color.
**isRgbColor(str, [, includePercentValues])** | 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.
**isIdentityCard(str [, locale])** | check if the string is a valid identity card code.<br/><br/>`locale` is one of `['ES', 'zh-TW', 'he-IL']` OR `'any'`. If 'any' is used, function will check if any of the locals match.<br/><br/>Defaults to 'any'.
**isIn(str, values)** | check if the string is in a array of allowed values.
**isInt(str [, options])** | check if the string is an integer.<br/><br/>`options` is an object which can contain the keys `min` and/or `max` to check the integer is within boundaries (e.g. `{ min: 10, max: 99 }`). `options` can also contain the key `allow_leading_zeroes`, which when set to false will disallow integer values with leading zeroes (e.g. `{ allow_leading_zeroes: false }`). Finally, `options` can contain the keys `gt` and/or `lt` which will enforce integers being greater than or less than, respectively, the value provided (e.g. `{gt: 1, lt: 4}` for a number between 1 and 4).
Expand Down
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ var _isDivisibleBy = _interopRequireDefault(require("./lib/isDivisibleBy"));

var _isHexColor = _interopRequireDefault(require("./lib/isHexColor"));

var _isRgbColor = _interopRequireDefault(require("./lib/isRgbColor"));

var _isISRC = _interopRequireDefault(require("./lib/isISRC"));

var _isBIC = _interopRequireDefault(require("./lib/isBIC"));
Expand Down Expand Up @@ -203,6 +205,7 @@ var validator = {
isOctal: _isOctal.default,
isDivisibleBy: _isDivisibleBy.default,
isHexColor: _isHexColor.default,
isRgbColor: _isRgbColor.default,
isISRC: _isISRC.default,
isMD5: _isMD.default,
isHash: _isHash.default,
Expand Down
29 changes: 29 additions & 0 deletions lib/isRgbColor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = isRgbColor;

var _assertString = _interopRequireDefault(require("./util/assertString"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var 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])\)$/;
var rgbaColor = /^rgba\((([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]),){3}(0?\.\d|1(\.0)?|0(\.0)?)\)$/;
var rgbColorPercent = /^rgb\((([0-9]%|[1-9][0-9]%|100%),){2}([0-9]%|[1-9][0-9]%|100%)\)/;
var rgbaColorPercent = /^rgba\((([0-9]%|[1-9][0-9]%|100%),){3}(0?\.\d|1(\.0)?|0(\.0)?)\)/;

function isRgbColor(str) {
var includePercentValues = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
(0, _assertString.default)(str);

if (!includePercentValues) {
return rgbColor.test(str) || rgbaColor.test(str);
}

return rgbColor.test(str) || rgbaColor.test(str) || rgbColorPercent.test(str) || rgbaColorPercent.test(str);
}

module.exports = exports.default;
module.exports.default = exports.default;
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import isOctal from './lib/isOctal';
import isDivisibleBy from './lib/isDivisibleBy';

import isHexColor from './lib/isHexColor';
import isRgbColor from './lib/isRgbColor';

import isISRC from './lib/isISRC';

Expand Down Expand Up @@ -141,6 +142,7 @@ const validator = {
isOctal,
isDivisibleBy,
isHexColor,
isRgbColor,
isISRC,
isMD5,
isHash,
Expand Down
19 changes: 19 additions & 0 deletions src/lib/isRgbColor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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])\)$/;
const rgbaColor = /^rgba\((([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]),){3}(0?\.\d|1(\.0)?|0(\.0)?)\)$/;
const rgbColorPercent = /^rgb\((([0-9]%|[1-9][0-9]%|100%),){2}([0-9]%|[1-9][0-9]%|100%)\)/;
const rgbaColorPercent = /^rgba\((([0-9]%|[1-9][0-9]%|100%),){3}(0?\.\d|1(\.0)?|0(\.0)?)\)/;

export default function isRgbColor(str, includePercentValues = true) {
assertString(str);

if (!includePercentValues) {
return rgbColor.test(str) || rgbaColor.test(str);
}

return rgbColor.test(str) ||
rgbaColor.test(str) ||
rgbColorPercent.test(str) ||
rgbaColorPercent.test(str);
}
31 changes: 31 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -2551,6 +2551,37 @@ describe('Validators', () => {
});
});

it('should validate rgb color strings', () => {
test({
validator: 'isRgbColor',
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)',
],
});
});

it('should validate ISRC code strings', () => {
test({
validator: 'isISRC',
Expand Down
16 changes: 16 additions & 0 deletions validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -948,6 +948,21 @@ function isHexColor(str) {
return hexcolor.test(str);
}

var 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])\)$/;
var rgbaColor = /^rgba\((([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]),){3}(0?\.\d|1(\.0)?|0(\.0)?)\)$/;
var rgbColorPercent = /^rgb\((([0-9]%|[1-9][0-9]%|100%),){2}([0-9]%|[1-9][0-9]%|100%)\)/;
var rgbaColorPercent = /^rgba\((([0-9]%|[1-9][0-9]%|100%),){3}(0?\.\d|1(\.0)?|0(\.0)?)\)/;
function isRgbColor(str) {
var includePercentValues = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
assertString(str);

if (!includePercentValues) {
return rgbColor.test(str) || rgbaColor.test(str);
}

return rgbColor.test(str) || rgbaColor.test(str) || rgbColorPercent.test(str) || rgbaColorPercent.test(str);
}

var isrc = /^[A-Z]{2}[0-9A-Z]{3}\d{2}\d{5}$/;
function isISRC(str) {
assertString(str);
Expand Down Expand Up @@ -2088,6 +2103,7 @@ var validator = {
isOctal: isOctal,
isDivisibleBy: isDivisibleBy,
isHexColor: isHexColor,
isRgbColor: isRgbColor,
isISRC: isISRC,
isMD5: isMD5,
isHash: isHash,
Expand Down
2 changes: 1 addition & 1 deletion validator.min.js

Large diffs are not rendered by default.

0 comments on commit ba5326e

Please sign in to comment.