Skip to content

Commit

Permalink
feat: add CLI commands and utility functions
Browse files Browse the repository at this point in the history
  • Loading branch information
theodrosyimer committed Feb 25, 2024
1 parent acb796d commit 26d5b52
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions src/binez.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { Command } from 'commander'

export const binezCli = new Command()

binezCli
.name('binez')
.version('0.0.1', '-v, --version', 'output the current version')
.description('CLI to calculate or convert binary numbers!')

binezCli
.command('convert')
.description('Convert a given number of megabytes to bytes')
.argument('<string>', 'string to process')
.option('-l, --locale', 'locale to use for formatting', 'en-US')
.action(
(
str: string,
options: {
locale: string
},
) => {
let result = str
if (options.locale) {
result = result.toLowerCase()
}
console.log(result)
},
)

binezCli
.command('calc')
.description('Reverse a string')
.argument('<string>', 'string to reverse')
.action((str: string) => {
console.log(str.split('').reverse().join(''))
})

/**
* Convert a given number of megabytes to bytes
*
* @param mb number of megabytes to convert to bytes
*/
function convertMBToBytes(mb: number) {

Check warning on line 43 in src/binez.ts

View workflow job for this annotation

GitHub Actions / lint-and-build (16)

'convertMBToBytes' is defined but never used

Check warning on line 43 in src/binez.ts

View workflow job for this annotation

GitHub Actions / lint-and-build (18)

'convertMBToBytes' is defined but never used

Check warning on line 43 in src/binez.ts

View workflow job for this annotation

GitHub Actions / lint-and-build (20)

'convertMBToBytes' is defined but never used
return mb * 1024 * 1024
}

/**
* Ca
* @param dataStorageSize size of data storage in `kilobytes`, `32` means `32kb`
* @param numberBitSize size in `bits` of the numeric value type
*/
function calculateDataStorageLimit(dataStorageSize: number, bitsSize: number) {

Check warning on line 52 in src/binez.ts

View workflow job for this annotation

GitHub Actions / lint-and-build (16)

'calculateDataStorageLimit' is defined but never used

Check warning on line 52 in src/binez.ts

View workflow job for this annotation

GitHub Actions / lint-and-build (18)

'calculateDataStorageLimit' is defined but never used

Check warning on line 52 in src/binez.ts

View workflow job for this annotation

GitHub Actions / lint-and-build (20)

'calculateDataStorageLimit' is defined but never used
return (dataStorageSize * 1024) / (bitsSize / 8)
}

/**
*
* @param bitsSize size in `bits` of the numeric value type. A positive value means return an `unsigned` range, a negative value means return a `signed` range
*/
function calculateValueRangeFromNumberBitsSize(bitsSize: number) {

Check warning on line 60 in src/binez.ts

View workflow job for this annotation

GitHub Actions / lint-and-build (16)

'calculateValueRangeFromNumberBitsSize' is defined but never used

Check warning on line 60 in src/binez.ts

View workflow job for this annotation

GitHub Actions / lint-and-build (18)

'calculateValueRangeFromNumberBitsSize' is defined but never used

Check warning on line 60 in src/binez.ts

View workflow job for this annotation

GitHub Actions / lint-and-build (20)

'calculateValueRangeFromNumberBitsSize' is defined but never used
let half = 0
if (Math.sign(bitsSize) === -1) {
half = 256 ** (-bitsSize / 8) / 2

return [-half, half - 1]
}

return [0, 256 ** (bitsSize / 8) - 1]
}

/**
*
* @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
*/
function calculateValueRangeFromBytes(bytes: number) {

Check warning on line 75 in src/binez.ts

View workflow job for this annotation

GitHub Actions / lint-and-build (16)

'calculateValueRangeFromBytes' is defined but never used

Check warning on line 75 in src/binez.ts

View workflow job for this annotation

GitHub Actions / lint-and-build (18)

'calculateValueRangeFromBytes' is defined but never used

Check warning on line 75 in src/binez.ts

View workflow job for this annotation

GitHub Actions / lint-and-build (20)

'calculateValueRangeFromBytes' is defined but never used
let half = 0
if (Math.sign(bytes) === -1) {
half = 256 ** -bytes / 2

return [-half, half - 1]
}

return [0, 256 ** bytes - 1]
}

/**
* @param value value to convert
* @param locale locale to use for formatting
*/
function formatNumber(value: number | bigint, locale = 'en-US') {

Check warning on line 90 in src/binez.ts

View workflow job for this annotation

GitHub Actions / lint-and-build (16)

'formatNumber' is defined but never used

Check warning on line 90 in src/binez.ts

View workflow job for this annotation

GitHub Actions / lint-and-build (18)

'formatNumber' is defined but never used

Check warning on line 90 in src/binez.ts

View workflow job for this annotation

GitHub Actions / lint-and-build (20)

'formatNumber' is defined but never used
return Intl.NumberFormat(locale).format(value)
}

0 comments on commit 26d5b52

Please sign in to comment.