@@ -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-
8253export 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-
135102export 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-
146106export 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
190146export 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
205156export 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-
237181const 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-
252189export 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-
268196export 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+ }
0 commit comments