@sukramj/fabric-ui 0.1.2 → 0.2.0

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.
@@ -1,7 +1,20 @@
1
1
  // List-valued item fields (Column.multi, Filter.multi): the provider
2
- // carries several values as one comma-separated string.
2
+ // carries the values in `lists[key]`, one entry per value, so a value may
3
+ // contain a comma. The comma-separated `fields[key]` is the deprecated
4
+ // fallback (until v0.2.0), read only when `lists` has no such key.
3
5
 
4
- /** The values of a comma-separated field, trimmed, without empty ones. */
6
+ /** The part of an item the list helpers read. */
7
+ export type ListSource = {
8
+ fields: Record<string, string>;
9
+ lists?: Record<string, string[]>;
10
+ };
11
+
12
+ /**
13
+ * The values of a comma-separated field, trimmed, without empty ones.
14
+ *
15
+ * @deprecated The comma form is accepted until v0.2.0; read
16
+ * {@link itemValues}, which prefers `lists`.
17
+ */
5
18
  export function listValues(field: string | undefined): string[] {
6
19
  if (!field) return [];
7
20
  return field
@@ -11,22 +24,44 @@ export function listValues(field: string | undefined): string[] {
11
24
  }
12
25
 
13
26
  /**
14
- * Whether an item's field matches a filter value: by equality, or, for a
15
- * list-valued field, by membership in its list. An empty value never
27
+ * The values of an item's list-valued field: `lists[key]` when the key is
28
+ * present there (empty entries dropped, the rest as given), otherwise the
29
+ * comma split of `fields[key]`.
30
+ */
31
+ export function itemValues(item: ListSource, key: string): string[] {
32
+ const list = item.lists?.[key];
33
+ if (list) return list.filter((v) => v !== "");
34
+ return listValues(item.fields[key]);
35
+ }
36
+
37
+ /**
38
+ * Whether an item matches a filter value on a field: by equality, or, for
39
+ * a list-valued field, by membership in its list. An empty value never
16
40
  * matches a list.
17
41
  */
18
- export function fieldMatches(field: string | undefined, value: string, multi: boolean): boolean {
19
- if (!multi) return field === value;
42
+ export function fieldMatches(
43
+ item: ListSource,
44
+ key: string,
45
+ value: string,
46
+ multi: boolean,
47
+ ): boolean {
48
+ if (!multi) return item.fields[key] === value;
20
49
  const v = value.trim();
21
- return v !== "" && listValues(field).includes(v);
50
+ return v !== "" && itemValues(item, key).includes(v);
22
51
  }
23
52
 
24
53
  /** The distinct filter values of a field across items, sorted for display. */
25
- export function distinctValues(fields: (string | undefined)[], multi: boolean): string[] {
54
+ export function distinctValues(
55
+ items: ListSource[],
56
+ key: string,
57
+ multi: boolean,
58
+ ): string[] {
26
59
  const values = new Set<string>();
27
- for (const f of fields) {
28
- if (multi) for (const v of listValues(f)) values.add(v);
29
- else if (f) values.add(f);
60
+ for (const item of items) {
61
+ if (multi) for (const v of itemValues(item, key)) values.add(v);
62
+ else if (item.fields[key]) values.add(item.fields[key]);
30
63
  }
31
- return [...values].sort((a, b) => a.localeCompare(b, undefined, { sensitivity: "base", numeric: true }));
64
+ return [...values].sort((a, b) =>
65
+ a.localeCompare(b, undefined, { sensitivity: "base", numeric: true }),
66
+ );
32
67
  }