@netlify/zip-it-and-ship-it
Version:
18 lines (17 loc) • 653 B
JavaScript
const MEMORY_UNIT_TO_MB = { mb: 1, gb: 1024 };
const MEMORY_PATTERN = /^(\d+(?:\.\d+)?)\s*(mb|gb)?$/;
// Accepts a bare number (interpreted as MB), a numeric string (`"2048"`), or
// a human-friendly size with a unit (`"2gb"`, `"1024mb"`, case-insensitive).
// Returns a normalized integer megabyte count.
export const parseMemoryMB = (input) => {
if (typeof input === 'number') {
return input;
}
const match = MEMORY_PATTERN.exec(input.trim().toLowerCase());
if (!match) {
return Number.NaN;
}
const value = parseFloat(match[1]);
const unit = match[2] || 'mb';
return value * MEMORY_UNIT_TO_MB[unit];
};