svelte-common 4.13.3 → 4.15.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/sorting.mjs +32 -6
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte-common",
3
- "version": "4.13.3",
3
+ "version": "4.15.0",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
package/src/sorting.mjs CHANGED
@@ -6,7 +6,7 @@ export const SORT_OTHER = "other";
6
6
  export const orderByCycle = {
7
7
  [SORT_NONE]: SORT_ASCENDING,
8
8
  [SORT_ASCENDING]: SORT_DESCENDING,
9
- [SORT_DESCENDING]: SORT_NONE
9
+ [SORT_DESCENDING]: SORT_ASCENDING
10
10
  };
11
11
 
12
12
  /**
@@ -77,21 +77,47 @@ export function sorter(sortBy, getters = {}) {
77
77
  case SORT_ASCENDING:
78
78
  return (a, b) => {
79
79
  const av = getter(a);
80
+ const bv = getter(b);
81
+ if (av === undefined) {
82
+ return -1;
83
+ }
84
+ if (bv === undefined) {
85
+ return 1;
86
+ }
87
+
80
88
  if (typeof av === "string") {
81
- const bv = getter(b);
82
89
  return typeof bv === "string" ? av.localeCompare(bv) : 1;
83
90
  }
84
- return -1;
91
+ if (av instanceof Date) {
92
+ const avt = av.getTime();
93
+ const bvt = bv.getTime();
94
+ return avt > bvt ? 1 : avt === bvt ? 0 : -1;
95
+ }
96
+
97
+ return av > bv ? 1 : av == bv ? 0 : -1;
85
98
  };
86
99
 
87
100
  case SORT_DESCENDING:
88
- return (a, b) => {
101
+ return (b, a) => {
89
102
  const av = getter(a);
103
+ const bv = getter(b);
104
+ if (av === undefined) {
105
+ return -1;
106
+ }
107
+ if (bv === undefined) {
108
+ return 1;
109
+ }
110
+
90
111
  if (typeof av === "string") {
91
- const bv = getter(b);
92
112
  return typeof bv === "string" ? av.localeCompare(bv) : 1;
93
113
  }
94
- return -1;
114
+ if (av instanceof Date) {
115
+ const avt = av.getTime();
116
+ const bvt = bv.getTime();
117
+ return avt > bvt ? 1 : avt === bvt ? 0 : -1;
118
+ }
119
+
120
+ return av > bv ? 1 : av == bv ? 0 : -1;
95
121
  };
96
122
  }
97
123
  }