b5650c53ba
Extracted pure utility functions (esc, sanitize, formatBytes, formatTime) into web/ui/util.js with CommonJS export for Node testing compatibility. Added 19 tests across 4 suites using node:test + node:assert (zero deps). - util.js: browser-compatible utility functions - util.test.js: 19 tests (5 esc, 3 sanitize, 6 formatBytes, 5 formatTime) - package.json: 'test' script → node --test util.test.js - mise.toml: js-test task - app.js: removed duplicate helper functions (now in util.js) - index.html: load util.js before app.js
29 lines
806 B
JavaScript
29 lines
806 B
JavaScript
// AKAI Utils — shared utility functions
|
|
|
|
function esc(s) {
|
|
if (!s) return ''
|
|
return s.replace(/&/g, '&').replace(/</g, '<')
|
|
.replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, ''')
|
|
}
|
|
|
|
function sanitize(s) {
|
|
return s.replace(/[^a-zA-Z0-9_-]/g, '_')
|
|
}
|
|
|
|
function formatBytes(bytes) {
|
|
if (bytes === 0) return '0 B'
|
|
const k = 1024
|
|
const sizes = ['B', 'KB', 'MB', 'GB']
|
|
const i = Math.floor(Math.log(bytes) / Math.log(k))
|
|
return parseFloat((bytes / Math.pow(k, i)).toFixed(1)) + ' ' + sizes[i]
|
|
}
|
|
|
|
function formatTime(secs) {
|
|
const m = Math.floor(secs / 60)
|
|
const s = Math.floor(secs % 60)
|
|
return m + ':' + s.toString().padStart(2, '0')
|
|
}
|
|
|
|
if (typeof module !== 'undefined' && module.exports) {
|
|
module.exports = { esc, sanitize, formatBytes, formatTime }
|
|
} |