-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Hugo Marques <[email protected]>
- Loading branch information
1 parent
5eac295
commit 3f73911
Showing
5 changed files
with
160 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
export const formatData = (numbers: object, format: boolean) => { | ||
function addCommas(number: string | number) { | ||
const parts = number.toString().split('.'); | ||
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ','); | ||
return parts.join('.'); | ||
} | ||
|
||
function formatNumber(number: number, format: boolean) { | ||
if (format) { | ||
if (number >= 1000000) { | ||
return addCommas((number / 1000000).toFixed(1)) + 'M'; | ||
} else if (number >= 1000) { | ||
return addCommas((number / 1000).toFixed(0)) + 'k'; | ||
} | ||
} | ||
|
||
const decimalPart = | ||
number % 1 !== 0 ? number.toFixed(3).split('.')[1] : ''; | ||
const formattedNumber = decimalPart | ||
? number.toFixed(3) | ||
: number.toFixed(0); | ||
return addCommas(formattedNumber); | ||
} | ||
|
||
let formattedData = null; | ||
|
||
if (numbers) { | ||
formattedData = {}; | ||
|
||
Object.entries(numbers).forEach(([key, value]) => { | ||
if (typeof value === 'string' && value.includes('$')) { | ||
formattedData[key] = value; // Return the value as is if it contains "$" | ||
} else { | ||
const number = parseFloat(value as string); | ||
if (Number.isNaN(number)) { | ||
formattedData[key] = value; // Return the value as is if it's not a number | ||
} else { | ||
formattedData[key] = formatNumber(number, format); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
return formattedData; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3f73911
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
web – ./
web-ipct.vercel.app
www.impactmarket.com
web-git-production-ipct.vercel.app
impactmarket.com