diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..f603626 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,38 @@ +/** + * encode a string to base64 + * + * @param {string} str string to encode + * @param {string} encoding encoding the string is in + * @returns {string} base64 + */ +export function to_b64(str: string, encoding?: string): string; +/** + * encode a string to hex + * + * @param {string} str string to encode + * @param {string} encoding encoding the string is in + * @returns {string} hex + */ +export function to_hex(str: string, encoding?: string): string; +/** + * encode a string to utf-8 + * + * @param {string} str string to encode + * @param {string} encoding encoding the string is in + * @returns {string} utf-8 + */ +export function to_utf8(str: string, encoding: string): string; +/** + * encode a number to hex + * + * @param {number} n number to encode + * @returns {string} hex + */ +export function num_to_hex(n: number): string; +/** + * decode a number from hex + * + * @param {string} h hex to decode + * @returns {number} number + */ +export function hex_to_num(h: string): number; diff --git a/index.js b/index.js index feb444e..5e55f77 100644 --- a/index.js +++ b/index.js @@ -44,9 +44,31 @@ function to_hex (str, encoding = '') { return buf.toString ('hex'); } +/** + * encode a number to hex + * + * @param {number} n number to encode + * @returns {string} hex + */ +function num_to_hex (n) { + return n.toString(16); +} + +/** + * decode a number from hex + * + * @param {string} h hex to decode + * @returns {number} number + */ +function hex_to_num (h) { + return parseInt(h, 16); +} + module.exports = { to_b64, to_hex, - to_utf8 + to_utf8, + num_to_hex, + hex_to_num };