Adding vitest for testing

Currently implemented

- FormatBytes
- MathHelpers
This commit is contained in:
2025-03-07 17:24:52 +09:00
parent 2b27dbae86
commit ea9882256e
8 changed files with 1071 additions and 17 deletions

View File

@@ -13,6 +13,29 @@
</div>
</body>
<script languagae="JavaScript">
/**
* converts a int number into bytes with prefix in two decimals precision
* currently precision is fixed, if dynamic needs check for max/min precision
* @param {Number|BigInt} bytes bytes in int
* @return {String} string in GB/MB/KB
*/
function _formatBytes(bytes)
{
var i = -1;
// If this ia bigint -> convert to number, we need the decimals
if (typeof bytes === "bigint") {
bytes = Number(bytes);
}
do {
bytes = bytes / 1024;
i++;
} while (bytes > 99);
return (
Math.round(bytes * Math.pow(10, 2)) / Math.pow(10, 2)
) + ['kB', 'MB', 'GB', 'TB', 'PB', 'EB'][i];
}
document.addEventListener('DOMContentLoaded', function() {
console.log('JavaScript Utils Test');
let el = document.getElementById('test-div');
@@ -24,9 +47,15 @@ document.addEventListener('DOMContentLoaded', function() {
el.innerHTML = '';
el.innerHTML += '<div>randomIdF: ' + randomIdF() + '</div>';
el.innerHTML += '<div>getWindowSize: ' + JSON.stringify(getWindowSize()) + '</div>';
el.innerHTML += '<div>roundPrecision: ' + roundPrecision(10.1234, 2) + '</div>';
el.innerHTML += '<div>getRandomIntInclusive: ' + getRandomIntInclusive(1, 5) + '</div>';
el.innerHTML += '<div>formatBytes: ' + formatBytes(bytes) + '</div>';
el.innerHTML += '<div>formatBytesLong: ' + formatBytesLong(bytes) + '</div>';
// el.innerHTML += '<div>_formatBytes: ' + _formatBytes(1024n) + '</div>';
el.innerHTML += '<div>_formatBytes: ' + _formatBytes(1021152n) + '</div>';
el.innerHTML += '<div>_formatBytes: ' + _formatBytes(1021152) + '</div>';
// console.log('TR: %s', l10n.__('Original'));
// console.log('TR: %s', l10n.__('Not exists'));
});