Skip to content
Open
Show file tree
Hide file tree
Changes from all 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 src/lib/isHexColor.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const default_is_hexcolor_options = {
export default function isHexColor(str, options) {
assertString(str);
options = merge(options, default_is_hexcolor_options);

const hexcolor_regex = options.require_hashtag
? hexcolor_with_prefix
: hexcolor;
Expand Down
3 changes: 3 additions & 0 deletions src/lib/util/merge.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export default function merge(obj = { }, defaults) {
if (typeof obj !== 'object' || obj === null) {
obj = {};
}
for (const key in defaults) {
if (typeof obj[key] === 'undefined') {
obj[key] = defaults[key];
Expand Down
15 changes: 15 additions & 0 deletions test/validators.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import assert from 'assert';
import fs from 'fs';
import timezone_mock from 'timezone-mock';
import vm from 'vm';
import validator from '../index';
import test from './testFunctions';

let validator_js = fs.readFileSync(require.resolve('../validator.js')).toString();
Expand Down Expand Up @@ -5041,6 +5042,20 @@ describe('Validators', () => {
'',
],
});
test({
validator: 'isHexColor',
args: [null],
valid: ['#fff', '#000000', '123'],
invalid: ['not-a-color'],
});
test({
validator: 'isHexColor',
args: [123],
valid: ['#fff', '#000000', '123', 'abc'],
invalid: ['gray', 'not-a-color'],
});
const validColors = ['#ff0034', '#CCCCCC'].filter(validator.isHexColor);
assert.strictEqual(validColors.length, 2);
});

it('should validate HSL color strings', () => {
Expand Down
Loading