GooDrive/frontend/src/lib/quota-utils.js
Louis Mazin ac44c6ed8b nice
2026-10-13 23:47:19 +02:00

12 lines
583 B
JavaScript

/** Values sent to Django are integer bytes (null = no per-space limit). */
const GIB = 1024 ** 3
export const fromQuota = bytes => bytes == null ? '' : String(Number((bytes / GIB).toFixed(3)))
export function toQuota(value) {
if (value === '') return null
const trimmed = String(value).trim().replace(',', '.')
if (!/^\d+(\.\d{1,3})?$/.test(trimmed)) throw new Error('Entre un quota positif en Go, avec 3 décimales maximum.')
const bytes = Math.round(Number(trimmed) * GIB)
if (!Number.isSafeInteger(bytes)) throw new Error('Ce quota est trop élevé.')
return bytes
}