UNPKG

@techmely/utils

Version:

Collection of helpful JavaScript / TypeScript utils

42 lines (37 loc) 808 B
/*! * @techmely/utils * Copyright(c) 2021-2024 Techmely <techmely.creation@gmail.com> * MIT Licensed */ // src/convert.ts function intToHex(integer) { if (integer < 0) { throw new Error("Invalid integer as argument, must be unsigned!"); } const hex = integer.toString(16); return hex.length % 2 ? `0${hex}` : hex; } function intToBuffer(integer) { const hex = intToHex(integer); return Buffer.from(hex, "hex"); } function toBoolean(val) { return val ? val !== "false" : false; } function convertHrTime(hrtime) { const nanoseconds = hrtime; const number = Number(nanoseconds); const milliseconds = number / 1e6; const seconds = number / 1e9; return { seconds, milliseconds, nanoseconds }; } export { intToHex, intToBuffer, toBoolean, convertHrTime };