From 2354ba87f22c622aaa97ec41c4a01c172a78377a Mon Sep 17 00:00:00 2001 From: Fahim Faisaal Date: Tue, 1 Mar 2022 12:22:24 +0600 Subject: [PATCH 1/6] feat: used js object intead of switch --- Conversions/HexToBinary.js | 40 ++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/Conversions/HexToBinary.js b/Conversions/HexToBinary.js index 2334954f92..546eca9d23 100644 --- a/Conversions/HexToBinary.js +++ b/Conversions/HexToBinary.js @@ -1,24 +1,22 @@ -const binLookup = (c) => { - switch (c.toLowerCase()) { - case '0': return '0000' - case '1': return '0001' - case '2': return '0010' - case '3': return '0011' - case '4': return '0100' - case '5': return '0101' - case '6': return '0110' - case '7': return '0111' - case '8': return '1000' - case '9': return '1001' - case 'a': return '1010' - case 'b': return '1011' - case 'c': return '1100' - case 'd': return '1101' - case 'e': return '1110' - case 'f': return '1111' - default: return '' - } -} +const binLookup = (key) => ({ + 0: '0000', + 1: '0001', + 2: '0010', + 3: '0011', + 4: '0100', + 5: '0101', + 6: '0110', + 7: '0111', + 8: '1000', + 9: '1001', + a: '1010', + b: '1011', + c: '1100', + d: '1101', + e: '1110', + f: '1111', +}[key.toLowerCase()]) + const hexToBinary = (hexString) => { /* Function for converting Hex to Binary From f4ba14a2683c45f1ae1f17777161ada76c377fc2 Mon Sep 17 00:00:00 2001 From: Fahim Faisaal Date: Tue, 1 Mar 2022 12:24:01 +0600 Subject: [PATCH 2/6] pref: optimzed the algo with regex & replace method --- Conversions/HexToBinary.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Conversions/HexToBinary.js b/Conversions/HexToBinary.js index 546eca9d23..20bbdb4bd9 100644 --- a/Conversions/HexToBinary.js +++ b/Conversions/HexToBinary.js @@ -23,10 +23,12 @@ const hexToBinary = (hexString) => { 1. We convert every hexadecimal bit to 4 binary bits 2. Conversion goes by searching in the lookup table - - */ - const hexLexemes = hexString.split('') - return hexLexemes.map(lexeme => binLookup(lexeme)).join('') + */ + + return hexString.replace( + /[0-9a-f]/gi, + lexeme => binLookup(lexeme) + ) } export default hexToBinary From 3fc90f776df0dfc7ecb92154d16a284fe78f9fef Mon Sep 17 00:00:00 2001 From: Fahim Faisaal Date: Tue, 1 Mar 2022 12:28:11 +0600 Subject: [PATCH 3/6] feat: add hex validation with test case --- Conversions/HexToBinary.js | 3 +++ Conversions/test/HexToBinary.test.js | 8 +++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Conversions/HexToBinary.js b/Conversions/HexToBinary.js index 20bbdb4bd9..6747bfa8ce 100644 --- a/Conversions/HexToBinary.js +++ b/Conversions/HexToBinary.js @@ -18,6 +18,9 @@ const binLookup = (key) => ({ }[key.toLowerCase()]) const hexToBinary = (hexString) => { + if (/[^\da-f]/gi.test(hexString)) { + throw new Error('Argument not a valid Hex code!') + } /* Function for converting Hex to Binary diff --git a/Conversions/test/HexToBinary.test.js b/Conversions/test/HexToBinary.test.js index 40148d1cee..bf8bd31dfb 100644 --- a/Conversions/test/HexToBinary.test.js +++ b/Conversions/test/HexToBinary.test.js @@ -1,6 +1,12 @@ import hexToBinary from '../HexToBinary' -describe('hexToBinary', () => { +describe('Testing hexToBinary', () => { + it('expects throw error in invalid hex', () => { + expect(() => hexToBinary('Hello i am not a valid Hex')).toThrowError() + expect(() => hexToBinary('Gf46f')).toThrowError() + expect(() => hexToBinary('M')).toThrowError() + }) + it('expects to return correct hexadecimal value', () => { expect(hexToBinary('8')).toBe('1000') }) From 6d9e10ab7fe7fc6999f60f4341205a10db4987c1 Mon Sep 17 00:00:00 2001 From: Fahim Faisaal Date: Tue, 1 Mar 2022 12:34:24 +0600 Subject: [PATCH 4/6] feat: add type validation --- Conversions/HexToBinary.js | 4 ++++ Conversions/test/HexToBinary.test.js | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/Conversions/HexToBinary.js b/Conversions/HexToBinary.js index 6747bfa8ce..f90c1b8dd7 100644 --- a/Conversions/HexToBinary.js +++ b/Conversions/HexToBinary.js @@ -18,6 +18,10 @@ const binLookup = (key) => ({ }[key.toLowerCase()]) const hexToBinary = (hexString) => { + if (typeof hexString !== 'string') { + throw new TypeError('Argument is not a string type') + } + if (/[^\da-f]/gi.test(hexString)) { throw new Error('Argument not a valid Hex code!') } diff --git a/Conversions/test/HexToBinary.test.js b/Conversions/test/HexToBinary.test.js index bf8bd31dfb..6a7ec265d9 100644 --- a/Conversions/test/HexToBinary.test.js +++ b/Conversions/test/HexToBinary.test.js @@ -1,6 +1,12 @@ import hexToBinary from '../HexToBinary' describe('Testing hexToBinary', () => { + it('expects throw error in invalid types', () => { + expect(() => hexToBinary(false)).toThrowError() + expect(() => hexToBinary(null)).toThrowError() + expect(() => hexToBinary(23464)).toThrowError() + }) + it('expects throw error in invalid hex', () => { expect(() => hexToBinary('Hello i am not a valid Hex')).toThrowError() expect(() => hexToBinary('Gf46f')).toThrowError() From d96a70b976c91953a139fc80879ff2de9a35fe0b Mon Sep 17 00:00:00 2001 From: Fahim Faisaal Date: Tue, 1 Mar 2022 12:36:06 +0600 Subject: [PATCH 5/6] chore: fix grammar mistake --- Conversions/HexToBinary.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Conversions/HexToBinary.js b/Conversions/HexToBinary.js index f90c1b8dd7..868544a71b 100644 --- a/Conversions/HexToBinary.js +++ b/Conversions/HexToBinary.js @@ -14,16 +14,16 @@ const binLookup = (key) => ({ c: '1100', d: '1101', e: '1110', - f: '1111', + f: '1111' }[key.toLowerCase()]) const hexToBinary = (hexString) => { if (typeof hexString !== 'string') { throw new TypeError('Argument is not a string type') } - + if (/[^\da-f]/gi.test(hexString)) { - throw new Error('Argument not a valid Hex code!') + throw new Error('Argument is not a valid HEX code!') } /* Function for converting Hex to Binary @@ -31,7 +31,7 @@ const hexToBinary = (hexString) => { 1. We convert every hexadecimal bit to 4 binary bits 2. Conversion goes by searching in the lookup table */ - + return hexString.replace( /[0-9a-f]/gi, lexeme => binLookup(lexeme) From fa985c08cedfa4306d6f354aea3d55fbcab74c91 Mon Sep 17 00:00:00 2001 From: Fahim Faisaal Date: Wed, 2 Mar 2022 07:04:22 +0600 Subject: [PATCH 6/6] docs: add binLookup comments --- Conversions/HexToBinary.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Conversions/HexToBinary.js b/Conversions/HexToBinary.js index 868544a71b..2456dba016 100644 --- a/Conversions/HexToBinary.js +++ b/Conversions/HexToBinary.js @@ -15,7 +15,7 @@ const binLookup = (key) => ({ d: '1101', e: '1110', f: '1111' -}[key.toLowerCase()]) +}[key.toLowerCase()]) // select the binary number by valid hex key with the help javascript object const hexToBinary = (hexString) => { if (typeof hexString !== 'string') {

AltStyle によって変換されたページ (->オリジナル) /