Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add isRgbColor validator #1141

Merged
merged 3 commits into from
Feb 13, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,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 @@ -73,6 +73,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 @@ -205,6 +207,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)?)\)$/;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work on this implementation.
But, have 1 request, rgb, percentage based.
E.g rgb(0%,0%,0%), I think it is a valid rgb value, which is not covered in this PR.
Here is a reference material https://www.december.com/html/spec/colorrgbaper.html

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, I'll try to adjust

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ezkemboi I have doubts how to tackle the percentage based values. On one hand - it's valid rgb, on the other - e.g. in React Native it's not supported. Would you agree for some kind of optional flag, like includePercentValues that would be by default set to true, but will enable to set percentages as invalid value?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I totally agree.

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 @@ -2599,6 +2599,37 @@ describe('Validators', () => {
});
});

it('should validate rgb color strings', () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Run npm test. It will generate files such as validator.js, validator.min.js and files in lib/isRgbColor.js.
I think it might be needed. Not sure, but @profnandaa can confirm.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay, I wasn't sure where these files came from, so didn't commit them

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 @@ -1000,6 +1000,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 @@ -2142,6 +2157,7 @@ var validator = {
isOctal: isOctal,
isDivisibleBy: isDivisibleBy,
isHexColor: isHexColor,
isRgbColor: isRgbColor,
isISRC: isISRC,
isMD5: isMD5,
isHash: isHash,
Expand Down
8 changes: 4 additions & 4 deletions validator.min.js

Large diffs are not rendered by default.