mirror of
https://github.com/cosmos/cosmjs.git
synced 2025-03-10 21:49:15 +00:00
Implement formatDecimal and formatCoin
This commit is contained in:
parent
8466d537f3
commit
42dbcfb46d
26
packages/proto-signing/src/testdata/coins.json
vendored
Normal file
26
packages/proto-signing/src/testdata/coins.json
vendored
Normal file
@ -0,0 +1,26 @@
|
||||
[
|
||||
[{"amount": "1", "denom": "ucosm"}, { "denom": "COSM", "exponent": 6 }, "0.000001 COSM"],
|
||||
[{"amount": "10", "denom": "ucosm"}, { "denom": "COSM", "exponent": 6 }, "0.00001 COSM"],
|
||||
[{"amount": "100", "denom": "ucosm"}, { "denom": "COSM", "exponent": 6 }, "0.0001 COSM"],
|
||||
[{"amount": "1000", "denom": "ucosm"}, { "denom": "COSM", "exponent": 6 }, "0.001 COSM"],
|
||||
[{"amount": "10000", "denom": "ucosm"}, { "denom": "COSM", "exponent": 6 }, "0.01 COSM"],
|
||||
[{"amount": "100000", "denom": "ucosm"}, { "denom": "COSM", "exponent": 6 }, "0.1 COSM"],
|
||||
[{"amount": "1000000", "denom": "ucosm"}, { "denom": "COSM", "exponent": 6 }, "1.0 COSM"],
|
||||
[{"amount": "10000000", "denom": "ucosm"}, { "denom": "COSM", "exponent": 6 }, "10.0 COSM"],
|
||||
[{"amount": "1", "denom": "ucosm"}, { "denom": "COSM", "exponent": 2 }, "0.01 COSM"],
|
||||
[{"amount": "10", "denom": "ucosm"}, { "denom": "COSM", "exponent": 2 }, "0.1 COSM"],
|
||||
[{"amount": "100", "denom": "ucosm"}, { "denom": "COSM", "exponent": 2 }, "1.0 COSM"],
|
||||
[{"amount": "1000", "denom": "ucosm"}, { "denom": "COSM", "exponent": 2 }, "10.0 COSM"],
|
||||
[{"amount": "10000", "denom": "ucosm"}, { "denom": "COSM", "exponent": 2 }, "100.0 COSM"],
|
||||
[{"amount": "100000", "denom": "ucosm"}, { "denom": "COSM", "exponent": 2 }, "1'000.0 COSM"],
|
||||
[{"amount": "1000000", "denom": "ucosm"}, { "denom": "COSM", "exponent": 2 }, "10'000.0 COSM"],
|
||||
[{"amount": "10000000", "denom": "ucosm"}, { "denom": "COSM", "exponent": 2 }, "100'000.0 COSM"],
|
||||
[{"amount": "1", "denom": "point"}, { "denom": "POINT", "exponent": 0 }, "1 POINT"],
|
||||
[{"amount": "10", "denom": "point"}, { "denom": "POINT", "exponent": 0 }, "10 POINT"],
|
||||
[{"amount": "100", "denom": "point"}, { "denom": "POINT", "exponent": 0 }, "100 POINT"],
|
||||
[{"amount": "1000", "denom": "point"}, { "denom": "POINT", "exponent": 0 }, "1'000 POINT"],
|
||||
[{"amount": "10000", "denom": "point"}, { "denom": "POINT", "exponent": 0 }, "10'000 POINT"],
|
||||
[{"amount": "100000", "denom": "point"}, { "denom": "POINT", "exponent": 0 }, "100'000 POINT"],
|
||||
[{"amount": "1000000", "denom": "point"}, { "denom": "POINT", "exponent": 0 }, "1'000'000 POINT"],
|
||||
[{"amount": "10000000", "denom": "point"}, { "denom": "POINT", "exponent": 0 }, "10'000'000 POINT"]
|
||||
]
|
43
packages/proto-signing/src/testdata/decimals.json
vendored
Normal file
43
packages/proto-signing/src/testdata/decimals.json
vendored
Normal file
@ -0,0 +1,43 @@
|
||||
[
|
||||
["0", "0.0"],
|
||||
["1", "1.0"],
|
||||
["12", "12.0"],
|
||||
["123", "123.0"],
|
||||
["1234", "1'234.0"],
|
||||
["0.1", "0.1"],
|
||||
["0.01", "0.01"],
|
||||
["0.001", "0.001"],
|
||||
["0.0001", "0.0001"],
|
||||
["0.00001", "0.00001"],
|
||||
["0.000001", "0.000001"],
|
||||
["0.0000001", "0.0000001"],
|
||||
["0.00000001", "0.00000001"],
|
||||
["0.000000001", "0.000000001"],
|
||||
["0.0000000001", "0.0000000001"],
|
||||
["0.00000000001", "0.00000000001"],
|
||||
["0.000000000001", "0.000000000001"],
|
||||
["0.0000000000001", "0.0000000000001"],
|
||||
["0.00000000000001", "0.00000000000001"],
|
||||
["0.000000000000001", "0.000000000000001"],
|
||||
["0.0000000000000001", "0.0000000000000001"],
|
||||
["0.00000000000000001", "0.00000000000000001"],
|
||||
["0.000000000000000001", "0.000000000000000001"],
|
||||
["0.100000000000000000", "0.1"],
|
||||
["0.010000000000000000", "0.01"],
|
||||
["0.001000000000000000", "0.001"],
|
||||
["0.000100000000000000", "0.0001"],
|
||||
["0.000010000000000000", "0.00001"],
|
||||
["0.000001000000000000", "0.000001"],
|
||||
["0.000000100000000000", "0.0000001"],
|
||||
["0.000000010000000000", "0.00000001"],
|
||||
["0.000000001000000000", "0.000000001"],
|
||||
["0.000000000100000000", "0.0000000001"],
|
||||
["0.000000000010000000", "0.00000000001"],
|
||||
["0.000000000001000000", "0.000000000001"],
|
||||
["0.000000000000100000", "0.0000000000001"],
|
||||
["0.000000000000010000", "0.00000000000001"],
|
||||
["0.000000000000001000", "0.000000000000001"],
|
||||
["0.000000000000000100", "0.0000000000000001"],
|
||||
["0.000000000000000010", "0.00000000000000001"],
|
||||
["0.000000000000000001", "0.000000000000000001"]
|
||||
]
|
19
packages/proto-signing/src/testdata/integers.json
vendored
Normal file
19
packages/proto-signing/src/testdata/integers.json
vendored
Normal file
@ -0,0 +1,19 @@
|
||||
[
|
||||
["0", "0"],
|
||||
["1", "1"],
|
||||
["12", "12"],
|
||||
["123", "123"],
|
||||
["1234", "1'234"],
|
||||
["12345", "12'345"],
|
||||
["123456", "123'456"],
|
||||
["1234567", "1'234'567"],
|
||||
["9007199254740991", "9'007'199'254'740'991"],
|
||||
["9007199254740992", "9'007'199'254'740'992"],
|
||||
["18446744073709551615", "18'446'744'073'709'551'615"],
|
||||
["18446744073709551616", "18'446'744'073'709'551'616"],
|
||||
["340282366920938463463374607431768211455", "340'282'366'920'938'463'463'374'607'431'768'211'455"],
|
||||
["01", "1"],
|
||||
["001", "1"],
|
||||
["0001", "1"],
|
||||
["00001", "1"]
|
||||
]
|
@ -1,4 +1,12 @@
|
||||
import { formatInteger } from "./valuerenderers";
|
||||
import { DenomUnit } from "cosmjs-types/cosmos/bank/v1beta1/bank";
|
||||
import { Coin } from "cosmjs-types/cosmos/base/v1beta1/coin";
|
||||
|
||||
import coins from "../testdata/coins.json";
|
||||
import decimals from "../testdata/decimals.json";
|
||||
import integers from "../testdata/integers.json";
|
||||
import { formatCoin, formatDecimal, formatInteger } from "./valuerenderers";
|
||||
|
||||
type TestDataCoins = Array<[Coin, Pick<DenomUnit, "denom" | "exponent">, string]>;
|
||||
|
||||
describe("valuerenderers", () => {
|
||||
describe("formatInteger", () => {
|
||||
@ -10,6 +18,34 @@ describe("valuerenderers", () => {
|
||||
expect(formatInteger("12345")).toEqual("12'345");
|
||||
expect(formatInteger("123456")).toEqual("123'456");
|
||||
expect(formatInteger("1234567")).toEqual("1'234'567");
|
||||
|
||||
for (const [input, expected] of integers) {
|
||||
expect(formatInteger(input)).withContext(`Input '${input}'`).toEqual(expected);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("formatDecimal", () => {
|
||||
it("works", () => {
|
||||
expect(formatDecimal("1234.5678")).toEqual("1'234.5678");
|
||||
|
||||
for (const [input, expected] of decimals) {
|
||||
expect(formatDecimal(input)).withContext(`Input '${input}'`).toEqual(expected);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("formatCoin", () => {
|
||||
it("works", () => {
|
||||
expect(formatCoin({ amount: "1", denom: "ucosm" }, { denom: "COSM", exponent: 6 })).toEqual(
|
||||
"0.000001 COSM",
|
||||
);
|
||||
|
||||
for (const [coin, unit, expected] of coins as TestDataCoins) {
|
||||
expect(formatCoin(coin, unit))
|
||||
.withContext(`Input '${JSON.stringify(coin)}'`)
|
||||
.toEqual(expected);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -1,5 +1,19 @@
|
||||
import { Decimal } from "@cosmjs/math";
|
||||
import { DenomUnit } from "cosmjs-types/cosmos/bank/v1beta1/bank";
|
||||
import { Coin } from "cosmjs-types/cosmos/base/v1beta1/coin";
|
||||
|
||||
export function formatInteger(input: string): string {
|
||||
return Decimal.fromAtomics(input, 0).toString(".", "'");
|
||||
}
|
||||
|
||||
export function formatDecimal(input: string): string {
|
||||
let out = Decimal.fromUserInput(input, 18).toString(".", "'");
|
||||
if (out.indexOf(".") === -1) out += ".0";
|
||||
return out;
|
||||
}
|
||||
|
||||
export function formatCoin(input: Coin, unit: Pick<DenomUnit, "denom" | "exponent">): string {
|
||||
let out = Decimal.fromAtomics(input.amount, unit.exponent).toString(".", "'");
|
||||
if (unit.exponent !== 0 && out.indexOf(".") === -1) out += ".0";
|
||||
return out + " " + unit.denom;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user