jan/docs/src/utils/format.ts
Faisal Amir 85d32a4c70
enhancement: show app size on download dropdown (#5360)
* enhancement: show app size on download dropdown

* Update docs/src/utils/format.ts

Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com>

* chor: update copy

---------

Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com>
2025-06-19 12:04:16 +07:00

40 lines
1.2 KiB
TypeScript

export function formatCompactNumber(count: number) {
const formatter = Intl.NumberFormat('en', {
notation: 'compact',
maximumFractionDigits: 1,
})
return formatter.format(count)
}
export function formatFileSize(bytes: number): string {
if (!bytes) return '0 B'
const sizes = ['B', 'KB', 'MB', 'GB', 'TB']
const i = Math.floor(Math.log(bytes) / Math.log(1024))
if (i === 0) return `${bytes} ${sizes[i]}`
return `${(bytes / Math.pow(1024, i)).toFixed(1)} ${sizes[i]}`
}
export const totalDownload = (release: []) => {
if (release instanceof Array) {
const count = release
.map((version: { assets: any[]; name: string }) => {
// it will be correct since 0.5.15
const tag = version.name >= '0.5.15' && version.name.includes('0.5.15')
return version.assets
.filter((os) => !(tag && os.name.endsWith('.yml')))
.map((os) => os.download_count)
})
.map((x: any[]) => x.reduce((a: any, b: any) => a + b, 0))
.reduce((a: any, b: any) => a + b, 0)
return formatCompactNumber(count)
} else {
// return dummy to avoid rate limit API when in dev mode
return formatCompactNumber(9000000)
}
}