|
| 1 | +/** |
| 2 | + * Scatter-filter logic, extracted from ui.mjs so it can be unit tested. |
| 3 | + * |
| 4 | + * These functions used to live inside `setupEventListeners` — a 400-line |
| 5 | + * closure — purely so they could capture `colNames`. That made the only part |
| 6 | + * of the filter subsystem with real correctness content (the predicate, and |
| 7 | + * the derived-quantity formulas) reachable only through Playwright. Both are |
| 8 | + * pure given their inputs, so they belong here with a fast node test. |
| 9 | + * |
| 10 | + * See test/test_js_filters.mjs. |
| 11 | + */ |
| 12 | + |
| 13 | +/** |
| 14 | + * Resolve a column name to its index, throwing loudly on a miss. |
| 15 | + * |
| 16 | + * A silent fallback here would be dangerous: `indexOf` returning -1 and being |
| 17 | + * used as an index yields `undefined`, which makes every numeric comparison |
| 18 | + * false and quietly disables the filter instead of failing. |
| 19 | + */ |
| 20 | +export function colIdxStrict(colNames, name) { |
| 21 | + const i = colNames.indexOf(name); |
| 22 | + if (i < 0) { |
| 23 | + throw new Error( |
| 24 | + `filters: column ${JSON.stringify(name)} not found in ${JSON.stringify(colNames)}. ` + |
| 25 | + "Column names must match DEFAULT_X_COLUMNS in boxcrete/utils.py.", |
| 26 | + ); |
| 27 | + } |
| 28 | + return i; |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Derived quantities offered in the filter dropdown alongside raw columns. |
| 33 | + * Each `compute` takes a raw composition row and returns a scalar. |
| 34 | + * |
| 35 | + * NOTE: `paste` divides by a 6-term total that omits HRWR, whereas Python's |
| 36 | + * `_TOTAL_MASS_NAMES` (boxcrete/utils.py) includes it. HRWR is at most ~13 of |
| 37 | + * ~2400 kg/m3, so the two differ by well under 1%, but they are not the same |
| 38 | + * definition. Preserved as-is here to keep this extraction behaviour-neutral. |
| 39 | + * |
| 40 | + * @param {string[]} colNames - composition column names, in catalog order. |
| 41 | + */ |
| 42 | +export function makeComputedFilters(colNames) { |
| 43 | + const iCement = colIdxStrict(colNames, "Cement (kg/m3)"); |
| 44 | + const iFlyAsh = colIdxStrict(colNames, "Fly Ash (kg/m3)"); |
| 45 | + const iSlag = colIdxStrict(colNames, "Slag (kg/m3)"); |
| 46 | + const iWater = colIdxStrict(colNames, "Water (kg/m3)"); |
| 47 | + const iCoarse = colIdxStrict(colNames, "Coarse Aggregates (kg/m3)"); |
| 48 | + const iFine = colIdxStrict(colNames, "Fine Aggregate (kg/m3)"); |
| 49 | + |
| 50 | + const binderOf = (c) => c[iCement] + c[iFlyAsh] + c[iSlag]; |
| 51 | + |
| 52 | + return [ |
| 53 | + { |
| 54 | + id: "wb", |
| 55 | + label: "W/B Ratio", |
| 56 | + compute: (c) => { |
| 57 | + const b = binderOf(c); |
| 58 | + return b > 0 ? c[iWater] / b : Infinity; |
| 59 | + }, |
| 60 | + }, |
| 61 | + { id: "binder", label: "Total Binder", compute: binderOf }, |
| 62 | + { |
| 63 | + id: "scm", |
| 64 | + label: "SCM Replacement %", |
| 65 | + compute: (c) => { |
| 66 | + const b = binderOf(c); |
| 67 | + return b > 0 ? ((c[iFlyAsh] + c[iSlag]) / b) * 100 : 0; |
| 68 | + }, |
| 69 | + }, |
| 70 | + { |
| 71 | + id: "paste", |
| 72 | + label: "Paste Fraction", |
| 73 | + compute: (c) => { |
| 74 | + const paste = binderOf(c) + c[iWater]; |
| 75 | + const total = paste + c[iCoarse] + c[iFine]; |
| 76 | + return total > 0 ? paste / total : 0; |
| 77 | + }, |
| 78 | + }, |
| 79 | + ]; |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * Does `comp` satisfy every active filter? |
| 84 | + * |
| 85 | + * A filter is either |
| 86 | + * { colIdx, classes: Set<number> } categorical: class membership |
| 87 | + * { colIdx | computed, min, max } numeric: inclusive bounds |
| 88 | + * |
| 89 | + * Categorical columns (Material Source) are unordered, so a min/max range is |
| 90 | + * meaningless for them — "between Source A and Source B" says nothing. They |
| 91 | + * test set membership on the rounded class instead. |
| 92 | + * |
| 93 | + * @param {number[]} comp - one composition row. |
| 94 | + * @param {Array|null} filters - active filter specs; null/empty means "match all". |
| 95 | + * @returns {boolean} true when the point should stay visible. |
| 96 | + */ |
| 97 | +export function matchesFilters(comp, filters) { |
| 98 | + if (!filters || filters.length === 0) return true; |
| 99 | + for (const f of filters) { |
| 100 | + const val = f.computed ? f.computed(comp) : comp[f.colIdx]; |
| 101 | + if (f.classes) { |
| 102 | + if (!f.classes.has(Math.round(val))) return false; |
| 103 | + } else if (val < f.min || val > f.max) { |
| 104 | + return false; |
| 105 | + } |
| 106 | + } |
| 107 | + return true; |
| 108 | +} |
0 commit comments