Skip to content

Commit

Permalink
refactor: refactor handleBits and handleBytes functions
Browse files Browse the repository at this point in the history
  • Loading branch information
theodrosyimer committed Feb 26, 2024
1 parent 2d8b844 commit f854ae1
Showing 1 changed file with 72 additions and 26 deletions.
98 changes: 72 additions & 26 deletions src/range.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,85 @@
import { formatNumber } from './utils.js'

export function bitsHandler(options: {
bits: number
signed: boolean
locale: string
}) {
const calculatedValue = calculateValueRangeFromBits(
options.signed ? -options.bits : options.bits,
)
export function handleBits(options: { bits: string; locale?: string }) {
let calculatedValue: number[] | bigint[] = []

try {
calculatedValue = calculateValueRangeFromBits(options.bits)
} catch (error) {
if (error instanceof Error) {
return error.message
}
}

if (
typeof calculatedValue[0] === 'number' &&
typeof calculatedValue[1] === 'number'
) {
return `${formatNumber(calculatedValue[0], options.locale)} <-> ${formatNumber(
calculatedValue[1],
options.locale,
)}`
let start = ''
let end = ''

try {
start = formatNumber(calculatedValue[0], options.locale)
end = formatNumber(calculatedValue[1], options.locale)
} catch (error) {
if (error instanceof Error) {
return error.message
}
}

return `${start} <-> ${end}`
}
}

export function bytesHandler(options: {
bytes: number
signed: boolean
locale: string
}) {
const bigCalculatedValue = calculateValueRangeFromBytes(
options.signed ? options.bytes : options.bytes,
)
export function handleBytes(options: { bytes: string; locale?: string }) {
let bigCalculatedValue: [number, bigint] | [number, number] = [0, 0]

try {
bigCalculatedValue = calculateValueRangeFromBytes(options.bytes)
} catch (error) {
if (error instanceof Error) {
return error.message
}
}

if (
typeof bigCalculatedValue[0] === 'number' &&
(typeof bigCalculatedValue[1] === 'bigint' ||
typeof bigCalculatedValue[1] === 'number')
) {
return `${formatNumber(bigCalculatedValue[0], options.locale)} <-> ${formatNumber(
bigCalculatedValue[1],
options.locale,
)}`
let start = ''
let end = ''

try {
start = formatNumber(bigCalculatedValue[0], options.locale)
end = formatNumber(bigCalculatedValue[1], options.locale)
} catch (error) {
if (error instanceof Error) {
return error.message
}
}

return `${start} <-> ${end}`
}
}

/**
* @param bits size in `bits` of the numeric value type. A positive value means return an `unsigned` range, a negative value means return a `signed` range
* @throws if the `bits` argument is not a number
*/
function calculateValueRangeFromBits(bits: number) {
function calculateValueRangeFromBits(input: string) {
if (Number.isNaN(+input)) {
throw new Error('input must be a number')
}

const bits = +input

if (bits === 0) {
return [0, 0]
}

let half = 0

if (Math.sign(bits) === -1) {
if (Math.abs(bits) < 8) {
half = 256 / (8 / -bits)
Expand All @@ -66,11 +100,23 @@ function calculateValueRangeFromBits(bits: number) {

/**
* @param bytes size in `bytes` of the numeric value type. A positive value means return an `unsigned` range, a negative value means return a `signed` range
* @throws if the `bytes` argument is not a number
*/
function calculateValueRangeFromBytes(
bytes: number,
input: string,
): [number, bigint] | [number, number] {
if (Number.isNaN(+input)) {
throw new Error('input must be a number')
}

const bytes = +input

if (bytes === 0) {
return [0, 0]
}

let half = 0

if (Math.sign(bytes) === -1) {
half = 256 ** -bytes / 2

Expand Down

0 comments on commit f854ae1

Please sign in to comment.