Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

features(hex): Basic Functionality #51

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions source/bits/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -27,6 +28,7 @@ export {
reduceXnor,
reduceXor,
toBoolean,
toHex,
toString,
xnor,
xor,
Expand All @@ -45,6 +47,7 @@ export default {
reduceXnor,
reduceXor,
toBoolean,
toHex,
toString,
xnor,
xor,
Expand Down
8 changes: 8 additions & 0 deletions source/bits/to-hex.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
20 changes: 20 additions & 0 deletions source/bits/to-hex.ts
Original file line number Diff line number Diff line change
@@ -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
}
7 changes: 7 additions & 0 deletions source/hex/index.ts
Original file line number Diff line number Diff line change
@@ -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 }
26 changes: 26 additions & 0 deletions source/hex/to-bits.test.ts
Original file line number Diff line number Diff line change
@@ -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], [])
)
})
20 changes: 20 additions & 0 deletions source/hex/to-bits.ts
Original file line number Diff line number Diff line change
@@ -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
}
28 changes: 28 additions & 0 deletions source/hex/to-boolean.test.ts
Original file line number Diff line number Diff line change
@@ -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)
)
})
20 changes: 20 additions & 0 deletions source/hex/to-boolean.ts
Original file line number Diff line number Diff line change
@@ -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
}
26 changes: 26 additions & 0 deletions source/hex/to-string.test.ts
Original file line number Diff line number Diff line change
@@ -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], [])
)
})
15 changes: 15 additions & 0 deletions source/hex/to-string.ts
Original file line number Diff line number Diff line change
@@ -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
}
5 changes: 3 additions & 2 deletions source/index.ts
Original file line number Diff line number Diff line change
@@ -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 }
1 change: 1 addition & 0 deletions source/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export type Bit = 0 | 1
export type Bits = Array<Bit>
export type BooleanBits = Array<boolean>
export type Byte = [Bit, Bit, Bit, Bit, Bit, Bit, Bit, Bit]
export type HexArray = Array<UInt4>
export type Nibble = [Bit, Bit, Bit, Bit]

// @TODO: Find a better solution
Expand Down