mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-04 15:39:42 +08:00

* chore: Switch to Node 20 + Vitest * chore: migrate to vitest mock functions * chore: code style (switch to prettier) * test: re-enable long-running test Seems the switch to Node 20 and Vitest has vastly improved the code's and / or the test's runtime! see #1193 * chore: code style * chore: fix failing tests * Updated Documentation in README.md * Update contribution guidelines to state usage of Prettier * fix: set prettier printWidth back to 80 * chore: apply updated code style automatically * fix: set prettier line endings to lf again * chore: apply updated code style automatically --------- Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com>
40 lines
879 B
JavaScript
40 lines
879 B
JavaScript
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()] // select the binary number by valid hex key with the help javascript object
|
|
|
|
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 is not a valid HEX code!')
|
|
}
|
|
/*
|
|
Function for converting Hex to Binary
|
|
|
|
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))
|
|
}
|
|
|
|
export default hexToBinary
|