UNPKG

@opengis/fastify-table

Version:

core-plugins

42 lines (41 loc) 1.4 kB
/* eslint-disable no-restricted-globals */ /* eslint-disable no-param-reassign */ /** * Повертає розмір файлу на диску у встановлених одиницях вимірювання об'єму файлу. * * @summary Повертає розмір растру у відповідних одиницях об'єму файлу (Тбайт, Гбайт, Мбайт) * @priority 2 * @type helper * @tag format * @alias formatUnit * @example * {{formatUnit '123.45678' number="2"}} * @param {data} data Число для перетворення * @param {Number} number Число після точки * @param {String} unit "В" * @returns {String} Returns HTML */ export default function formatUnit(data, options) { data = parseFloat(data); if (isNaN(data)) return data; const UNIT = { B: { 4: "TB", 3: "GB", 2: "MB", 1: "KB", 0: "B", }, }; const unit = UNIT[options.hash.unit] || UNIT.B; const number = options.hash.number || 0; for (let i = 4; i >= 0; i -= 1) { /* If i=0 then dataNew>0 - size in min units (17/03/20 by Olya) */ if (data >= (i ? 10 ** (3 * i) : i)) { return ((data / 10 ** (3 * i)).toFixed(data % 10 ** (3 * i) ? number : 0) + unit[i]); } } return data.toString(); }