Skip to main content

Predicates: rows picked by data, not by functions

{
  "wins >= 11": [
    "KC",
    "LAC",
    "BUF"
  ],
  "division == \"East\"": [
    "BUF",
    "MIA",
    "NYJ",
    "NE"
  ],
  "net_epa isNull": [
    "NE"
  ],
  "team in [\"KC\", \"BUF\"]": [
    "KC",
    "BUF"
  ],
  "qb matches \"^J\"": [
    "LAC",
    "BUF"
  ],
  "selectRows([0, 4]) (indices pass through)": [
    0,
    4
  ],
  "matches(wins > 14, KC)": true
}
import { STANDINGS, type Standing } from "@sportsdataverse/examples/data";
import { type Predicate, matches, selectRows } from "@sportsdataverse/sdvtables";

// highlight, boldRows, spotlight and rowAccent take a Predicate (or row indices): plain data, so a spec stays JSON.
const teams = (p: Predicate<Standing>): string[] =>
selectRows(p, STANDINGS).map((i) => STANDINGS[i]?.team ?? "");
const [kc] = STANDINGS;

{
"wins >= 11": teams({ key: "wins", op: ">=", value: 11 }),
'division == "East"': teams({ key: "division", op: "==", value: "East" }),
"net_epa isNull": teams({ key: "net_epa", op: "isNull" }),
'team in ["KC", "BUF"]': teams({ key: "team", op: "in", value: ["KC", "BUF"] }),
'qb matches "^J"': teams({ key: "qb", op: "matches", value: "^J" }),
"selectRows([0, 4]) (indices pass through)": selectRows([0, 4], STANDINGS),
"matches(wins > 14, KC)": kc !== undefined && matches({ key: "wins", op: ">", value: 14 }, kc),
};

Tags: table · predicate · matches · selectRows. All sdvtables examples.