diff --git a/source/bits/index.ts b/source/bits/index.ts index d1eb53a..d983b20 100644 --- a/source/bits/index.ts +++ b/source/bits/index.ts @@ -10,6 +10,7 @@ import reduceOr from './reduce-or' import reduceXnor from './reduce-xnor' import reduceXor from './reduce-xor' import toBoolean from './to-boolean' +import toHex from './to-hex' import toString from './to-string' import xnor from './xnor' import xor from './xor' @@ -27,6 +28,7 @@ export { reduceXnor, reduceXor, toBoolean, + toHex, toString, xnor, xor, @@ -45,6 +47,7 @@ export default { reduceXnor, reduceXor, toBoolean, + toHex, toString, xnor, xor, diff --git a/source/bits/to-hex.test.ts b/source/bits/to-hex.test.ts new file mode 100644 index 0000000..cc3a516 --- /dev/null +++ b/source/bits/to-hex.test.ts @@ -0,0 +1,8 @@ +import bits from '.' +import { Bits, HexArray } from '../types' + +test('toHex', () => { + const input: Bits = [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0] + const output: HexArray = [10, 10, 10] + expect(bits.toHex(input)).toEqual(output) +}) diff --git a/source/bits/to-hex.ts b/source/bits/to-hex.ts new file mode 100644 index 0000000..5c2cda3 --- /dev/null +++ b/source/bits/to-hex.ts @@ -0,0 +1,20 @@ +import { Bits, HexArray, Nibble } from '../types' +import nibble from '../nibble' + +/** + * @description converts a Bit Array to a UInt4 Array + * @example bitwise.hex.toBits([15]) => [1,1,1,1,] + */ +export default (bits: Bits): HexArray => { + if (bits.length % 4 !== 0) + throw new RangeError('Bits length not divisible by 4') + + const result: HexArray = [] + + for (let i = 0; i < bits.length; i = i + 4) { + const nibbleBits: Nibble = [bits[i], bits[i + 1], bits[i + 2], bits[i + 3]] + result.push(nibble.write(nibbleBits)) + } + + return result +} diff --git a/source/hex/index.ts b/source/hex/index.ts new file mode 100644 index 0000000..b7c659e --- /dev/null +++ b/source/hex/index.ts @@ -0,0 +1,7 @@ +import toBits from './to-bits' +import toBoolean from './to-boolean' +import toString from './to-string' + +export { toBits, toBoolean, toString } + +export default { toBits, toBoolean, toString } diff --git a/source/hex/to-bits.test.ts b/source/hex/to-bits.test.ts new file mode 100644 index 0000000..363be2d --- /dev/null +++ b/source/hex/to-bits.test.ts @@ -0,0 +1,26 @@ +import { HexArray } from '../types' +import toBits from './to-bits' + +test('toBits', () => { + const hex: HexArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] + expect(toBits(hex)).toEqual( + [ + [0, 0, 0, 0], + [0, 0, 0, 1], + [0, 0, 1, 0], + [0, 0, 1, 1], + [0, 1, 0, 0], + [0, 1, 0, 1], + [0, 1, 1, 0], + [0, 1, 1, 1], + [1, 0, 0, 0], + [1, 0, 0, 1], + [1, 0, 1, 0], + [1, 0, 1, 1], + [1, 1, 0, 0], + [1, 1, 0, 1], + [1, 1, 1, 0], + [1, 1, 1, 1], + ].reduce((a, b) => [...a, ...b], []) + ) +}) diff --git a/source/hex/to-bits.ts b/source/hex/to-bits.ts new file mode 100644 index 0000000..7d9fa1d --- /dev/null +++ b/source/hex/to-bits.ts @@ -0,0 +1,20 @@ +import { Bits, HexArray } from '../types' +import nibble from '../nibble' + +/** + * @description converts a UInt4 Array to Bits + * @example bitwise.hex.toBits([15]) => [1, 1, 1, 1] + */ +export default (hex: HexArray): Bits => { + const result: Bits = [] + + for (let i = 0; i < hex.length; i++) { + const bits = nibble.read(hex[i]) + result[4 * i] = bits[0] + result[4 * i + 1] = bits[1] + result[4 * i + 2] = bits[2] + result[4 * i + 3] = bits[3] + } + + return result +} diff --git a/source/hex/to-boolean.test.ts b/source/hex/to-boolean.test.ts new file mode 100644 index 0000000..d2c18c0 --- /dev/null +++ b/source/hex/to-boolean.test.ts @@ -0,0 +1,28 @@ +import { HexArray } from '../types' +import toBoolean from './to-boolean' + +test('toBoolean', () => { + const hex: HexArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] + expect(toBoolean(hex)).toEqual( + [ + [0, 0, 0, 0], + [0, 0, 0, 1], + [0, 0, 1, 0], + [0, 0, 1, 1], + [0, 1, 0, 0], + [0, 1, 0, 1], + [0, 1, 1, 0], + [0, 1, 1, 1], + [1, 0, 0, 0], + [1, 0, 0, 1], + [1, 0, 1, 0], + [1, 0, 1, 1], + [1, 1, 0, 0], + [1, 1, 0, 1], + [1, 1, 1, 0], + [1, 1, 1, 1], + ] + .reduce((a, b) => [...a, ...b], []) + .map(Boolean) + ) +}) diff --git a/source/hex/to-boolean.ts b/source/hex/to-boolean.ts new file mode 100644 index 0000000..ba7a914 --- /dev/null +++ b/source/hex/to-boolean.ts @@ -0,0 +1,20 @@ +import { HexArray, BooleanBits } from '../types' +import nibble from '../nibble' + +/** + * @description converts a UInt4 Array to boolean Bits + * @example bitwise.hex.toBoolean([15]) => [true, true, true, true] + */ +export default (hex: HexArray): BooleanBits => { + const result: BooleanBits = [] + + for (let i = 0; i < hex.length; i++) { + const bits = nibble.read(hex[i]) + result[4 * i] = bits[0] === 1 + result[4 * i + 1] = bits[1] === 1 + result[4 * i + 2] = bits[2] === 1 + result[4 * i + 3] = bits[3] === 1 + } + + return result +} diff --git a/source/hex/to-string.test.ts b/source/hex/to-string.test.ts new file mode 100644 index 0000000..363be2d --- /dev/null +++ b/source/hex/to-string.test.ts @@ -0,0 +1,26 @@ +import { HexArray } from '../types' +import toBits from './to-bits' + +test('toBits', () => { + const hex: HexArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] + expect(toBits(hex)).toEqual( + [ + [0, 0, 0, 0], + [0, 0, 0, 1], + [0, 0, 1, 0], + [0, 0, 1, 1], + [0, 1, 0, 0], + [0, 1, 0, 1], + [0, 1, 1, 0], + [0, 1, 1, 1], + [1, 0, 0, 0], + [1, 0, 0, 1], + [1, 0, 1, 0], + [1, 0, 1, 1], + [1, 1, 0, 0], + [1, 1, 0, 1], + [1, 1, 1, 0], + [1, 1, 1, 1], + ].reduce((a, b) => [...a, ...b], []) + ) +}) diff --git a/source/hex/to-string.ts b/source/hex/to-string.ts new file mode 100644 index 0000000..d866c96 --- /dev/null +++ b/source/hex/to-string.ts @@ -0,0 +1,15 @@ +import { HexArray } from '../types' + +/** + * @description converts a UInt4 Array to a String + * @example bitwise.hex.toString([15]) => 'F' + */ +export default (hex: HexArray): string => { + let result: string = '' + + for (let i: number = 0; i < hex.length; i++) { + result += i.toString(16) + } + + return result +} diff --git a/source/index.ts b/source/index.ts index 3db0bb9..6ab1b9e 100644 --- a/source/index.ts +++ b/source/index.ts @@ -1,10 +1,11 @@ import bits from './bits' import buffer from './buffer' import byte from './byte' +import hex from './hex' import integer from './integer' import nibble from './nibble' import string from './string' -export { bits, buffer, byte, integer, nibble, string } +export { bits, buffer, byte, hex, integer, nibble, string } -export default { bits, buffer, byte, integer, nibble, string } +export default { bits, buffer, byte, hex, integer, nibble, string } diff --git a/source/types.ts b/source/types.ts index ae6688c..0f33225 100644 --- a/source/types.ts +++ b/source/types.ts @@ -2,6 +2,7 @@ export type Bit = 0 | 1 export type Bits = Array export type BooleanBits = Array export type Byte = [Bit, Bit, Bit, Bit, Bit, Bit, Bit, Bit] +export type HexArray = Array export type Nibble = [Bit, Bit, Bit, Bit] // @TODO: Find a better solution