UNPKG

utilyx

Version:

Modern utility helper library for cleaner, faster TypeScript/JavaScript development 🚀🔧

64 lines (63 loc) • 2.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.bytesToHumanReadable = bytesToHumanReadable; exports.humanReadableToBytes = humanReadableToBytes; exports.msToTime = msToTime; exports.hexToRgb = hexToRgb; exports.rgbToHex = rgbToHex; exports.degToRad = degToRad; exports.radToDeg = radToDeg; exports.celsiusToFahrenheit = celsiusToFahrenheit; exports.fahrenheitToCelsius = fahrenheitToCelsius; // 1. bytesToHumanReadable: 1337420 → "1.27 MB" function bytesToHumanReadable(bytes) { if (bytes === 0) return "0 B"; const units = ["B", "KB", "MB", "GB", "TB"]; const i = Math.floor(Math.log(bytes) / Math.log(1024)); const size = bytes / Math.pow(1024, i); return `${size.toFixed(2)} ${units[i]}`; } // 2. humanReadableToBytes: "1.27 MB" → 1337420 function humanReadableToBytes(str) { const [num, unit] = str.trim().split(/[\s]+/); const units = { B: 0, KB: 1, MB: 2, GB: 3, TB: 4 }; const exponent = units[unit.toUpperCase()] ?? 0; return Math.round(parseFloat(num) * Math.pow(1024, exponent)); } // 5. msToTime: 90061 → "1m 30s" function msToTime(ms) { const minutes = Math.floor(ms / 60000); const seconds = Math.floor((ms % 60000) / 1000); return `${minutes > 0 ? `${minutes}m ` : ''}${seconds}s`.trim(); } // 6. hexToRgb: "#ffffff" → { r: 255, g: 255, b: 255 } function hexToRgb(hex) { const cleanHex = hex.replace('#', ''); const bigint = parseInt(cleanHex, 16); return { r: (bigint >> 16) & 255, g: (bigint >> 8) & 255, b: bigint & 255, }; } // 7. rgbToHex: (255, 255, 255) → "#ffffff" function rgbToHex(r, g, b) { return `#${[r, g, b].map(x => x.toString(16).padStart(2, '0')).join('')}`; } // 9. degToRad: 180 → π function degToRad(deg) { return deg * (Math.PI / 180); } // 10. radToDeg: π → 180 function radToDeg(rad) { return rad * (180 / Math.PI); } // 11. celsiusToFahrenheit: 0 → 32 function celsiusToFahrenheit(c) { return (c * 9) / 5 + 32; } // 12. fahrenheitToCelsius: 32 → 0 function fahrenheitToCelsius(f) { return ((f - 32) * 5) / 9; }