34 lines
839 B
JavaScript
34 lines
839 B
JavaScript
export function clamp(value, min, max) {
|
|
return Math.min(max, Math.max(min, value));
|
|
}
|
|
|
|
export function debounce(callback, delayMs = 150) {
|
|
let timeoutId = null;
|
|
return (...args) => {
|
|
clearTimeout(timeoutId);
|
|
timeoutId = setTimeout(() => callback(...args), delayMs);
|
|
};
|
|
}
|
|
|
|
export function cellCoordsToIndex(row, col, cols) {
|
|
return row * cols + col;
|
|
}
|
|
|
|
export function listLiveCells(buffer, rows, cols) {
|
|
const result = [];
|
|
for (let row = 0; row < rows; row += 1) {
|
|
for (let col = 0; col < cols; col += 1) {
|
|
const idx = row * cols + col;
|
|
if (buffer[idx] === 1) {
|
|
result.push([row, col]);
|
|
}
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
export function parsePositiveInt(value, fallback) {
|
|
const parsed = Number.parseInt(value, 10);
|
|
return Number.isFinite(parsed) && parsed > 0 ? parsed : fallback;
|
|
}
|