Merge pull request #46 from rastapasta/color-bug

Bug fix: utils.hex2rgb does not fail on invalid hex strings
pull/49/head
Christian Paul 2018-10-18 19:58:29 -07:00 zatwierdzone przez GitHub
commit 712898aa97
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 4AEE18F83AFDEB23
2 zmienionych plików z 30 dodań i 2 usunięć

Wyświetl plik

@ -59,8 +59,8 @@ const utils = {
hex2rgb: (color) => {
if (typeof color !== 'string') return [255, 0, 0];
if (/^#[a-fA-F0-9]{3,6}$/.test()) {
throw new Error('#{color} isn\'t a supported hex color');
if (!/^#[a-fA-F0-9]{3,6}$/.test(color)) {
throw new Error(`${color} isn't a supported hex color`);
}
color = color.substr(1);

28
src/utils.spec.js 100644
Wyświetl plik

@ -0,0 +1,28 @@
'use strict';
const utils = require('./utils');
describe('utils', () => {
describe('hex2rgb', () => {
test('#ff0000', () => {
expect(utils.hex2rgb('#ff0000')).toEqual([255,0,0]);
});
test('#ffff00', () => {
expect(utils.hex2rgb('#ffff00')).toEqual([255,255,0]);
});
test('#0000ff', () => {
expect(utils.hex2rgb('#0000ff')).toEqual([0,0,255]);
});
test('#112233', () => {
expect(utils.hex2rgb('#112233')).toEqual([17,34,51]);
});
test('#888', () => {
expect(utils.hex2rgb('#888')).toEqual([136,136,136]);
});
test('33', () => {
function wrapper() {
utils.hex2rgb('33');
}
expect(wrapper).toThrowError('isn\'t a supported hex color');
});
});
});