@onsvisual/svelte-components 1.0.14 → 1.0.16

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/dist/js/utils.js +32 -20
  2. package/package.json +1 -1
package/dist/js/utils.js CHANGED
@@ -55,31 +55,43 @@ export const formatter = (dp = null) => {
55
55
  : new Intl.NumberFormat("en-GB").format;
56
56
  };
57
57
 
58
+ // Functions for natural sorting, regardless of type
59
+ // Less performant than d3.ascending and d3.descending, but handles mixed types
60
+ const collator = new Intl.Collator("en", { numeric: true });
61
+ const isSortable = (a, b) =>
62
+ typeof a === typeof b &&
63
+ (["number", "string", "boolean"].includes(typeof a) ||
64
+ (typeof a.getMonth === "function" && typeof b.getMonth === "function"));
65
+
58
66
  export const ascending = (a, b) =>
59
- a == null && b != null
60
- ? 1
61
- : b == null && a != null
62
- ? -1
63
- : a < b
67
+ a === b
68
+ ? 0
69
+ : a == null
70
+ ? 1
71
+ : b == null
64
72
  ? -1
65
- : a > b
66
- ? 1
67
- : a >= b
68
- ? 0
69
- : NaN;
73
+ : !isSortable(a, b)
74
+ ? collator.compare(a, b)
75
+ : a < b
76
+ ? -1
77
+ : a > b
78
+ ? 1
79
+ : 0;
70
80
 
71
81
  export const descending = (a, b) =>
72
- a == null && b != null
73
- ? 1
74
- : b == null && a != null
75
- ? -1
76
- : b < a
82
+ a === b
83
+ ? 0
84
+ : a == null
85
+ ? 1
86
+ : b == null
77
87
  ? -1
78
- : b > a
79
- ? 1
80
- : b >= a
81
- ? 0
82
- : NaN;
88
+ : !isSortable(a, b)
89
+ ? collator.compare(b, a)
90
+ : b < a
91
+ ? -1
92
+ : b > a
93
+ ? 1
94
+ : 0;
83
95
 
84
96
  export const sleep = (ms = 1000) => new Promise((resolve) => setTimeout(resolve, ms));
85
97
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onsvisual/svelte-components",
3
- "version": "1.0.14",
3
+ "version": "1.0.16",
4
4
  "scripts": {
5
5
  "dev": "vite dev",
6
6
  "build": "npm run build:package && npm run build:docs",