Skip to content

Commit 6e6ef6e

Browse files
refactor: use a more cleaner implementation of the function
1 parent 633642a commit 6e6ef6e

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

src/utilities/index.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ export function sleep(ms: number): Promise<unknown> {
1414
* @date 12/7/2023 - 6:50:18 PM
1515
*/
1616
export const shortenNumber = (amount: number) => {
17-
const abbrevObject = [
17+
const abreviations = [
1818
{ value: 1000000000, abbreviation: "B" },
1919
{ value: 1000000, abbreviation: "M" },
2020
{ value: 1000, abbreviation: "K" },
2121
];
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}`;
3030
};

0 commit comments

Comments
 (0)