svelte-common 4.19.9 → 4.19.11

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/sorting.mjs +13 -9
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte-common",
3
- "version": "4.19.9",
3
+ "version": "4.19.11",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
package/src/sorting.mjs CHANGED
@@ -9,6 +9,8 @@ export const orderByCycle = {
9
9
  [SORT_DESCENDING]: SORT_ASCENDING
10
10
  };
11
11
 
12
+ const ARIA_SORT = "aria-sort";
13
+
12
14
  /**
13
15
  * Deliver next value in the order by cycle.
14
16
  * SORT_NONE -> SORT_ASCENDING -> SORT_DESCENDING -> SORT_NONE ...
@@ -21,17 +23,17 @@ export function toggleOrderBy(orderBy) {
21
23
 
22
24
  /**
23
25
  * Add sortable toggle button to a th node.
24
- * Synchronizes store value with th nodes "aria-sort" attribute.
26
+ * Synchronizes store value with the nodes "aria-sort" attribute.
25
27
  * @param {Node} the header node
26
28
  * @param {WritableStore} to keep in sync with sorting properties
27
29
  */
28
30
  export function sortable(th, store) {
29
31
  const storeSubscription = store.subscribe(orderBy =>
30
- th.setAttribute("aria-sort", orderBy[th.id] || SORT_NONE)
32
+ th.setAttribute(ARIA_SORT, orderBy[th.id] || SORT_NONE)
31
33
  );
32
34
 
33
- if (!th.getAttribute("aria-sort")) {
34
- th.setAttribute("aria-sort", SORT_NONE);
35
+ if (!th.getAttribute(ARIA_SORT)) {
36
+ th.setAttribute(ARIA_SORT, SORT_NONE);
35
37
  }
36
38
 
37
39
  const button = document.createElement("button");
@@ -40,16 +42,16 @@ export function sortable(th, store) {
40
42
  button.onclick = () => {
41
43
  const orderBy = {};
42
44
 
43
- th.setAttribute("aria-sort", toggleOrderBy(th.getAttribute("aria-sort")));
45
+ th.setAttribute(ARIA_SORT, toggleOrderBy(th.getAttribute(ARIA_SORT)));
44
46
 
45
47
  for (const peer of th.parentElement.children) {
46
- let sort = peer.getAttribute("aria-sort");
48
+ let sort = peer.getAttribute(ARIA_SORT);
47
49
 
48
50
  if (sort) {
49
51
  if (peer !== th) {
50
52
  if (sort !== SORT_NONE) {
51
53
  sort = SORT_NONE;
52
- peer.setAttribute("aria-sort", sort);
54
+ peer.setAttribute(ARIA_SORT, sort);
53
55
  }
54
56
  }
55
57
 
@@ -88,12 +90,13 @@ export function sorter(sortBy, getters = {}) {
88
90
  rev = -1;
89
91
 
90
92
  case SORT_ASCENDING:
93
+
91
94
  return (a, b) => {
92
95
  let av = getter(a);
93
96
  let bv = getter(b);
94
97
 
95
98
  if (av === undefined) {
96
- return -rev;
99
+ return bv === undefined ? 0 : -rev;
97
100
  }
98
101
  if (bv === undefined) {
99
102
  return rev;
@@ -104,7 +107,8 @@ export function sorter(sortBy, getters = {}) {
104
107
  switch (typeof bv) {
105
108
  case "number":
106
109
  case "string":
107
- return av.localeCompare(bv);
110
+ const r = av.localeCompare(bv);
111
+ return r ? r * rev : r;
108
112
  }
109
113
  }
110
114