als-statistics
Version:
A powerful and lightweight JavaScript library for descriptive statistics, regression, clustering, outlier detection, and noise analysis using a flexible table/column architecture.
39 lines (36 loc) • 1.48 kB
JavaScript
function addRow(obj, index = null, table) {
// Проверка соответствия ключей и типов
const colNames = Object.keys(table.columns);
for (let key of Object.keys(obj)) {
if (!colNames.includes(key)) return false;
const colType = table.columns[key].type;
if (colType === Number && typeof obj[key] !== 'number') return false;
if (colType === String && typeof obj[key] !== 'string') return false;
}
for (let colName of colNames) {
if (!(colName in obj)) return false;
}
if (index === null || index >= table.n) { // Добавление строки
for (let [colName, value] of Object.entries(obj)) { // Добавление в конец
table.columns[colName]._values.push(value);
}
table.n++;
} else { // Вставка по индексу
for (let [colName, value] of Object.entries(obj)) {
table.columns[colName]._values.splice(index, 0, value);
}
table.n++;
// Обновление фильтра
const updatedFiltered = {};
for (let i in table.filtered) {
const numIndex = Number(i);
if (numIndex >= index) updatedFiltered[numIndex + 1] = true;
else updatedFiltered[numIndex] = true;
}
table.filtered = updatedFiltered;
if (table.maxFilter >= index) table.maxFilter++;
}
table.filterState++;
return true;
}
module.exports = addRow