{"_id":"@bangbu/decimaljs-crypto-extensions","_rev":"2-ebd134a39ae2187a7530acf7e776c82c","name":"@bangbu/decimaljs-crypto-extensions","dist-tags":{"latest":"0.0.1-alpha.2"},"versions":{"0.0.1-alpha.1":{"name":"@bangbu/decimaljs-crypto-extensions","version":"0.0.1-alpha.1","keywords":["decimal.js","crypto","extensions"],"author":{"name":"BangBu","email":"68235687+bangbu10820@users.noreply.github.com"},"license":"MIT","_id":"@bangbu/decimaljs-crypto-extensions@0.0.1-alpha.1","maintainers":[{"name":"bangbu","email":"daoqtoan@gmail.com"}],"homepage":"https://github.com/bangbu10820/decimaljs-crypto-extensions#readme","bugs":{"url":"https://github.com/bangbu10820/decimaljs-crypto-extensions/issues"},"dist":{"shasum":"3900854d36b1db9ccd8fb528d012a52b1db820f6","tarball":"https://registry.npmjs.org/@bangbu/decimaljs-crypto-extensions/-/decimaljs-crypto-extensions-0.0.1-alpha.1.tgz","fileCount":23,"integrity":"sha512-5sdQttezODLfW9kiJanlOJlspcPe4FY6om9YKrSe2np5mfZG+Lv0K/wYmUJYjRfrEsZjljgZ/YtUrbVtEGWi6w==","signatures":[{"sig":"MEUCIQDJ5o3Lf9nX4gJcZOm0tQDsNg0H0qlnSoxKlasno3uZ7gIgOy2qCUlIX1vxmqjigicC32st04Ir1T5j2IO5jc0R0tY=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":10112},"main":"dist/cjs/index.js","type":"module","types":"dist/types/index.d.ts","exports":{"types":"./dist/types/index.d.ts","import":"./dist/esm/index.js","default":"./dist/esm/index.js","require":"./dist/cjs/index.js"},"scripts":{"dev":"tsc --project tsconfig.esm.json && tsc --project tsconfig.cjs.json --watch","build":"rimraf dist && tsc --project tsconfig.esm.json && tsc --project tsconfig.cjs.json"},"_npmUser":{"name":"bangbu","email":"daoqtoan@gmail.com"},"repository":{"url":"git+https://github.com/bangbu10820/decimaljs-crypto-extensions.git"},"_npmVersion":"10.7.0","description":"Decimal.js crypto extension","directories":{},"_nodeVersion":"18.20.4","publishConfig":{"access":"public"},"_hasShrinkwrap":false,"devDependencies":{"rimraf":"^6.0.1"},"peerDependencies":{"decimal.js":"10","typescript":"^5.8.3"},"peerDependenciesMeta":{"typescript":{"optional":true}},"_npmOperationalInternal":{"tmp":"tmp/decimaljs-crypto-extensions_0.0.1-alpha.1_1749183534905_0.28694056089034814","host":"s3://npm-registry-packages-npm-production"}},"0.0.1-alpha.2":{"name":"@bangbu/decimaljs-crypto-extensions","description":"Decimal.js crypto extension","version":"0.0.1-alpha.2","type":"module","main":"dist/cjs/index.js","types":"dist/types/index.d.ts","repository":{"url":"git+https://github.com/bangbu10820/decimaljs-crypto-extensions.git"},"exports":{".":{"import":"./dist/esm/index.js","require":"./dist/cjs/index.js","types":"./dist/types/index.d.ts"},"./extensions/toBigInt":{"import":"./dist/esm/extensions/toBigInt.js","require":"./dist/cjs/extensions/toBigInt.js","types":"./dist/types/extensions/toBigInt.d.ts"},"./extensions/formatUnits":{"import":"./dist/esm/extensions/formatUnits.js","require":"./dist/cjs/extensions/formatUnits.js","types":"./dist/types/extensions/formatUnits.d.ts"}},"scripts":{"build":"rimraf dist && tsc --project tsconfig.esm.json && tsc --project tsconfig.cjs.json","dev":"tsc --project tsconfig.esm.json && tsc --project tsconfig.cjs.json --watch"},"keywords":["decimal.js","crypto","extensions"],"author":{"name":"BangBu","email":"68235687+bangbu10820@users.noreply.github.com"},"license":"MIT","devDependencies":{"rimraf":"^6.0.1"},"publishConfig":{"access":"public"},"peerDependencies":{"typescript":"^5.8.3","decimal.js":"10"},"peerDependenciesMeta":{"typescript":{"optional":true}},"_id":"@bangbu/decimaljs-crypto-extensions@0.0.1-alpha.2","bugs":{"url":"https://github.com/bangbu10820/decimaljs-crypto-extensions/issues"},"homepage":"https://github.com/bangbu10820/decimaljs-crypto-extensions#readme","_nodeVersion":"18.20.4","_npmVersion":"10.7.0","dist":{"integrity":"sha512-qM+cb7VdsdX18CwrHG7axZHt60S83C5AYL1pb8FCwlEcT1Chu9nm7QjdAn0SPSwZbNW7HAgvmEJe4Af31t+E1w==","shasum":"a2eef766e71cc43c0cbf8f8b17f6949ce8641cd1","tarball":"https://registry.npmjs.org/@bangbu/decimaljs-crypto-extensions/-/decimaljs-crypto-extensions-0.0.1-alpha.2.tgz","fileCount":23,"unpackedSize":11623,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEYCIQDpnV41rW0mdE0bMhieRE1cm2amYBkltiqHXXOwcYwe5gIhAOH0i4UByiJ+X2kJwSXfy0wXK8tZxzHsb7A6y2mczByO"}]},"_npmUser":{"name":"bangbu","email":"daoqtoan@gmail.com"},"directories":{},"maintainers":[{"name":"bangbu","email":"daoqtoan@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/decimaljs-crypto-extensions_0.0.1-alpha.2_1749184696917_0.04043765591101778"},"_hasShrinkwrap":false}},"time":{"created":"2025-06-06T04:18:54.534Z","modified":"2025-06-06T04:38:17.280Z","0.0.1-alpha.1":"2025-06-06T04:18:55.090Z","0.0.1-alpha.2":"2025-06-06T04:38:17.086Z"},"bugs":{"url":"https://github.com/bangbu10820/decimaljs-crypto-extensions/issues"},"author":{"name":"BangBu","email":"68235687+bangbu10820@users.noreply.github.com"},"license":"MIT","homepage":"https://github.com/bangbu10820/decimaljs-crypto-extensions#readme","keywords":["decimal.js","crypto","extensions"],"repository":{"url":"git+https://github.com/bangbu10820/decimaljs-crypto-extensions.git"},"description":"Decimal.js crypto extension","maintainers":[{"name":"bangbu","email":"daoqtoan@gmail.com"}],"readme":"# @bangbu/decimaljs-crypto-extensions\n\n[![npm version](https://img.shields.io/npm/v/@bangbu/decimaljs-crypto-extensions.svg)](https://www.npmjs.com/package/@bangbu/decimaljs-crypto-extensions)\n[![MIT License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)\n\nA lightweight, zero-dependency package that extends the `decimal.js` library with utility functions commonly needed in cryptocurrency applications. It uses monkey-patching to add new methods directly to the `Decimal` class, making them seamlessly available throughout your project.\n\n## Features\n\n-   Immutable operations that align with the design principles of `decimal.js`.\n-   Adds instance method `toBigInt()` for easy conversion to JavaScript's native `bigint`.\n-   Adds static method `formatUnits()` for handling token unit conversions, similar to libraries like Ethers.js.\n\n## Installation\n\nInstall the package and its peer dependency, `decimal.js`.\n\n```bash\nnpm install decimal.js @bangbu/decimaljs-crypto-extensions\n```\n\nOr using yarn:\n\n```bash\nyarn add decimal.js @bangbu/decimaljs-crypto-extensions\n```\n\n## Usage\n\nYou can choose to either import all extensions at once or select only the specific ones you need for your project.\n\n### Basic Usage: All-in-One\n\nTo activate all extensions, simply import the package **once** in your application's entry point (e.g., `index.ts` or `main.ts`). This single import will apply all available patches to the `Decimal` class.\n\n**1. Apply the patch:**\n\n```typescript\n// In your project's main entry file (e.g., index.ts)\nimport '@bangbu/decimaljs-crypto-extensions';\n\n// That's it! All extensions are now available on the Decimal object.\n```\n\n**2. Use the extended methods:**\n\nIn any other file in your project, you can import `Decimal` directly from `decimal.js` as you normally would.\n\n```typescript\nimport { Decimal } from 'decimal.js';\n\n// Now you can use the extended methods\nconst bigNumber = Decimal.formatUnits('123450000000000000000', 18); // 123.45\nconst asBigInt = new Decimal('98765').toBigInt();\n```\n\n### Advanced Usage: Selective Imports\n\nIf you only need a specific extension and want to minimize your bundle size, you can import extensions individually. This approach only applies the patch for the feature you import.\n\n**Importing `toBigInt` only:**\n```typescript\nimport '@bangbu/decimaljs-crypto-extensions/extensions/toBigInt';\nimport { Decimal } from 'decimal.js';\n\nconst myNumber = new Decimal('12345');\nconsole.log(myNumber.toBigInt()); // Works!\n\n// Decimal.formatUnits(..); // This would throw a TypeError because it was not imported.\n```\n\n**Importing `formatUnits` only:**\n```typescript\nimport '@bangbu/decimaljs-crypto-extensions/extensions/formatUnits';\nimport { Decimal } from 'decimal.js';\n\nconst formatted = Decimal.formatUnits('123e18', 18);\nconsole.log(formatted.toString()); // Works!\n\n// const d = new Decimal('123');\n// d.toBigInt(); // This would throw a TypeError because it was not imported.\n```\n\n## API Reference\n\n### `toBigInt(force?: boolean): bigint`\n\nAn **instance method** that converts a `Decimal` value to a native JavaScript `bigint`.\n\n-   If the `Decimal` value is a whole number, it converts directly.\n-   If the `Decimal` value has fractional parts, it will throw an error to prevent accidental precision loss.\n-   **`force`** (optional `boolean`, default `false`): If set to `true`, it will truncate the decimal part and convert the integer part to a `bigint` without throwing an error.\n\n**Examples:**\n\n```typescript\nimport { Decimal } from 'decimal.js';\n\n// Simple conversion\nconst intDecimal = new Decimal('12345678901234567890');\nconst bigIntValue = intDecimal.toBigInt();\n// => 12345678901234567890n\n\n// Throws an error by default for non-integers\nconst nonIntDecimal = new Decimal('123.45');\ntry {\n  nonIntDecimal.toBigInt();\n} catch (e) {\n  console.error(e.message); // => \"Decimal value is not an integer.\"\n}\n\n// Use `force` to truncate the decimal part\nconst forcedBigInt = nonIntDecimal.toBigInt(true);\n// => 123n\n```\n\n### `Decimal.formatUnits(value, decimals): Decimal`\n\nA **static method** to convert a value from its smallest unit (e.g., wei) into its standard, human-readable representation (e.g., ether) by dividing it by 10 to the power of `decimals`.\n\n-   **`value`**: The amount in its smallest unit. Can be a `bigint`, `string`, or `number`.\n-   **`decimals`**: The number of decimal places the token uses (e.g., `18` for ETH, `6` for USDC).\n\n**Examples:**\n\n```typescript\nimport { Decimal } from 'decimal.js';\n\n// Example 1: Convert an amount of a token with 18 decimals\nconst tokenAmountInWei = '123450000000000000000'; // Represents 123.45 tokens\nconst formattedAmount = Decimal.formatUnits(tokenAmountInWei, 18);\nconsole.log(formattedAmount.toString()); // => \"123.45\"\n\n// Example 2: Convert a BigInt value for a token with 9 decimals\nconst gweiAmount = 420000000000n;\nconst formattedGwei = Decimal.formatUnits(gweiAmount, 9);\nconsole.log(formattedGwei.toString()); // => \"420\"\n```\n\n## License\n\nThis project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.\n","readmeFilename":"README.md"}