|
| 1 | +/* eslint-disable no-magic-numbers */ |
| 2 | +const PRICE_BUMP = 15 |
| 3 | + |
| 4 | +/** |
| 5 | + * @typedef {Record<string, any> & { |
| 6 | + * tickCount?: number |
| 7 | + * rowIdField?: string |
| 8 | + * rowData: Record<string, any>[] |
| 9 | + * }} GridDemoEntity |
| 10 | + */ |
| 11 | + |
| 12 | +/** |
| 13 | + * Initializes demo-specific counters. |
| 14 | + * @param {GridDemoEntity} entity |
| 15 | + */ |
| 16 | +export function create(entity) { |
| 17 | + entity.tickCount ??= 0 |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * Increments the render tick counter for the demo UI. |
| 22 | + * @param {GridDemoEntity} entity |
| 23 | + */ |
| 24 | +export function tick(entity) { |
| 25 | + entity.tickCount += 1 |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * Shuffles rows to showcase incremental row updates. |
| 30 | + * @param {GridDemoEntity} entity |
| 31 | + */ |
| 32 | +export function shuffleRows(entity) { |
| 33 | + const nextRows = [...entity.rowData] |
| 34 | + for (let i = nextRows.length - 1; i > 0; i -= 1) { |
| 35 | + const j = Math.floor(Math.random() * (i + 1)) |
| 36 | + const current = nextRows[i] |
| 37 | + nextRows[i] = nextRows[j] |
| 38 | + nextRows[j] = current |
| 39 | + } |
| 40 | + entity.rowData = nextRows |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Applies a simple price/rating mutation used by demo controls. |
| 45 | + * @param {GridDemoEntity} entity |
| 46 | + */ |
| 47 | +export function bumpPrices(entity) { |
| 48 | + entity.rowData = entity.rowData.map((row) => ({ |
| 49 | + ...row, |
| 50 | + price: typeof row.price === "number" ? row.price + PRICE_BUMP : row.price, |
| 51 | + rating: |
| 52 | + typeof row.rating === "number" |
| 53 | + ? Number((row.rating + Math.random()).toFixed(1)) |
| 54 | + : row.rating, |
| 55 | + })) |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * Appends a synthetic row based on the last row shape. |
| 60 | + * @param {GridDemoEntity} entity |
| 61 | + */ |
| 62 | +export function addRow(entity) { |
| 63 | + const idField = entity.rowIdField || "id" |
| 64 | + const nextId = entity.rowData.length + 1 |
| 65 | + const previousRow = entity.rowData.at(-1) || {} |
| 66 | + const nextRow = {} |
| 67 | + |
| 68 | + const entries = Object.entries(previousRow) |
| 69 | + if (!entries.length) { |
| 70 | + nextRow[idField] = nextId |
| 71 | + } |
| 72 | + |
| 73 | + for (const [key, value] of entries) { |
| 74 | + if (key === idField) { |
| 75 | + nextRow[key] = nextId |
| 76 | + continue |
| 77 | + } |
| 78 | + |
| 79 | + if (typeof value === "number") { |
| 80 | + const randomFactor = 0.9 + Math.random() * 0.2 |
| 81 | + nextRow[key] = Number((value * randomFactor).toFixed(2)) |
| 82 | + continue |
| 83 | + } |
| 84 | + |
| 85 | + if (typeof value === "string") { |
| 86 | + nextRow[key] = `${value} ${nextId}` |
| 87 | + continue |
| 88 | + } |
| 89 | + |
| 90 | + nextRow[key] = value |
| 91 | + } |
| 92 | + |
| 93 | + if (!(idField in nextRow)) { |
| 94 | + nextRow[idField] = nextId |
| 95 | + } |
| 96 | + |
| 97 | + entity.rowData = [...entity.rowData, nextRow] |
| 98 | +} |
0 commit comments