File tree Expand file tree Collapse file tree 1 file changed +9
-9
lines changed Expand file tree Collapse file tree 1 file changed +9
-9
lines changed Original file line number Diff line number Diff line change @@ -14,17 +14,17 @@ export function sleep(ms: number): Promise<unknown> {
14
14
* @date 12/7/2023 - 6:50:18 PM
15
15
*/
16
16
export const shortenNumber = ( amount : number ) => {
17
- const abbrevObject = [
17
+ const abreviations = [
18
18
{ value : 1000000000 , abbreviation : "B" } ,
19
19
{ value : 1000000 , abbreviation : "M" } ,
20
20
{ value : 1000 , abbreviation : "K" } ,
21
21
] ;
22
- for ( const { value , abbreviation } of abbrevObject ) {
23
- if ( amount >= value ) {
24
- const product = amount / value ;
25
- const isFloat = product % 1 !== 0 ;
26
- return ` ${ isFloat ? product . toFixed ( 1 ) : product } ${ abbreviation } ` ;
27
- }
28
- }
29
- return amount ;
22
+
23
+ const abbreviation = abreviations . find ( ( { value } ) => amount >= value ) ;
24
+
25
+ if ( ! abbreviation ) return amount ;
26
+
27
+ const product = amount / abbreviation . value ;
28
+ const isFloat = product % 1 !== 0 ;
29
+ return ` ${ isFloat ? product . toFixed ( 1 ) : product } ${ abbreviation . abbreviation } ` ;
30
30
} ;
You can’t perform that action at this time.
0 commit comments