Colota - Android GPS Location tracker
refactor: unify formatBytes in utils/format.ts
Max Dietrichc9415024b8ca
diff --git a/apps/mobile/src/components/features/map/OfflinePackManager.ts b/apps/mobile/src/components/features/map/OfflinePackManager.tsindex 7edcd99d..f816580d 100644--- a/apps/mobile/src/components/features/map/OfflinePackManager.ts+++ b/apps/mobile/src/components/features/map/OfflinePackManager.ts@@ -95,11 +95,6 @@ export function estimateSizeLabel(ne: [number, number], sw: [number, number]): sreturn `~${mb.toFixed(0)} MB`}-export function formatBytes(bytes: number): string {- if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`- return `${(bytes / (1024 * 1024)).toFixed(1)} MB`-}-/** Finds a pack by its stored metadata.name (packs are identified by UUID in v11). */async function findPackByName(name: string): Promise<OfflinePack | null> {const packs = await OfflineManager.getPacks()diff --git a/apps/mobile/src/components/features/map/__tests__/OfflinePackManager.test.ts b/apps/mobile/src/components/features/map/__tests__/OfflinePackManager.test.tsindex dce95a0c..68e08535 100644--- a/apps/mobile/src/components/features/map/__tests__/OfflinePackManager.test.ts+++ b/apps/mobile/src/components/features/map/__tests__/OfflinePackManager.test.ts@@ -23,11 +23,11 @@ jest.mock("../../../../services/NativeLocationService")import { OfflineManager } from "@maplibre/maplibre-react-native"import NativeLocationService from "../../../../services/NativeLocationService"+import { formatBytes } from "../../../../utils/format"import {willExceedTileLimit,estimateSizeLabel,estimateSizeBytes,- formatBytes,createOfflinePack,loadOfflineAreas,deleteOfflineArea,@@ -77,8 +77,8 @@ describe("formatBytes", () => {expect(formatBytes(512 * 1024)).toBe("512.0 KB")})- it("formats zero bytes as KB", () => {- expect(formatBytes(0)).toBe("0.0 KB")+ it("formats zero bytes as B", () => {+ expect(formatBytes(0)).toBe("0 B")})it("formats 1MB as MB", () => {diff --git a/apps/mobile/src/screens/OfflineMapsScreen.tsx b/apps/mobile/src/screens/OfflineMapsScreen.tsxindex 65497b8b..461604af 100644--- a/apps/mobile/src/screens/OfflineMapsScreen.tsx+++ b/apps/mobile/src/screens/OfflineMapsScreen.tsx@@ -20,12 +20,12 @@ import { MapCenterButton } from "../components/features/map/MapCenterButton"import { ColotaMapView, ColotaMapRef } from "../components/features/map/ColotaMapView"import { logger } from "../utils/logger"import NativeLocationService from "../services/NativeLocationService"+import { formatBytes } from "../utils/format"import {createOfflinePack,loadOfflineAreas,deleteOfflineArea,unsubscribeOfflinePack,- formatBytes,DOWNLOAD_STATE,OfflinePackStatus,OfflineAreaInfo,diff --git a/apps/mobile/src/screens/__tests__/OfflineMapsScreen.test.tsx b/apps/mobile/src/screens/__tests__/OfflineMapsScreen.test.tsxindex a1c91eb6..b2e1393b 100644--- a/apps/mobile/src/screens/__tests__/OfflineMapsScreen.test.tsx+++ b/apps/mobile/src/screens/__tests__/OfflineMapsScreen.test.tsx@@ -93,9 +93,12 @@ const mockEstimateSizeLabel = jest.fn()const mockEstimateSizeBytes = jest.fn()const mockWillExceedTileLimit = jest.fn()+jest.mock("../../utils/format", () => ({+ formatBytes: jest.fn().mockReturnValue("5.0 MB")+}))+jest.mock("../../components/features/map/OfflinePackManager", () => ({DOWNLOAD_STATE: { INACTIVE: "inactive", ACTIVE: "active", COMPLETE: "complete" },- formatBytes: jest.fn().mockReturnValue("5.0 MB"),loadOfflineAreas: (...args: any[]) => mockLoadOfflineAreas(...args),loadOfflineAreaBounds: (...args: any[]) => mockLoadOfflineAreaBounds(...args),createOfflinePack: (...args: any[]) => mockCreateOfflinePack(...args),diff --git a/apps/mobile/src/utils/__tests__/exportConverters.test.ts b/apps/mobile/src/utils/__tests__/exportConverters.test.tsindex e54b7b93..974a9fd5 100644--- a/apps/mobile/src/utils/__tests__/exportConverters.test.ts+++ b/apps/mobile/src/utils/__tests__/exportConverters.test.ts@@ -1,5 +1,5 @@+import { formatBytes } from "../format"import {- formatBytes,getByteSize,convertTripsToCSV,convertTripsToGeoJSON,diff --git a/apps/mobile/src/utils/exportConverters.ts b/apps/mobile/src/utils/exportConverters.tsindex 51a6f513..4cde6c23 100644--- a/apps/mobile/src/utils/exportConverters.ts+++ b/apps/mobile/src/utils/exportConverters.ts@@ -16,12 +16,6 @@ function hexToKmlColor(hex: string): string {return `ff${h.slice(4, 6)}${h.slice(2, 4)}${h.slice(0, 2)}`}-export const formatBytes = (bytes: number): string => {- if (bytes < 1024) return `${bytes} B`- if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`- return `${(bytes / (1024 * 1024)).toFixed(1)} MB`-}-export const getByteSize = (content: string): number => {let bytes = 0for (let i = 0; i < content.length; i++) {diff --git a/apps/mobile/src/utils/format.ts b/apps/mobile/src/utils/format.tsnew file mode 100644index 00000000..1bda86cd--- /dev/null+++ b/apps/mobile/src/utils/format.ts@@ -0,0 +1,11 @@+/**+ * Copyright (C) 2026 Max Dietrich+ * Licensed under the GNU AGPLv3. See LICENSE in the project root for details.+ */++/** Formats a byte count as B / KB / MB. */+export const formatBytes = (bytes: number): string => {+ if (bytes < 1024) return `${bytes} B`+ if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`+ return `${(bytes / (1024 * 1024)).toFixed(1)} MB`+}