Skip to content

Commit 1a82bce

Browse files
author
jorgen
committed
add tests and clean up
1 parent a6d7c42 commit 1a82bce

File tree

2 files changed

+53
-87
lines changed

2 files changed

+53
-87
lines changed

src/utils/format.ts

Lines changed: 22 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -50,35 +50,6 @@ export function shortenString(
5050
)}`;
5151
}
5252

53-
/**
54-
* Shortens an arbitrary string to a fixed number of characters, mindful of the character length of the delimiter
55-
* @param str The string to be potentially shortened
56-
* @param delimiter The delimiter
57-
* @param maxChars The number of characters to constrain this string
58-
* @returns `str` if string is less than maxChars. The first `maxChars` chars if the delimiter is too large. A collapsed version with the delimiter in the middle.
59-
*/
60-
export function shortenStringToLength(
61-
str: string,
62-
delimiter: string,
63-
maxChars: number
64-
) {
65-
if (str.length <= maxChars) {
66-
return str;
67-
} else {
68-
const charsNeeded = maxChars - delimiter.length;
69-
// Delimiter is out of bounds
70-
if (charsNeeded <= 0) {
71-
return str.slice(0, maxChars);
72-
} else {
73-
const charDivision = charsNeeded / 2;
74-
const left = str.slice(0, Math.ceil(charDivision));
75-
const right =
76-
charDivision < 1 ? "" : str.slice(-Math.floor(charDivision));
77-
return `${left}${delimiter}${right}`;
78-
}
79-
}
80-
}
81-
8253
export function shortenTransactionHash(hash: string): string {
8354
return `${hash.substring(0, 5)}...`;
8455
}
@@ -128,21 +99,10 @@ export function formatUnitsWithMaxFractionsFnBuilder(decimals: number) {
12899
return closure;
129100
}
130101

131-
export function formatEtherRaw(wei: ethers.BigNumberish): string {
132-
return ethers.utils.formatUnits(wei, 18);
133-
}
134-
135102
export function parseUnits(value: string, decimals: number): ethers.BigNumber {
136103
return ethers.utils.parseUnits(value, decimals);
137104
}
138105

139-
export function parseUnitsFnBuilder(decimals: number) {
140-
function closure(value: string) {
141-
return parseUnits(value, decimals);
142-
}
143-
return closure;
144-
}
145-
146106
export function parseEtherLike(value: string): ethers.BigNumber {
147107
return parseUnits(value, 18);
148108
}
@@ -182,10 +142,6 @@ export function formattedBigNumberToNumber(
182142
}
183143
}
184144

185-
export function stringToHex(value: string) {
186-
return ethers.utils.hexlify(ethers.utils.toUtf8Bytes(value));
187-
}
188-
189145
// appends hex tag to data
190146
export function tagHex(
191147
dataHex: string,
@@ -196,11 +152,6 @@ export function tagHex(
196152
return ethers.utils.hexConcat([dataHex, delimitterHex, tagHex]);
197153
}
198154

199-
// converts a string tag to hex and appends, currently not in use
200-
export function tagString(dataHex: string, tagString: string) {
201-
return tagHex(dataHex, stringToHex(tagString));
202-
}
203-
204155
// tags only an address
205156
export function tagAddress(
206157
dataHex: string,
@@ -227,13 +178,6 @@ export function convertToCapitalCase(str: string) {
227178
.join(" ");
228179
}
229180

230-
const twoSigFormatter = new Intl.NumberFormat("en-US", {
231-
maximumSignificantDigits: 2,
232-
});
233-
234-
export const formatNumberTwoSigDigits =
235-
twoSigFormatter.format.bind(twoSigFormatter);
236-
237181
const threeMaxFracFormatter = new Intl.NumberFormat("en-US", {
238182
maximumFractionDigits: 3,
239183
});
@@ -242,29 +186,13 @@ export const formatNumberMaxFracDigits = threeMaxFracFormatter.format.bind(
242186
threeMaxFracFormatter
243187
);
244188

245-
const twoMaxFracFormatter = new Intl.NumberFormat("en-US", {
246-
maximumFractionDigits: 2,
247-
});
248-
249-
export const formatNumberTwoFracDigits =
250-
twoMaxFracFormatter.format.bind(twoMaxFracFormatter);
251-
252189
export function formatMaxFracDigits(number: number, maxFracDigits: number) {
253190
const formatter = new Intl.NumberFormat("en-US", {
254191
maximumFractionDigits: maxFracDigits,
255192
});
256193
return formatter.format(number);
257194
}
258195

259-
export function formatPoolAPY(
260-
wei: ethers.BigNumberish,
261-
decimals: number
262-
): string {
263-
return formatNumberMaxFracDigits(
264-
Number(ethers.utils.formatUnits(wei, decimals))
265-
);
266-
}
267-
268196
export function formatWeiPct(wei?: ethers.BigNumberish, precision: number = 3) {
269197
if (wei === undefined) {
270198
return undefined;
@@ -275,20 +203,6 @@ export function formatWeiPct(wei?: ethers.BigNumberish, precision: number = 3) {
275203
}).format(Number(ethers.utils.formatEther(wei)) * 100);
276204
}
277205

278-
/**
279-
* Formats a number into a human readable format
280-
* @param num The number to format
281-
* @returns A human readable format. I.e. 1000 -> 1K, 1001 -> 1K+
282-
*/
283-
export function humanReadableNumber(num: number, decimals = 0): string {
284-
if (num <= 0) return "0";
285-
return (
286-
numeral(num)
287-
.format(decimals <= 0 ? "0a" : `0.${"0".repeat(decimals)}a`)
288-
.toUpperCase() + "+"
289-
);
290-
}
291-
292206
/**
293207
* Formats an 18 decimal WEI representation of USD into standard USD format
294208
* @param value A 18 decimal fixed-point integer representation of USD
@@ -343,3 +257,25 @@ export function parseUnitsWithExtendedDecimals(
343257
}
344258
return ethers.utils.parseUnits(valueToParse, decimals);
345259
}
260+
261+
export function formatNumberWithSeparators(
262+
value: string,
263+
maxDecimals: number = 18
264+
): string {
265+
if (!value || value === ".") return value;
266+
267+
const parts = value.split(".");
268+
const integerPart = parts[0] || "0";
269+
const decimalPart = parts[1];
270+
271+
const formattedInteger = integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
272+
273+
if (parts.length === 2) {
274+
const limitedDecimals = decimalPart.substring(0, maxDecimals);
275+
return `${formattedInteger}.${limitedDecimals}`;
276+
} else if (value.endsWith(".")) {
277+
return `${formattedInteger}.`;
278+
}
279+
280+
return formattedInteger;
281+
}

src/utils/tests/format.test.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import { utils } from "ethers";
22

33
import {
4+
formatNumberWithSeparators,
5+
formatUSD,
46
isValidString,
57
shortenAddress,
68
shortenString,
79
shortenTransactionHash,
810
smallNumberFormatter,
9-
formatUSD,
1011
} from "../format";
12+
1113
const VALID_ADDRESS = "0x04Fa0d235C4abf4BcF4787aF4CF447DE572eF828";
1214
const TX_HASH =
1315
"0xe5a0c976ca4d09ce6ea034101e78b1a6a9536940cb8f246e7b54d1fe16b8c125";
@@ -72,3 +74,31 @@ describe("#formatUSD", () => {
7274
expect(formatUSD(utils.parseEther("100000.123456"))).toEqual("100,000.12");
7375
});
7476
});
77+
78+
describe("#formatNumberWithSeparators", () => {
79+
it("adds thousand separators to integers", () => {
80+
expect(formatNumberWithSeparators("1234567")).toEqual("1,234,567");
81+
});
82+
83+
it("preserves decimals while adding separators", () => {
84+
expect(formatNumberWithSeparators("1234567.89")).toEqual("1,234,567.89");
85+
});
86+
87+
it("handles numbers under 1000", () => {
88+
expect(formatNumberWithSeparators("999")).toEqual("999");
89+
expect(formatNumberWithSeparators("42.5")).toEqual("42.5");
90+
});
91+
92+
it("preserves trailing decimal point during input", () => {
93+
expect(formatNumberWithSeparators("1234.")).toEqual("1,234.");
94+
});
95+
96+
it("truncates decimals to maxDecimals", () => {
97+
expect(formatNumberWithSeparators("1.123456789", 4)).toEqual("1.1234");
98+
});
99+
100+
it("returns empty/special values unchanged", () => {
101+
expect(formatNumberWithSeparators("")).toEqual("");
102+
expect(formatNumberWithSeparators(".")).toEqual(".");
103+
});
104+
});

0 commit comments

Comments
 (0)