mirror of
https://github.com/cosmos/cosmjs.git
synced 2025-03-10 21:49:15 +00:00
Add bytes formatting
This commit is contained in:
parent
67f41d7611
commit
fc6a954717
10
packages/proto-signing/src/testdata/bytes.json
vendored
Normal file
10
packages/proto-signing/src/testdata/bytes.json
vendored
Normal file
@ -0,0 +1,10 @@
|
||||
[
|
||||
[[], ""],
|
||||
[[0], "AA=="],
|
||||
[[0,0], "AAA="],
|
||||
[[0,0,0], "AAAA"],
|
||||
[[0,0,0,0], "AAAAAA=="],
|
||||
[[0,0,0,0,0], "AAAAAAA="],
|
||||
[[255,255,255], "////"],
|
||||
[[1,2,3], "AQID"]
|
||||
]
|
@ -1,13 +1,24 @@
|
||||
import { toAscii } from "@cosmjs/encoding";
|
||||
import { Coin } from "cosmjs-types/cosmos/base/v1beta1/coin";
|
||||
|
||||
import bytesData from "../testdata/bytes.json";
|
||||
import coinData from "../testdata/coin.json";
|
||||
import coinsData from "../testdata/coins.json";
|
||||
import decimals from "../testdata/decimals.json";
|
||||
import integers from "../testdata/integers.json";
|
||||
import { DisplayUnit, formatCoin, formatCoins, formatDecimal, formatInteger } from "./valuerenderers";
|
||||
import {
|
||||
DisplayUnit,
|
||||
formatBytes,
|
||||
formatCoin,
|
||||
formatCoins,
|
||||
formatDecimal,
|
||||
formatInteger,
|
||||
} from "./valuerenderers";
|
||||
|
||||
type TestDataCoin = Array<[Coin, DisplayUnit, string]>;
|
||||
type TestDataCoins = Array<[Coin[], Record<string, DisplayUnit>, string]>;
|
||||
/** First argument is an array of bytes (0-255), second argument is the expected string */
|
||||
type TestDataBytes = Array<[number[], string]>;
|
||||
|
||||
describe("valuerenderers", () => {
|
||||
describe("formatInteger", () => {
|
||||
@ -67,4 +78,16 @@ describe("valuerenderers", () => {
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("formatBytes", () => {
|
||||
it("works", () => {
|
||||
expect(formatBytes(toAscii("foo"))).toEqual("Zm9v");
|
||||
|
||||
for (const [data, expected] of bytesData as TestDataBytes) {
|
||||
expect(formatBytes(Uint8Array.from(data)))
|
||||
.withContext(`Input '${JSON.stringify(data)}'`)
|
||||
.toEqual(expected);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -1,3 +1,4 @@
|
||||
import { toBase64 } from "@cosmjs/encoding";
|
||||
import { Decimal } from "@cosmjs/math";
|
||||
import { DenomUnit } from "cosmjs-types/cosmos/bank/v1beta1/bank";
|
||||
import { Coin } from "cosmjs-types/cosmos/base/v1beta1/coin";
|
||||
@ -35,3 +36,7 @@ export function formatCoins(input: Coin[], units: Record<string, DisplayUnit>):
|
||||
pairs.sort((a, b) => compareAscii(a[1], b[1]));
|
||||
return pairs.map((pair) => pair.join(" ")).join(", ");
|
||||
}
|
||||
|
||||
export function formatBytes(data: Uint8Array): string {
|
||||
return toBase64(data);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user