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.
44 lines (40 loc) • 1.28 kB
JavaScript
class ColumnFilter {
constructor() {
this.filterState = 0;
this.init()
}
init() {
this.filtered = {};
this.maxFilter = 0;
this.count = 0
}
get isFiltered() { return this.count > 0 }
filterRows(indexes = []) {
const { count } = this
for (let i of indexes) {
if (this.filtered[i]) continue
this.filtered[i] = true
this.count++
}
if (indexes[indexes.length - 1] > this.maxFilter) this.maxFilter = indexes[indexes.length - 1]
if (this.count > count) this.filterState++
return this
}
clearRowsFilters(indexes = []) {
if (this.maxFilter === 0) return
const { count, maxFilter } = this
for (let i of indexes) {
if (!this.filtered[i]) continue
delete this.filtered[i]
this.count--
}
if (!this.filtered[maxFilter]) { // If `maxFilter` deleted, find new max
const keys = Object.keys(this.filtered).map(n => Number(n))
this.maxFilter = keys[keys.length - 1] || 0;
}
if (count > this.count) this.filterState++
return this
}
clearAllRowsFilters() { this.init(); this.filterState++; return this }
}
module.exports = ColumnFilter