@lavalogic/scoria 0.37.50 → 0.37.51

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.
@@ -9,6 +9,18 @@ import type { SortingState as SortingStateArray } from '../DataRepository/Sortin
9
9
  * pays no extra branching per comparison.
10
10
  */
11
11
  export declare function comparableSortingFn<T>(id: keyof T, desc: boolean): (a: T, b: T) => -1 | 0 | 1;
12
+ /**
13
+ * Comparator factory for columns whose sort key comes from a value
14
+ * accessor rather than a row property. `DisplayDef` ids are synthetic, so
15
+ * the row carries no property under the column id — the accessor (the
16
+ * `col.display` `value` thunk) is the only available sort key.
17
+ *
18
+ * A Promise-returning accessor (an async `value` thunk) is not
19
+ * synchronously comparable, so such pairs compare equal — the column is a
20
+ * stable no-op unless the consumer supplies an explicit `getSortingFn`.
21
+ * Nullish results sort last in both directions.
22
+ */
23
+ export declare function accessorSortingFn<T, V>(accessorFn: ((row: T, index: number) => V) | undefined, desc: boolean): (a: T, b: T) => -1 | 0 | 1;
12
24
  /**
13
25
  * Comparator factory for `CheckboxDef` columns. Boolean-coercion compares
14
26
  * truthy-vs-falsy so undefined / null / 0 collapse to "unchecked".
@@ -47,6 +47,39 @@ export function comparableSortingFn(id, desc) {
47
47
  return 0;
48
48
  };
49
49
  }
50
+ /**
51
+ * Comparator factory for columns whose sort key comes from a value
52
+ * accessor rather than a row property. `DisplayDef` ids are synthetic, so
53
+ * the row carries no property under the column id — the accessor (the
54
+ * `col.display` `value` thunk) is the only available sort key.
55
+ *
56
+ * A Promise-returning accessor (an async `value` thunk) is not
57
+ * synchronously comparable, so such pairs compare equal — the column is a
58
+ * stable no-op unless the consumer supplies an explicit `getSortingFn`.
59
+ * Nullish results sort last in both directions.
60
+ */
61
+ export function accessorSortingFn(accessorFn, desc) {
62
+ if (!accessorFn) {
63
+ return () => 0;
64
+ }
65
+ return (a, b) => {
66
+ const av = accessorFn(a, 0);
67
+ const bv = accessorFn(b, 0);
68
+ if (av == null && bv != null) {
69
+ return 1;
70
+ }
71
+ if (bv == null && av != null) {
72
+ return -1;
73
+ }
74
+ if (av < bv) {
75
+ return desc ? 1 : -1;
76
+ }
77
+ if (av > bv) {
78
+ return desc ? -1 : 1;
79
+ }
80
+ return 0;
81
+ };
82
+ }
50
83
  /**
51
84
  * Comparator factory for `CheckboxDef` columns. Boolean-coercion compares
52
85
  * truthy-vs-falsy so undefined / null / 0 collapse to "unchecked".
@@ -175,8 +208,12 @@ export class SortingState {
175
208
  if (def.getSortingFn != undefined) {
176
209
  return def.getSortingFn(desc);
177
210
  }
178
- if (def instanceof DisplayDef ||
179
- def instanceof TextInputDef ||
211
+ if (def instanceof DisplayDef) {
212
+ // `DisplayDef` ids are synthetic — the row has no property under
213
+ // `id` — so sort by the column's value accessor, not `row[id]`.
214
+ return accessorSortingFn(def.accessorFn, desc);
215
+ }
216
+ if (def instanceof TextInputDef ||
180
217
  def instanceof NumberInputDef ||
181
218
  def instanceof DateInputDef) {
182
219
  return comparableSortingFn(id, desc);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@lavalogic/scoria",
3
3
  "description": "Svelte components used for the FloWMS Web Frontend",
4
- "version": "0.37.50",
4
+ "version": "0.37.51",
5
5
  "publishConfig": {
6
6
  "access": "restricted"
7
7
  },