@canonical/design-system 0.2.1 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,55 @@
1
+ import type { RowFilter } from "../config/types.js";
2
+
3
+ /**
4
+ * Coerce a normalized cell value to a comparable/measurable string.
5
+ *
6
+ * The provider normalizes lookups to `{ id, name }` and strips whole-value
7
+ * Markdown fences, so a cell is typically a string; a lookup object is reduced
8
+ * to its `name`. Anything else stringifies. Used by the row filter to read the
9
+ * name column and to measure a body column's length.
10
+ *
11
+ * @param value - The raw normalized cell value.
12
+ * @returns The cell as a trimmed string ("" for null/undefined).
13
+ */
14
+ function cellText(value: unknown): string {
15
+ if (value === null || value === undefined) return "";
16
+ if (typeof value === "string") return value.trim();
17
+ if (
18
+ typeof value === "object" &&
19
+ "name" in (value as Record<string, unknown>)
20
+ ) {
21
+ const name = (value as { name?: unknown }).name;
22
+ return typeof name === "string" ? name.trim() : "";
23
+ }
24
+ return String(value).trim();
25
+ }
26
+
27
+ /**
28
+ * Whether a row passes a table's {@link RowFilter} (logical AND over every
29
+ * declared condition). A row is kept when every condition holds; an absent
30
+ * filter keeps every row (the caller guards that case).
31
+ *
32
+ * @param row - The normalized row (column name → cell value).
33
+ * @param filter - The include-filter to apply.
34
+ * @returns True when the row should be emitted.
35
+ */
36
+ export function rowPassesFilter(
37
+ row: Record<string, unknown>,
38
+ filter: RowFilter,
39
+ ): boolean {
40
+ const { nameIn, nonEmpty } = filter;
41
+
42
+ if (nameIn) {
43
+ const actual = cellText(row[nameIn.column]).toLowerCase();
44
+ const wanted = nameIn.values.map((v) => v.trim().toLowerCase());
45
+ if (!wanted.includes(actual)) return false;
46
+ }
47
+
48
+ if (nonEmpty) {
49
+ for (const { column, minLength } of nonEmpty) {
50
+ if (cellText(row[column]).length < (minLength ?? 1)) return false;
51
+ }
52
+ }
53
+
54
+ return true;
55
+ }
@@ -2,6 +2,7 @@ import type { TransformConfig } from "../config/types.js";
2
2
  import { GraphStore, PrefixMap } from "../graph/index.js";
3
3
  import expectedTables from "./expectedTables.js";
4
4
  import resolveReferences from "./resolveReferences.js";
5
+ import { rowPassesFilter } from "./rowFilter.js";
5
6
  import transformRow from "./transformRow.js";
6
7
  import type {
7
8
  InlineDataMap,
@@ -116,9 +117,14 @@ export default function transform(
116
117
  prefixes.add(prefix, namespace);
117
118
  }
118
119
 
119
- // Transform each row
120
+ // Transform each row, skipping any excluded by an optional include-filter
121
+ // (e.g. Concepts narrowed to the named, non-empty subset).
122
+ const { rowFilter } = tableConfig;
120
123
  let subjectCount = 0;
121
124
  for (const row of tableData) {
125
+ if (rowFilter && !rowPassesFilter(row, rowFilter)) {
126
+ continue;
127
+ }
122
128
  const subjectUri = transformRow(
123
129
  row,
124
130
  tableConfig,