@yamada-ui/react
Version:
React UI components of the Yamada, by the Yamada, for the Yamada built with React and Emotion
56 lines (52 loc) • 1.76 kB
JavaScript
"use client";
const require_rolldown_runtime = require('../../_virtual/rolldown_runtime.cjs');
const require_i18n_provider = require('../../providers/i18n-provider/i18n-provider.cjs');
const require_use_format_number = require('./use-format-number.cjs');
let react = require("react");
react = require_rolldown_runtime.__toESM(react);
//#region src/components/format/use-format-byte.ts
const bitPrefixes = [
"",
"kilo",
"mega",
"giga",
"tera"
];
const bytePrefixes = [
"",
"kilo",
"mega",
"giga",
"tera",
"peta"
];
/**
* `useFormatByte` is a custom hook that returns the formatted byte.
*
* @see https://yamada-ui.com/docs/hooks/use-format-byte
*/
const useFormatByte = (bytes, options) => {
return useByteFormat(options)(bytes);
};
const useByteFormat = ({ locale, unit: defaultUnit = "byte", unitDisplay: defaultUnitDisplay = "short" } = {}) => {
const { locale: defaultLocale } = require_i18n_provider.useI18n();
const numberFormat = require_use_format_number.useNumberFormat({ locale: locale ?? defaultLocale });
return (0, react.useCallback)((bytes, { unit = defaultUnit, unitDisplay = defaultUnitDisplay } = {}) => {
const sanitizedBytes = Number.isNaN(bytes) ? 0 : bytes;
const prefix = unit === "bit" ? bitPrefixes : bytePrefixes;
const index = Math.max(0, Math.min(Math.floor(Math.log10(sanitizedBytes) / 3), prefix.length - 1));
return numberFormat(sanitizedBytes === 0 ? 0 : Number.parseFloat((sanitizedBytes / 10 ** (3 * index)).toPrecision(3)), {
style: "unit",
unit: prefix[index] + unit,
unitDisplay
});
}, [
defaultUnit,
defaultUnitDisplay,
numberFormat
]);
};
//#endregion
exports.useByteFormat = useByteFormat;
exports.useFormatByte = useFormatByte;
//# sourceMappingURL=use-format-byte.cjs.map