Skip to content

Commit 7e59665

Browse files
committed
refactor: add error handling
1 parent 3128972 commit 7e59665

File tree

3 files changed

+50
-19
lines changed

3 files changed

+50
-19
lines changed

js/src/utilities/hex-to-rgb.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,27 @@
77

88
/* eslint-disable no-magic-numbers */
99
const hexToRgb = (color) => {
10-
const hex = color.replace('#', '')
11-
const r = parseInt(hex.substring(0, 2), 16)
12-
const g = parseInt(hex.substring(2, 4), 16)
13-
const b = parseInt(hex.substring(4, 6), 16)
10+
if (typeof color === 'undefined') {
11+
throw new Error('Hex color is not defined')
12+
}
13+
const hex = color.match(/^#(?:[0-9a-f]{3}){1,2}$/i)
14+
if (!hex) {
15+
throw new Error(`${color} is not a valid hex color`)
16+
}
17+
let r
18+
let g
19+
let b
20+
if (color.length === 7) {
21+
r = parseInt(color.substring(1, 3), 16)
22+
g = parseInt(color.substring(3, 5), 16)
23+
b = parseInt(color.substring(5, 7), 16)
24+
} else {
25+
r = parseInt(color.substring(1, 2), 16)
26+
g = parseInt(color.substring(2, 3), 16)
27+
b = parseInt(color.substring(3, 5), 16)
28+
}
1429

15-
const result = `rgba(${r}, ${g}, ${b}`
16-
return result
30+
return `rgba(${r}, ${g}, ${b}`
1731
}
1832

1933
export default hexToRgb

js/src/utilities/hex-to-rgba.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,27 @@
77

88
/* eslint-disable no-magic-numbers */
99
const hexToRgba = (color, opacity = 100) => {
10-
const hex = color.replace('#', '')
11-
const r = parseInt(hex.substring(0, 2), 16)
12-
const g = parseInt(hex.substring(2, 4), 16)
13-
const b = parseInt(hex.substring(4, 6), 16)
10+
if (typeof color === 'undefined') {
11+
throw new Error('Hex color is not defined')
12+
}
13+
const hex = color.match(/^#(?:[0-9a-f]{3}){1,2}$/i)
14+
if (!hex) {
15+
throw new Error(`${color} is not a valid hex color`)
16+
}
17+
let r
18+
let g
19+
let b
20+
if (color.length === 7) {
21+
r = parseInt(color.substring(1, 3), 16)
22+
g = parseInt(color.substring(3, 5), 16)
23+
b = parseInt(color.substring(5, 7), 16)
24+
} else {
25+
r = parseInt(color.substring(1, 2), 16)
26+
g = parseInt(color.substring(2, 3), 16)
27+
b = parseInt(color.substring(3, 5), 16)
28+
}
1429

15-
const result = `rgba(${r}, ${g}, ${b}, ${opacity / 100}`
16-
return result
30+
return `rgba(${r}, ${g}, ${b}, ${opacity / 100}`
1731
}
1832

1933
export default hexToRgba

js/src/utilities/rgb-to-hex.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@
77

88
/* eslint-disable no-magic-numbers */
99
const rgbToHex = (color) => {
10+
if (typeof color === 'undefined') {
11+
throw new Error('Hex color is not defined')
12+
}
1013
const rgb = color.match(/^rgba?[\s+]?\([\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?,[\s+]?(\d+)[\s+]?/i)
11-
if (rgb) {
12-
const r = `0${parseInt(rgb[1], 10).toString(16)}`
13-
const g = `0${parseInt(rgb[2], 10).toString(16)}`
14-
const b = `0${parseInt(rgb[3], 10).toString(16)}`
15-
16-
return rgb ? `#${r.slice(-2)}${g.slice(-2)}${b.slice(-2)}` : ''
14+
if (!rgb) {
15+
throw new Error(`${color} is not a valid rgb color`)
1716
}
18-
return ''
17+
const r = `0${parseInt(rgb[1], 10).toString(16)}`
18+
const g = `0${parseInt(rgb[2], 10).toString(16)}`
19+
const b = `0${parseInt(rgb[3], 10).toString(16)}`
20+
21+
return `#${r.slice(-2)}${g.slice(-2)}${b.slice(-2)}`
1922
}
2023

2124
export default rgbToHex

0 commit comments

Comments
 (0)