Skip to content

Commit 378d4ab

Browse files
authored
merge: Upgraded hexToBinary function (TheAlgorithms#910)
* feat: used js object intead of switch * pref: optimzed the algo with regex & replace method * feat: add hex validation with test case * feat: add type validation * chore: fix grammar mistake * docs: add binLookup comments
1 parent ab06131 commit 378d4ab

File tree

2 files changed

+44
-25
lines changed

2 files changed

+44
-25
lines changed

Conversions/HexToBinary.js

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,41 @@
1-
const binLookup = (c) => {
2-
switch (c.toLowerCase()) {
3-
case '0': return '0000'
4-
case '1': return '0001'
5-
case '2': return '0010'
6-
case '3': return '0011'
7-
case '4': return '0100'
8-
case '5': return '0101'
9-
case '6': return '0110'
10-
case '7': return '0111'
11-
case '8': return '1000'
12-
case '9': return '1001'
13-
case 'a': return '1010'
14-
case 'b': return '1011'
15-
case 'c': return '1100'
16-
case 'd': return '1101'
17-
case 'e': return '1110'
18-
case 'f': return '1111'
19-
default: return ''
20-
}
21-
}
1+
const binLookup = (key) => ({
2+
0: '0000',
3+
1: '0001',
4+
2: '0010',
5+
3: '0011',
6+
4: '0100',
7+
5: '0101',
8+
6: '0110',
9+
7: '0111',
10+
8: '1000',
11+
9: '1001',
12+
a: '1010',
13+
b: '1011',
14+
c: '1100',
15+
d: '1101',
16+
e: '1110',
17+
f: '1111'
18+
}[key.toLowerCase()]) // select the binary number by valid hex key with the help javascript object
19+
2220
const hexToBinary = (hexString) => {
21+
if (typeof hexString !== 'string') {
22+
throw new TypeError('Argument is not a string type')
23+
}
24+
25+
if (/[^\da-f]/gi.test(hexString)) {
26+
throw new Error('Argument is not a valid HEX code!')
27+
}
2328
/*
2429
Function for converting Hex to Binary
2530
2631
1. We convert every hexadecimal bit to 4 binary bits
2732
2. Conversion goes by searching in the lookup table
33+
*/
2834

29-
*/
30-
const hexLexemes = hexString.split('')
31-
return hexLexemes.map(lexeme => binLookup(lexeme)).join('')
35+
return hexString.replace(
36+
/[0-9a-f]/gi,
37+
lexeme => binLookup(lexeme)
38+
)
3239
}
3340

3441
export default hexToBinary

Conversions/test/HexToBinary.test.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
import hexToBinary from '../HexToBinary'
22

3-
describe('hexToBinary', () => {
3+
describe('Testing hexToBinary', () => {
4+
it('expects throw error in invalid types', () => {
5+
expect(() => hexToBinary(false)).toThrowError()
6+
expect(() => hexToBinary(null)).toThrowError()
7+
expect(() => hexToBinary(23464)).toThrowError()
8+
})
9+
10+
it('expects throw error in invalid hex', () => {
11+
expect(() => hexToBinary('Hello i am not a valid Hex')).toThrowError()
12+
expect(() => hexToBinary('Gf46f')).toThrowError()
13+
expect(() => hexToBinary('M')).toThrowError()
14+
})
15+
416
it('expects to return correct hexadecimal value', () => {
517
expect(hexToBinary('8')).toBe('1000')
618
})

0 commit comments

Comments
 (0)