Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# smart-round

![NPM Version](https://img.shields.io/npm/v/smart-round)
![NPM Version](https://img.shields.io/npm/v/smart-round)![npm bundle size](https://img.shields.io/bundlephobia/minzip/smart-round)

Round big numbers with arbitrary precision.

Expand Down
26 changes: 19 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 4 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "smart-round",
"version": "2.0.0",
"version": "3.0.0",
"description": "Round big numbers with arbitrary precision",
"keywords": [
"bignumber",
"big.js",
"decimals",
"precision",
"round",
Expand Down Expand Up @@ -40,10 +40,11 @@
"test": "vitest run"
},
"dependencies": {
"bignumber.js": "9.1.2"
"big.js": "7.0.1"
},
"devDependencies": {
"@commitlint/cli": "19.7.1",
"@types/big.js": "6.2.2",
"@vitest/coverage-v8": "3.0.6",
"better-sort-package-json": "1.1.0",
"commitlint-config-bloq": "1.1.0",
Expand All @@ -62,10 +63,6 @@
"import": "./_esm/index.js",
"require": "./_cjs/index.js",
"types": "./_types/index.d.ts"
},
"./contracts": {
"import": "./_esm/contracts/index.js",
"types": "./_types/contracts/index.d.ts"
}
},
"module": "./_esm/index.js",
Expand Down
76 changes: 52 additions & 24 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,86 @@
import { BigNumber } from "bignumber.js";
import Big, { BigSource } from "big.js";

import { RoundingMode, toBigNumberRoundingModes } from "./roundingModes.js";
import { RoundingMode, toBigJsRoundingModes } from "./roundingModes.js";

type Options = {
locale?: string;
roundingMode?: RoundingMode;
shouldFormat?: boolean;
};

const getDecimalPlaces = (bigJsNumber: Big) =>
bigJsNumber.toFixed().split(".")[1]?.length ?? 0;

const parse = function ({
input,
maxDecimals,
maxPrecision,
minDecimals,
}: {
input: BigSource;
maxDecimals: number;
maxPrecision: number;
minDecimals: number;
}) {
if (maxPrecision <= 0) {
throw new Error("maxPrecision should be positive larger than 1");
}
if (minDecimals < 0) {
throw new Error("minDecimals should be positive");
}
if (maxDecimals < 0) {
throw new Error("maxDecimals should be positive");
}
if (minDecimals > maxDecimals) {
throw new Error("minDecimals should be larger than maxDecimals");
}
if (typeof input === "number" && (isNaN(input) || !isFinite(input))) {
throw new Error("input should be a valid number");
}

try {
const bigJsNumber = new Big(input.toString().replaceAll(",", ""));
return bigJsNumber;
} catch {
throw new Error("input should be a valid number");
}
};

export const smartRound = (
maxPrecision: number,
minDecimals: number,
maxDecimals: number,
) =>
function (input: BigNumber.Value, options?: Options) {
function (input: BigSource, options?: Options) {
const {
locale = "en-US",
// Big Number default's, but just to make it explicit
roundingMode = "round-half-up",
shouldFormat = false,
} = options ?? {};

if (maxPrecision <= 0) {
throw new Error("maxPrecision should be positive larger than 1");
}
if (minDecimals < 0) {
throw new Error("minDecimals should be positive");
}
if (maxDecimals < 0) {
throw new Error("maxDecimals should be positive");
}
if (minDecimals > maxDecimals) {
throw new Error("minDecimals should be larger than maxDecimals");
}

const bn = new BigNumber(input);
const bigJsNumber = parse({
input,
maxDecimals,
maxPrecision,
minDecimals,
});

if (bn.isNaN() || !bn.isFinite()) {
throw new Error("input should be a valid number");
}
// Calculate how much to reduce the precision
const adjustment = Math.max(bn.precision() - maxPrecision, 0);
// The array of digits is the precision
const adjustment = Math.max(bigJsNumber.c.length - maxPrecision, 0);

// Calculate how much to reduce the decimals
const decimals = Math.max(
(bn.decimalPlaces() ?? 0) - adjustment,
getDecimalPlaces(bigJsNumber) - adjustment,
minDecimals,
);

const newDecimals = Math.min(decimals, maxDecimals);

const string = bn.toFixed(
const string = bigJsNumber.toFixed(
newDecimals,
toBigNumberRoundingModes(roundingMode),
toBigJsRoundingModes(roundingMode),
);

if (!shouldFormat) {
Expand Down
15 changes: 5 additions & 10 deletions src/roundingModes.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
import { BigNumber } from "bignumber.js";
import { RoundingMode as BigJsRoundingMode } from "big.js";

// Use this to allow easier configuration for consumers, without them needing
// to import BigNumber
// to import Big.js

const roundingModes = [
"round-up",
"round-down",
"round-ceil",
"round-floor",
"round-half-up",
"round-half-down",
"round-half-even",
"round-half-ceil",
"round-half-floor",
"round-up",
] as const;

export type RoundingMode = (typeof roundingModes)[number];

export const toBigNumberRoundingModes = (roundingMode: RoundingMode) =>
roundingModes.findIndex((r) => r === roundingMode)! as BigNumber.RoundingMode;
export const toBigJsRoundingModes = (roundingMode: RoundingMode) =>
roundingModes.findIndex((r) => r === roundingMode)! as BigJsRoundingMode;
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"compilerOptions": {
"allowJs": false,
"forceConsistentCasingInFileNames": true,
"esModuleInterop": true,
"incremental": true,
"module": "NodeNext",
"moduleResolution": "NodeNext",
Expand Down