@ethernauta/utils
Advanced tools
+21
| MIT License | ||
| Copyright (c) 2026 Nicolas Accetta | ||
| Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| of this software and associated documentation files (the "Software"), to deal | ||
| in the Software without restriction, including without limitation the rights | ||
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| copies of the Software, and to permit persons to whom the Software is | ||
| furnished to do so, subject to the following conditions: | ||
| The above copyright notice and this permission notice shall be included in all | ||
| copies or substantial portions of the Software. | ||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| SOFTWARE. |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"bytes-to-uint.js","names":[],"sources":["../src/bytes-to-uint.ts"],"sourcesContent":["import { bytes_to_hex } from \"./bytes-to-hex\"\n\nexport function bytes_to_uint(\n bytes: Uint8Array,\n): `0x${string}` {\n if (bytes.length === 0) return \"0x0\"\n const n = BigInt(bytes_to_hex(bytes))\n return `0x${n.toString(16)}`\n}\n"],"mappings":";;;AAEA,SAAgB,cACd,OACe;CACf,IAAI,MAAM,WAAW,GAAG,OAAO;CAE/B,OAAO,KADG,OAAO,aAAa,KAAK,CACvB,EAAE,SAAS,EAAE;AAC3B"} | ||
| {"version":3,"file":"bytes-to-uint.js","names":[],"sources":["../src/bytes-to-uint.ts"],"sourcesContent":["import { bytes_to_hex } from \"./bytes-to-hex\"\n\nexport function bytes_to_uint(\n bytes: Uint8Array,\n): `0x${string}` {\n if (bytes.length === 0) return \"0x0\"\n const n = BigInt(bytes_to_hex(bytes))\n return `0x${n.toString(16)}`\n}\n"],"mappings":";;;AAEA,SAAgB,cACd,OACe;CACf,IAAI,MAAM,WAAW,GAAG,OAAO;CAE/B,OAAO,KADG,OAAO,aAAa,KAAK,CACvB,CAAC,CAAC,SAAS,EAAE;AAC3B"} |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"camel-to-kebab.js","names":[],"sources":["../src/camel-to-kebab.ts"],"sourcesContent":["export function camel_to_kebab(input: string): string {\n return input\n .replace(/([a-z0-9])([A-Z])/g, \"$1-$2\")\n .replace(/([A-Z]+)([A-Z][a-z])/g, \"$1-$2\")\n .toLowerCase()\n}\n"],"mappings":";AAAA,SAAgB,eAAe,OAAuB;CACpD,OAAO,MACJ,QAAQ,sBAAsB,OAAO,EACrC,QAAQ,yBAAyB,OAAO,EACxC,YAAY;AACjB"} | ||
| {"version":3,"file":"camel-to-kebab.js","names":[],"sources":["../src/camel-to-kebab.ts"],"sourcesContent":["export function camel_to_kebab(input: string): string {\n return input\n .replace(/([a-z0-9])([A-Z])/g, \"$1-$2\")\n .replace(/([A-Z]+)([A-Z][a-z])/g, \"$1-$2\")\n .toLowerCase()\n}\n"],"mappings":";AAAA,SAAgB,eAAe,OAAuB;CACpD,OAAO,MACJ,QAAQ,sBAAsB,OAAO,CAAC,CACtC,QAAQ,yBAAyB,OAAO,CAAC,CACzC,YAAY;AACjB"} |
+1
-1
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"rlp.js","names":[],"sources":["../src/rlp.ts"],"sourcesContent":["// https://ethereum.org/en/developers/docs/data-structures-and-encoding/rlp/\n// Recursive Length Prefix encoding. Used by every Ethereum\n// transaction type for the canonical serialized form.\n\nimport { hex_to_bytes } from \"./hex-to-bytes\"\n\nexport type RlpInput =\n | string\n | number\n | bigint\n | Uint8Array\n | RlpInput[]\n\nexport function rlp_encode(input: RlpInput): Uint8Array {\n if (Array.isArray(input)) return encode_list(input)\n return encode_item(input)\n}\n\nfunction encode_item(\n item: string | number | bigint | Uint8Array,\n): Uint8Array {\n const bytes = to_bytes(item)\n const first = bytes[0]\n if (\n bytes.length === 1 &&\n first !== undefined &&\n first < 0x80\n ) {\n return bytes\n }\n if (bytes.length <= 55) {\n const out = new Uint8Array(1 + bytes.length)\n out[0] = 0x80 + bytes.length\n out.set(bytes, 1)\n return out\n }\n const length_bytes = encode_length(bytes.length)\n const out = new Uint8Array(\n 1 + length_bytes.length + bytes.length,\n )\n out[0] = 0xb7 + length_bytes.length\n out.set(length_bytes, 1)\n out.set(bytes, 1 + length_bytes.length)\n return out\n}\n\nfunction encode_list(list: RlpInput[]): Uint8Array {\n const items = list.map(rlp_encode)\n const total = items.reduce((s, i) => s + i.length, 0)\n if (total <= 55) {\n const out = new Uint8Array(1 + total)\n out[0] = 0xc0 + total\n let offset = 1\n for (const item of items) {\n out.set(item, offset)\n offset += item.length\n }\n return out\n }\n const length_bytes = encode_length(total)\n const out = new Uint8Array(\n 1 + length_bytes.length + total,\n )\n out[0] = 0xf7 + length_bytes.length\n out.set(length_bytes, 1)\n let offset = 1 + length_bytes.length\n for (const item of items) {\n out.set(item, offset)\n offset += item.length\n }\n return out\n}\n\nfunction to_bytes(\n input: string | number | bigint | Uint8Array,\n): Uint8Array {\n if (input instanceof Uint8Array) return input\n if (typeof input === \"string\") {\n if (input.startsWith(\"0x\")) return hex_to_bytes(input)\n return new TextEncoder().encode(input)\n }\n return number_to_bytes(BigInt(input))\n}\n\nfunction number_to_bytes(big: bigint): Uint8Array {\n if (big === 0n) return new Uint8Array([])\n const hex = big.toString(16)\n const padded = hex.padStart(\n hex.length + (hex.length % 2),\n \"0\",\n )\n return hex_to_bytes(padded)\n}\n\nfunction encode_length(length: number): Uint8Array {\n if (length === 0) return new Uint8Array([])\n const out: number[] = []\n let temp = length\n while (temp > 0) {\n out.unshift(temp & 0xff)\n temp >>= 8\n }\n return new Uint8Array(out)\n}\n\n// RLP only preserves bytes and nesting — numbers and strings\n// fed into `rlp_encode` come back as byte payloads. Callers\n// reapply meaning (number, address, hash, …) from the\n// surrounding schema.\nexport type RlpDecoded = Uint8Array | RlpDecoded[]\n\nexport function rlp_decode(bytes: Uint8Array): RlpDecoded {\n const [value, consumed] = decode_item(bytes, 0)\n if (consumed !== bytes.length) {\n throw new Error(\n `rlp_decode: ${bytes.length - consumed} trailing byte(s)`,\n )\n }\n return value\n}\n\nfunction decode_item(\n bytes: Uint8Array,\n offset: number,\n): [RlpDecoded, number] {\n if (offset >= bytes.length) {\n throw new Error(\"rlp_decode: unexpected end of input\")\n }\n const prefix = bytes[offset]\n if (prefix === undefined) {\n throw new Error(\"rlp_decode: unexpected end of input\")\n }\n if (prefix < 0x80) {\n return [bytes.slice(offset, offset + 1), 1]\n }\n if (prefix <= 0xb7) {\n const length = prefix - 0x80\n const start = offset + 1\n const end = start + length\n assert_in_bounds(bytes, end)\n return [bytes.slice(start, end), 1 + length]\n }\n if (prefix <= 0xbf) {\n const length_of_length = prefix - 0xb7\n const length_start = offset + 1\n const length_end = length_start + length_of_length\n assert_in_bounds(bytes, length_end)\n const length = read_length(\n bytes.subarray(length_start, length_end),\n )\n const start = length_end\n const end = start + length\n assert_in_bounds(bytes, end)\n return [\n bytes.slice(start, end),\n 1 + length_of_length + length,\n ]\n }\n if (prefix <= 0xf7) {\n const length = prefix - 0xc0\n const start = offset + 1\n const end = start + length\n assert_in_bounds(bytes, end)\n return [\n decode_list_payload(bytes, start, end),\n 1 + length,\n ]\n }\n const length_of_length = prefix - 0xf7\n const length_start = offset + 1\n const length_end = length_start + length_of_length\n assert_in_bounds(bytes, length_end)\n const length = read_length(\n bytes.subarray(length_start, length_end),\n )\n const start = length_end\n const end = start + length\n assert_in_bounds(bytes, end)\n return [\n decode_list_payload(bytes, start, end),\n 1 + length_of_length + length,\n ]\n}\n\nfunction decode_list_payload(\n bytes: Uint8Array,\n start: number,\n end: number,\n): RlpDecoded[] {\n const items: RlpDecoded[] = []\n let cursor = start\n while (cursor < end) {\n const [value, consumed] = decode_item(bytes, cursor)\n items.push(value)\n cursor += consumed\n }\n if (cursor !== end) {\n throw new Error(\n \"rlp_decode: list payload length mismatch\",\n )\n }\n return items\n}\n\nfunction read_length(bytes: Uint8Array): number {\n let length = 0\n for (const byte of bytes) {\n length = length * 0x100 + byte\n }\n return length\n}\n\nfunction assert_in_bounds(\n bytes: Uint8Array,\n end: number,\n): void {\n if (end > bytes.length) {\n throw new Error(\"rlp_decode: payload overruns input\")\n }\n}\n"],"mappings":";;;AAaA,SAAgB,WAAW,OAA6B;CACtD,IAAI,MAAM,QAAQ,KAAK,GAAG,OAAO,YAAY,KAAK;CAClD,OAAO,YAAY,KAAK;AAC1B;AAEA,SAAS,YACP,MACY;CACZ,MAAM,QAAQ,SAAS,IAAI;CAC3B,MAAM,QAAQ,MAAM;CACpB,IACE,MAAM,WAAW,KACjB,UAAU,UACV,QAAQ,KAER,OAAO;CAET,IAAI,MAAM,UAAU,IAAI;EACtB,MAAM,MAAM,IAAI,WAAW,IAAI,MAAM,MAAM;EAC3C,IAAI,KAAK,MAAO,MAAM;EACtB,IAAI,IAAI,OAAO,CAAC;EAChB,OAAO;CACT;CACA,MAAM,eAAe,cAAc,MAAM,MAAM;CAC/C,MAAM,MAAM,IAAI,WACd,IAAI,aAAa,SAAS,MAAM,MAClC;CACA,IAAI,KAAK,MAAO,aAAa;CAC7B,IAAI,IAAI,cAAc,CAAC;CACvB,IAAI,IAAI,OAAO,IAAI,aAAa,MAAM;CACtC,OAAO;AACT;AAEA,SAAS,YAAY,MAA8B;CACjD,MAAM,QAAQ,KAAK,IAAI,UAAU;CACjC,MAAM,QAAQ,MAAM,QAAQ,GAAG,MAAM,IAAI,EAAE,QAAQ,CAAC;CACpD,IAAI,SAAS,IAAI;EACf,MAAM,MAAM,IAAI,WAAW,IAAI,KAAK;EACpC,IAAI,KAAK,MAAO;EAChB,IAAI,SAAS;EACb,KAAK,MAAM,QAAQ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ACd,MAAM,QAAsB,CAAC;CAC7B,IAAI,SAAS;CACb,OAAO,SAAS,KAAK;EACnB,MAAM,CAAC,OAAO,YAAY,YAAY,OAAO,MAAM;EACnD,MAAM,KAAK,KAAK;EAChB,UAAU;CACZ;CACA,IAAI,WAAW,KACb,MAAM,IAAI,MACR,0CACF;CAEF,OAAO;AACT;AAEA,SAAS,YAAY,OAA2B;CAC9C,IAAI,SAAS;CACb,KAAK,MAAM,QAAQ,OACjB,SAAS,SAAS,MAAQ;CAE5B,OAAO;AACT;AAEA,SAAS,iBACP,OACA,KACM;CACN,IAAI,MAAM,MAAM,QACd,MAAM,IAAI,MAAM,oCAAoC;AAExD"} | ||
| {"version":3,"file":"rlp.js","names":[],"sources":["../src/rlp.ts"],"sourcesContent":["// https://ethereum.org/en/developers/docs/data-structures-and-encoding/rlp/\n// Recursive Length Prefix encoding. Used by every Ethereum\n// transaction type for the canonical serialized form.\n\nimport { hex_to_bytes } from \"./hex-to-bytes\"\n\nexport type RlpInput =\n | string\n | number\n | bigint\n | Uint8Array\n | RlpInput[]\n\nexport function rlp_encode(input: RlpInput): Uint8Array {\n if (Array.isArray(input)) return encode_list(input)\n return encode_item(input)\n}\n\nfunction encode_item(\n item: string | number | bigint | Uint8Array,\n): Uint8Array {\n const bytes = to_bytes(item)\n const first = bytes[0]\n if (\n bytes.length === 1 &&\n first !== undefined &&\n first < 0x80\n ) {\n return bytes\n }\n if (bytes.length <= 55) {\n const out = new Uint8Array(1 + bytes.length)\n out[0] = 0x80 + bytes.length\n out.set(bytes, 1)\n return out\n }\n const length_bytes = encode_length(bytes.length)\n const out = new Uint8Array(\n 1 + length_bytes.length + bytes.length,\n )\n out[0] = 0xb7 + length_bytes.length\n out.set(length_bytes, 1)\n out.set(bytes, 1 + length_bytes.length)\n return out\n}\n\nfunction encode_list(list: RlpInput[]): Uint8Array {\n const items = list.map(rlp_encode)\n const total = items.reduce((s, i) => s + i.length, 0)\n if (total <= 55) {\n const out = new Uint8Array(1 + total)\n out[0] = 0xc0 + total\n let offset = 1\n for (const item of items) {\n out.set(item, offset)\n offset += item.length\n }\n return out\n }\n const length_bytes = encode_length(total)\n const out = new Uint8Array(\n 1 + length_bytes.length + total,\n )\n out[0] = 0xf7 + length_bytes.length\n out.set(length_bytes, 1)\n let offset = 1 + length_bytes.length\n for (const item of items) {\n out.set(item, offset)\n offset += item.length\n }\n return out\n}\n\nfunction to_bytes(\n input: string | number | bigint | Uint8Array,\n): Uint8Array {\n if (input instanceof Uint8Array) return input\n if (typeof input === \"string\") {\n if (input.startsWith(\"0x\")) return hex_to_bytes(input)\n return new TextEncoder().encode(input)\n }\n return number_to_bytes(BigInt(input))\n}\n\nfunction number_to_bytes(big: bigint): Uint8Array {\n if (big === 0n) return new Uint8Array([])\n const hex = big.toString(16)\n const padded = hex.padStart(\n hex.length + (hex.length % 2),\n \"0\",\n )\n return hex_to_bytes(padded)\n}\n\nfunction encode_length(length: number): Uint8Array {\n if (length === 0) return new Uint8Array([])\n const out: number[] = []\n let temp = length\n while (temp > 0) {\n out.unshift(temp & 0xff)\n temp >>= 8\n }\n return new Uint8Array(out)\n}\n\n// RLP only preserves bytes and nesting — numbers and strings\n// fed into `rlp_encode` come back as byte payloads. Callers\n// reapply meaning (number, address, hash, …) from the\n// surrounding schema.\nexport type RlpDecoded = Uint8Array | RlpDecoded[]\n\nexport function rlp_decode(bytes: Uint8Array): RlpDecoded {\n const [value, consumed] = decode_item(bytes, 0)\n if (consumed !== bytes.length) {\n throw new Error(\n `rlp_decode: ${bytes.length - consumed} trailing byte(s)`,\n )\n }\n return value\n}\n\nfunction decode_item(\n bytes: Uint8Array,\n offset: number,\n): [RlpDecoded, number] {\n if (offset >= bytes.length) {\n throw new Error(\"rlp_decode: unexpected end of input\")\n }\n const prefix = bytes[offset]\n if (prefix === undefined) {\n throw new Error(\"rlp_decode: unexpected end of input\")\n }\n if (prefix < 0x80) {\n return [bytes.slice(offset, offset + 1), 1]\n }\n if (prefix <= 0xb7) {\n const length = prefix - 0x80\n const start = offset + 1\n const end = start + length\n assert_in_bounds(bytes, end)\n return [bytes.slice(start, end), 1 + length]\n }\n if (prefix <= 0xbf) {\n const length_of_length = prefix - 0xb7\n const length_start = offset + 1\n const length_end = length_start + length_of_length\n assert_in_bounds(bytes, length_end)\n const length = read_length(\n bytes.subarray(length_start, length_end),\n )\n const start = length_end\n const end = start + length\n assert_in_bounds(bytes, end)\n return [\n bytes.slice(start, end),\n 1 + length_of_length + length,\n ]\n }\n if (prefix <= 0xf7) {\n const length = prefix - 0xc0\n const start = offset + 1\n const end = start + length\n assert_in_bounds(bytes, end)\n return [\n decode_list_payload(bytes, start, end),\n 1 + length,\n ]\n }\n const length_of_length = prefix - 0xf7\n const length_start = offset + 1\n const length_end = length_start + length_of_length\n assert_in_bounds(bytes, length_end)\n const length = read_length(\n bytes.subarray(length_start, length_end),\n )\n const start = length_end\n const end = start + length\n assert_in_bounds(bytes, end)\n return [\n decode_list_payload(bytes, start, end),\n 1 + length_of_length + length,\n ]\n}\n\nfunction decode_list_payload(\n bytes: Uint8Array,\n start: number,\n end: number,\n): RlpDecoded[] {\n const items: RlpDecoded[] = []\n let cursor = start\n while (cursor < end) {\n const [value, consumed] = decode_item(bytes, cursor)\n items.push(value)\n cursor += consumed\n }\n if (cursor !== end) {\n throw new Error(\n \"rlp_decode: list payload length mismatch\",\n )\n }\n return items\n}\n\nfunction read_length(bytes: Uint8Array): number {\n let length = 0\n for (const byte of bytes) {\n length = length * 0x100 + byte\n }\n return length\n}\n\nfunction assert_in_bounds(\n bytes: Uint8Array,\n end: number,\n): void {\n if (end > bytes.length) {\n throw new Error(\"rlp_decode: payload overruns input\")\n }\n}\n"],"mappings":";;;AAaA,SAAgB,WAAW,OAA6B;CACtD,IAAI,MAAM,QAAQ,KAAK,GAAG,OAAO,YAAY,KAAK;CAClD,OAAO,YAAY,KAAK;AAC1B;AAEA,SAAS,YACP,MACY;CACZ,MAAM,QAAQ,SAAS,IAAI;CAC3B,MAAM,QAA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AAS,YACb,MAAM,SAAS,cAAc,UAAU,CACzC;EACA,MAAM,QAAQ;EACd,MAAM,MAAM,QAAQ;EACpB,iBAAiB,OAAO,GAAG;EAC3B,OAAO,CACL,MAAM,MAAM,OAAO,GAAG,GACtB,IAAI,mBAAmB,MACzB;CACF;CACA,IAAI,UAAU,KAAM;EAClB,MAAM,SAAS,SAAS;EACxB,MAAM,QAAQ,SAAS;EACvB,MAAM,MAAM,QAAQ;EACpB,iBAAiB,OAAO,GAAG;EAC3B,OAAO,CACL,oBAAoB,OAAO,OAAO,GAAG,GACrC,IAAI,MACN;CACF;CACA,MAAM,mBAAmB,SAAS;CAClC,MAAM,eAAe,SAAS;CAC9B,MAAM,aAAa,eAAe;CAClC,iBAAiB,OAAO,UAAU;CAClC,MAAM,SAAS,YACb,MAAM,SAAS,cAAc,UAAU,CACzC;CACA,MAAM,QAAQ;CACd,MAAM,MAAM,QAAQ;CACpB,iBAAiB,OAAO,GAAG;CAC3B,OAAO,CACL,oBAAoB,OAAO,OAAO,GAAG,GACrC,IAAI,mBAAmB,MACzB;AACF;AAEA,SAAS,oBACP,OACA,OACA,KACc;CACd,MAAM,QAAsB,CAAC;CAC7B,IAAI,SAAS;CACb,OAAO,SAAS,KAAK;EACnB,MAAM,CAAC,OAAO,YAAY,YAAY,OAAO,MAAM;EACnD,MAAM,KAAK,KAAK;EAChB,UAAU;CACZ;CACA,IAAI,WAAW,KACb,MAAM,IAAI,MACR,0CACF;CAEF,OAAO;AACT;AAEA,SAAS,YAAY,OAA2B;CAC9C,IAAI,SAAS;CACb,KAAK,MAAM,QAAQ,OACjB,SAAS,SAAS,MAAQ;CAE5B,OAAO;AACT;AAEA,SAAS,iBACP,OACA,KACM;CACN,IAAI,MAAM,MAAM,QACd,MAAM,IAAI,MAAM,oCAAoC;AAExD"} |
+1
-1
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"unit.js","names":[],"sources":["../src/unit.ts"],"sourcesContent":["export function format_unit(\n value: bigint,\n decimals = 18,\n): string {\n if (value === 0n) {\n return \"0\"\n }\n const negative = value < 0n\n const abs = negative ? -value : value\n const base = 10n ** BigInt(decimals)\n const integer_part = abs / base\n const fraction_part = abs % base\n const sign = negative ? \"-\" : \"\"\n if (fraction_part === 0n) {\n return `${sign}${integer_part.toString()}`\n }\n const fraction = fraction_part\n .toString()\n .padStart(decimals, \"0\")\n .replace(/0+$/, \"\")\n return `${sign}${integer_part.toString()}.${fraction}`\n}\n\nexport function format_ether(value: bigint): string {\n return format_unit(value, 18)\n}\n\nexport function format_gwei(value: bigint): string {\n return format_unit(value, 9)\n}\n\nexport function parse_ether(value: string): bigint {\n return parse_unit(value, 18)\n}\n\nexport function parse_gwei(value: string): bigint {\n return parse_unit(value, 9)\n}\n\nexport function parse_unit(\n value: string,\n decimals = 18,\n): bigint {\n if (value === \"\") {\n throw new Error(\"parse_unit: empty string\")\n }\n const negative = value.startsWith(\"-\")\n const unsigned = negative ? value.slice(1) : value\n if (!/^(\\d+\\.?\\d*|\\.\\d+)$/.test(unsigned)) {\n throw new Error(`parse_unit: invalid number \"${value}\"`)\n }\n const [integer_str, fraction_str = \"\"] =\n unsigned.split(\".\")\n if (fraction_str.length > decimals) {\n throw new Error(\n `parse_unit: \"${value}\" has more than ${decimals} fractional digits`,\n )\n }\n const integer =\n !integer_str || integer_str === \"\" ? \"0\" : integer_str\n const padded = fraction_str.padEnd(decimals, \"0\")\n const base = 10n ** BigInt(decimals)\n const fraction_bigint =\n padded === \"\" ? 0n : BigInt(padded)\n const result = BigInt(integer) * base + fraction_bigint\n return negative ? -result : result\n}\n"],"mappings":";AAAA,SAAgB,YACd,OACA,WAAW,IACH;CACR,IAAI,UAAU,IACZ,OAAO;CAET,MAAM,WAAW,QAAQ;CACzB,MAAM,MAAM,WAAW,CAAC,QAAQ;CAChC,MAAM,OAAO,OAAO,OAAO,QAAQ;CACnC,MAAM,eAAe,MAAM;CAC3B,MAAM,gBAAgB,MAAM;CAC5B,MAAM,OAAO,WAAW,MAAM;CAC9B,IAAI,kBAAkB,IACpB,OAAO,GAAG,OAAO,aAAa,SAAS;CAEzC,MAAM,WAAW,cACd,SAAS,EACT,SAAS,UAAU,GAAG,EACtB,QAAQ,OAAO,EAAE;CACpB,OAAO,GAAG,OAAO,aAAa,SAAS,EAAE,GAAG;AAC9C;AAEA,SAAgB,aAAa,OAAuB;CAClD,OAAO,YAAY,OAAO,EAAE;AAC9B;AAEA,SAAgB,YAAY,OAAuB;CACjD,OAAO,YAAY,OAAO,CAAC;AAC7B;AAEA,SAAgB,YAAY,OAAuB;CACjD,OAAO,WAAW,OAAO,EAAE;AAC7B;AAEA,SAAgB,WAAW,OAAuB;CAChD,OAAO,WAAW,OAAO,CAAC;AAC5B;AAEA,SAAgB,WACd,OACA,WAAW,IACH;CACR,IAAI,UAAU,IACZ,MAAM,IAAI,MAAM,0BAA0B;CAE5C,MAAM,WAAW,MAAM,WAAW,GAAG;CACrC,MAAM,WAAW,WAAW,MAAM,MAAM,CAAC,IAAI;CAC7C,IAAI,CAAC,sBAAsB,KAAK,QAAQ,GACtC,MAAM,IAAI,MAAM,+BAA+B,MAAM,EAAE;CAEzD,MAAM,CAAC,aAAa,eAAe,MACjC,SAAS,MAAM,GAAG;CACpB,IAAI,aAAa,SAAS,UACxB,MAAM,IAAI,MACR,gBAAgB,MAAM,kBAAkB,SAAS,mBACnD;CAEF,MAAM,UACJ,CAAC,eAAe,gBAAgB,KAAK,MAAM;CAC7C,MAAM,SAAS,aAAa,OAAO,UAAU,GAAG;CAChD,MAAM,OAAO,OAAO,OAAO,QAAQ;CACnC,MAAM,kBACJ,WAAW,KAAK,KAAK,OAAO,MAAM;CACpC,MAAM,SAAS,OAAO,OAAO,IAAI,OAAO;CACxC,OAAO,WAAW,CAAC,SAAS;AAC9B"} | ||
| {"version":3,"file":"unit.js","names":[],"sources":["../src/unit.ts"],"sourcesContent":["export function format_unit(\n value: bigint,\n decimals = 18,\n): string {\n if (value === 0n) {\n return \"0\"\n }\n const negative = value < 0n\n const abs = negative ? -value : value\n const base = 10n ** BigInt(decimals)\n const integer_part = abs / base\n const fraction_part = abs % base\n const sign = negative ? \"-\" : \"\"\n if (fraction_part === 0n) {\n return `${sign}${integer_part.toString()}`\n }\n const fraction = fraction_part\n .toString()\n .padStart(decimals, \"0\")\n .replace(/0+$/, \"\")\n return `${sign}${integer_part.toString()}.${fraction}`\n}\n\nexport function format_ether(value: bigint): string {\n return format_unit(value, 18)\n}\n\nexport function format_gwei(value: bigint): string {\n return format_unit(value, 9)\n}\n\nexport function parse_ether(value: string): bigint {\n return parse_unit(value, 18)\n}\n\nexport function parse_gwei(value: string): bigint {\n return parse_unit(value, 9)\n}\n\nexport function parse_unit(\n value: string,\n decimals = 18,\n): bigint {\n if (value === \"\") {\n throw new Error(\"parse_unit: empty string\")\n }\n const negative = value.startsWith(\"-\")\n const unsigned = negative ? value.slice(1) : value\n if (!/^(\\d+\\.?\\d*|\\.\\d+)$/.test(unsigned)) {\n throw new Error(`parse_unit: invalid number \"${value}\"`)\n }\n const [integer_str, fraction_str = \"\"] =\n unsigned.split(\".\")\n if (fraction_str.length > decimals) {\n throw new Error(\n `parse_unit: \"${value}\" has more than ${decimals} fractional digits`,\n )\n }\n const integer =\n !integer_str || integer_str === \"\" ? \"0\" : integer_str\n const padded = fraction_str.padEnd(decimals, \"0\")\n const base = 10n ** BigInt(decimals)\n const fraction_bigint =\n padded === \"\" ? 0n : BigInt(padded)\n const result = BigInt(integer) * base + fraction_bigint\n return negative ? -result : result\n}\n"],"mappings":";AAAA,SAAgB,YACd,OACA,WAAW,IACH;CACR,IAAI,UAAU,IACZ,OAAO;CAET,MAAM,WAAW,QAAQ;CACzB,MAAM,MAAM,WAAW,CAAC,QAAQ;CAChC,MAAM,OAAO,OAAO,OAAO,QAAQ;CACnC,MAAM,eAAe,MAAM;CAC3B,MAAM,gBAAgB,MAAM;CAC5B,MAAM,OAAO,WAAW,MAAM;CAC9B,IAAI,kBAAkB,IACpB,OAAO,GAAG,OAAO,aAAa,SAAS;CAEzC,MAAM,WAAW,cACd,SAAS,CAAC,CACV,SAAS,UAAU,GAAG,CAAC,CACvB,QAAQ,OAAO,EAAE;CACpB,OAAO,GAAG,OAAO,aAAa,SAAS,EAAE,GAAG;AAC9C;AAEA,SAAgB,aAAa,OAAuB;CAClD,OAAO,YAAY,OAAO,EAAE;AAC9B;AAEA,SAAgB,YAAY,OAAuB;CACjD,OAAO,YAAY,OAAO,CAAC;AAC7B;AAEA,SAAgB,YAAY,OAAuB;CACjD,OAAO,WAAW,OAAO,EAAE;AAC7B;AAEA,SAAgB,WAAW,OAAuB;CAChD,OAAO,WAAW,OAAO,CAAC;AAC5B;AAEA,SAAgB,WACd,OACA,WAAW,IACH;CACR,IAAI,UAAU,IACZ,MAAM,IAAI,MAAM,0BAA0B;CAE5C,MAAM,WAAW,MAAM,WAAW,GAAG;CACrC,MAAM,WAAW,WAAW,MAAM,MAAM,CAAC,IAAI;CAC7C,IAAI,CAAC,sBAAsB,KAAK,QAAQ,GACtC,MAAM,IAAI,MAAM,+BAA+B,MAAM,EAAE;CAEzD,MAAM,CAAC,aAAa,eAAe,MACjC,SAAS,MAAM,GAAG;CACpB,IAAI,aAAa,SAAS,UACxB,MAAM,IAAI,MACR,gBAAgB,MAAM,kBAAkB,SAAS,mBACnD;CAEF,MAAM,UACJ,CAAC,eAAe,gBAAgB,KAAK,MAAM;CAC7C,MAAM,SAAS,aAAa,OAAO,UAAU,GAAG;CAChD,MAAM,OAAO,OAAO,OAAO,QAAQ;CACnC,MAAM,kBACJ,WAAW,KAAK,KAAK,OAAO,MAAM;CACpC,MAAM,SAAS,OAAO,OAAO,IAAI,OAAO;CACxC,OAAO,WAAW,CAAC,SAAS;AAC9B"} |
+14
-1
| { | ||
| "$schema": "https://json.schemastore.org/package.json", | ||
| "name": "@ethernauta/utils", | ||
| "version": "0.0.48", | ||
| "description": "Pure dependency-free helpers for Ethernauta — hex, bytes, BigInt, type guards.", | ||
| "keywords": [ | ||
| "ethereum", | ||
| "utils", | ||
| "hex", | ||
| "bytes", | ||
| "bigint" | ||
| ], | ||
| "homepage": "https://ethernauta-docs.pages.dev/", | ||
| "bugs": { | ||
| "url": "https://github.com/niconiahi/ethernauta/issues" | ||
| }, | ||
| "license": "MIT", | ||
| "type": "module", | ||
| "version": "0.0.47", | ||
| "publishConfig": { | ||
@@ -7,0 +20,0 @@ "access": "public" |
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No License Found
LicenseLicense information could not be found.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
60865
2.42%77
1.32%0
-100%1
-50%1
-50%