@mrpelz/misc-utils
v3.0.1
Published
Miscellaneous TypeScript helpers.
Readme
@mrpelz/misc-utils
Miscellaneous TypeScript helpers.
data
Miscellaneous data conversion helpers.
emptyBuffer
Empty/zero-length Buffer.
falseBuffer
One-byte 0 Buffer.
trueBuffer
One-byte 1 Buffer.
arrayCompare
const arrayCompare = <
A extends Array<unknown>,
B extends Array<unknown>,
>(
a: A,
b: B,
): b is BReturns true if all elements of array a strict-equal array b.
arrayPadLeft
const arrayPadLeft = <T>(
input: T[],
length: number,
value: T = null as unknown as T,
): T[]Inserts length-n of value at the start of array input.
Modifies array in-place and returns it.
arrayPadRight
const arrayPadRight = <T>(
input: T[],
length: number,
value: T = null as unknown as T,
): T[]Inserts length-n of value at the end of array input.
Modifies array in-place and returns it.
bufferChunks
const bufferChunks = (input: Buffer, chunkSize = 1): Buffer[]Splits Buffer input into chunks of size chunkSize each.
Returns array of Buffer chunks. Throws if input Buffer is smaller than chunkSize.
concatBytes
const concatBytes = (input: number[]): BufferConcatenates number-array as bytes and returns as Buffer.
humanPayload
const humanPayload = (input: Buffer): stringPretty-prints Buffer input’s binary contents in a human readable format, represented as binary, decimal and hexadecimal:
0b00000000 | 0b00000001 | 0b00000010 | 0b00001111 | 0b00010000
0 | 1 | 2 | 15 | 16
0x00 | 0x01 | 0x02 | 0x0f | 0x10Returns pre-formatted multi-line string.
jsonParseGuarded
const jsonParseGuarded = <T>(input: unknown): T | ErrorGuarded JSON-parse. Tries to parse anything in input, returns JSON.parse result. Returns Error if input is not a string. If parsing of the string fails, catches the Error and returns it. Error is returned instead of thrown to ease implementation of fallback code paths.
numberToDigits
const numberToDigits = (
input: number,
pad = 0,
radix = 10,
): number[]Splits number in input into single digits and returns them as number-array. Throws if input is not an integer.
readNumber
const readNumber = (input: Buffer, bytes = 1): numberRead number value from Buffer input with number of bytes. Throws if the buffer isn’t long enough to fit the desired byte count.
bytes= 1 → UInt8bytes= 2 → UInt16LEbytes= 4 → UInt32LE
Throws otherwise.
booleanToBuffer
const booleanToBuffer = (input: boolean): BufferTurns boolean input into trueBuffer or falseBuffer respectively.
bufferToBoolean
const bufferToBoolean = (input: Buffer): booleanTurns Buffer input into boolean. A buffer whose first byte is zero is considered false, everything else is considered true. Throws if Buffer isn’t long enough.
swapByte
const swapByte = (input: number): numberBitwise-invert the the input value. Use for single byte values.
writeNumber
const writeNumber = (input: number, bytes = 1): BufferWrite number value from input with number of bytes into Buffer. Throws if the number cannot be represented using the desired byte count.
bytes= 1 → UInt8bytes= 2 → UInt16LEbytes= 4 → UInt32LE
Throws otherwise.
number
bitRange
const bitRange = (count: number): numberReturns the number bit range of given bit length in count, e.g.
- 4 → 15
- 8 → 255
- 16 → 65535
byteRange
const byteRange = (count: number): numberReturns the number range of given byte length in count, e.g. 4 → 4294967295.
