Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix(web): clamp unit index in formatFileSize/formatTime to avoid undefined unit (#37839)

Eldar Dadashove41beb5942f9
diff --git a/web/utils/format.spec.ts b/web/utils/format.spec.ts
index fee8a0a0053..b5edaa18f14 100644
--- a/web/utils/format.spec.ts
+++ b/web/utils/format.spec.ts
@@ -20,6 +20,9 @@ describe('formatFileSize', () => {
])('formats %s bytes as %s', (input, expected) => {
expect(formatFileSize(input)).toBe(expected)
})
+ it('should clamp to PB instead of an undefined unit for very large sizes', () => {
+ expect(formatFileSize(1024 ** 6)).toBe('1024.00 PB')
+ })
})
describe('formatTime', () => {
@@ -30,6 +33,9 @@ describe('formatTime', () => {
])('formats %s seconds as %s', (input, expected) => {
expect(formatTime(input)).toBe(expected)
})
+ it('should clamp to hours instead of an undefined unit for very large durations', () => {
+ expect(formatTime(216000)).toBe('60.00 h')
+ })
})
describe('formatNumberAbbreviated', () => {
diff --git a/web/utils/format.ts b/web/utils/format.ts
index 80657e0f6c9..a53c29f0b40 100644
--- a/web/utils/format.ts
+++ b/web/utils/format.ts
@@ -72,7 +72,7 @@ export const formatFileSize = (fileSize: number) => {
if (!fileSize) return fileSize
const units = ['', 'K', 'M', 'G', 'T', 'P']
let index = 0
- while (fileSize >= 1024 && index < units.length) {
+ while (fileSize >= 1024 && index < units.length - 1) {
fileSize = fileSize / 1024
index++
}
@@ -89,7 +89,7 @@ export const formatTime = (seconds: number) => {
if (!seconds) return seconds
const units = ['sec', 'min', 'h']
let index = 0
- while (seconds >= 60 && index < units.length) {
+ while (seconds >= 60 && index < units.length - 1) {
seconds = seconds / 60
index++
}