diff --git a/README.md b/README.md index 0889a5ae0..d97ae7a84 100644 --- a/README.md +++ b/README.md @@ -116,7 +116,7 @@ Validator | Description **isHalfWidth(str)** | check if the string contains any half-width chars. **isHash(str, algorithm)** | check if the string is a hash of type algorithm.

Algorithm is one of `['crc32', 'crc32b', 'md4', 'md5', 'ripemd128', 'ripemd160', 'sha1', 'sha256', 'sha384', 'sha512', 'tiger128', 'tiger160', 'tiger192']`. **isHexadecimal(str)** | check if the string is a hexadecimal number. -**isHexColor(str)** | check if the string is a hexadecimal color. +**isHexColor(str [, options])** | check if the string is a hexadecimal color.

`options` is an object that defaults to `{ require_hashtag: false }`.
Options:
`require_hashtag`: Enforce # prefix, default false. **isHSL(str)** | check if the string is an HSL (hue, saturation, lightness, optional alpha) color based on [CSS Colors Level 4 specification][CSS Colors Level 4 Specification].

Comma-separated format supported. Space-separated format supported with the exception of a few edge cases (ex: `hsl(200grad+.1%62%/1)`). **isIBAN(str, [, options])** | check if the string is an IBAN (International Bank Account Number).

`options` is an object which accepts two attributes: `whitelist`: where you can restrict IBAN codes you want to receive data from and `blacklist`: where you can remove some of the countries from the current list. For both you can use an array with the following values `['AD','AE','AL','AT','AZ','BA','BE','BG','BH','BR','BY','CH','CR','CY','CZ','DE','DK','DO','EE','EG','ES','FI','FO','FR','GB','GE','GI','GL','GR','GT','HR','HU','IE','IL','IQ','IR','IS','IT','JO','KW','KZ','LB','LC','LI','LT','LU','LV','MC','MD','ME','MK','MR','MT','MU','MZ','NL','NO','PK','PL','PS','PT','QA','RO','RS','SA','SC','SE','SI','SK','SM','SV','TL','TN','TR','UA','VA','VG','XK']`. **isIdentityCard(str [, locale])** | check if the string is a valid identity card code.

`locale` is one of `['LK', 'PL', 'ES', 'FI', 'IN', 'IT', 'IR', 'MZ', 'NO', 'TH', 'zh-TW', 'he-IL', 'ar-LY', 'ar-TN', 'zh-CN', 'zh-HK', 'PK']` OR `'any'`. If 'any' is used, function will check if any of the locales match.

Defaults to 'any'. diff --git a/src/lib/isHexColor.js b/src/lib/isHexColor.js index 21a037504..b7a2e5a23 100644 --- a/src/lib/isHexColor.js +++ b/src/lib/isHexColor.js @@ -1,8 +1,18 @@ import assertString from './util/assertString'; +import merge from './util/merge'; const hexcolor = /^#?([0-9A-F]{3}|[0-9A-F]{4}|[0-9A-F]{6}|[0-9A-F]{8})$/i; +const hexcolor_with_prefix = /^#([0-9A-F]{3}|[0-9A-F]{4}|[0-9A-F]{6}|[0-9A-F]{8})$/i; -export default function isHexColor(str) { +const default_is_hexcolor_options = { + require_hashtag: false, +}; + +export default function isHexColor(str, options) { assertString(str); - return hexcolor.test(str); + options = merge(options, default_is_hexcolor_options); + const hexcolor_regex = options.require_hashtag + ? hexcolor_with_prefix + : hexcolor; + return hexcolor_regex.test(str); } diff --git a/test/validators.test.js b/test/validators.test.js index e85d041b6..b5b8f32db 100644 --- a/test/validators.test.js +++ b/test/validators.test.js @@ -4711,6 +4711,48 @@ describe('Validators', () => { '#ff', 'fff0a', '#ff12FG', + '#######', + '', + ], + }); + test({ + validator: 'isHexColor', + args: [{ require_hashtag: false }], + valid: [ + '#ff0000ff', + '#ff0034', + '#CCCCCC', + '0f38', + 'fff', + '#f00', + ], + invalid: [ + '#ff', + 'fff0a', + '#ff12FG', + '#######', + '', + ], + }); + test({ + validator: 'isHexColor', + args: [{ require_hashtag: true }], + valid: [ + '#ff0000ff', + '#ff0034', + '#CCCCCC', + '#0f38', + '#fff', + '#f00', + ], + invalid: [ + '#ff', + 'fff0a', + '#ff12FG', + '0f38', + 'fff', + '#######', + '', ], }); });