Skip to content

Commit 6531047

Browse files
authored
fix(isHexColor): add require_hashtag option (#2535)
1 parent f605a9c commit 6531047

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ Validator | Description
116116
**isHalfWidth(str)** | check if the string contains any half-width chars.
117117
**isHash(str, algorithm)** | check if the string is a hash of type algorithm.<br/><br/>Algorithm is one of `['crc32', 'crc32b', 'md4', 'md5', 'ripemd128', 'ripemd160', 'sha1', 'sha256', 'sha384', 'sha512', 'tiger128', 'tiger160', 'tiger192']`.
118118
**isHexadecimal(str)** | check if the string is a hexadecimal number.
119-
**isHexColor(str)** | check if the string is a hexadecimal color.
119+
**isHexColor(str [, options])** | check if the string is a hexadecimal color. <br/><br/>`options` is an object that defaults to `{ require_hashtag: false }`.<br />Options: <br/> `require_hashtag`: Enforce # prefix, default false.
120120
**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].<br/><br/>Comma-separated format supported. Space-separated format supported with the exception of a few edge cases (ex: `hsl(200grad+.1%62%/1)`).
121121
**isIBAN(str, [, options])** | check if the string is an IBAN (International Bank Account Number).<br/><br/>`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']`.
122122
**isIdentityCard(str [, locale])** | check if the string is a valid identity card code.<br/><br/>`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.<br/><br/>Defaults to 'any'.

src/lib/isHexColor.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
import assertString from './util/assertString';
2+
import merge from './util/merge';
23

34
const hexcolor = /^#?([0-9A-F]{3}|[0-9A-F]{4}|[0-9A-F]{6}|[0-9A-F]{8})$/i;
5+
const hexcolor_with_prefix = /^#([0-9A-F]{3}|[0-9A-F]{4}|[0-9A-F]{6}|[0-9A-F]{8})$/i;
46

5-
export default function isHexColor(str) {
7+
const default_is_hexcolor_options = {
8+
require_hashtag: false,
9+
};
10+
11+
export default function isHexColor(str, options) {
612
assertString(str);
7-
return hexcolor.test(str);
13+
options = merge(options, default_is_hexcolor_options);
14+
const hexcolor_regex = options.require_hashtag
15+
? hexcolor_with_prefix
16+
: hexcolor;
17+
return hexcolor_regex.test(str);
818
}

test/validators.test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4997,6 +4997,48 @@ describe('Validators', () => {
49974997
'#ff',
49984998
'fff0a',
49994999
'#ff12FG',
5000+
'#######',
5001+
'',
5002+
],
5003+
});
5004+
test({
5005+
validator: 'isHexColor',
5006+
args: [{ require_hashtag: false }],
5007+
valid: [
5008+
'#ff0000ff',
5009+
'#ff0034',
5010+
'#CCCCCC',
5011+
'0f38',
5012+
'fff',
5013+
'#f00',
5014+
],
5015+
invalid: [
5016+
'#ff',
5017+
'fff0a',
5018+
'#ff12FG',
5019+
'#######',
5020+
'',
5021+
],
5022+
});
5023+
test({
5024+
validator: 'isHexColor',
5025+
args: [{ require_hashtag: true }],
5026+
valid: [
5027+
'#ff0000ff',
5028+
'#ff0034',
5029+
'#CCCCCC',
5030+
'#0f38',
5031+
'#fff',
5032+
'#f00',
5033+
],
5034+
invalid: [
5035+
'#ff',
5036+
'fff0a',
5037+
'#ff12FG',
5038+
'0f38',
5039+
'fff',
5040+
'#######',
5041+
'',
50005042
],
50015043
});
50025044
});

0 commit comments

Comments
 (0)