@soleil-se/app-util 5.13.0 → 5.14.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.
package/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,12 @@ All notable changes to this project will be documented in this file.
|
|
|
7
7
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
8
8
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
9
9
|
|
|
10
|
+
## [5.14.0] - 2026-04-24
|
|
11
|
+
|
|
12
|
+
- `localizedCompareBy` now accepts objects with a `key` and optional `order` (`'asc'` | `'desc'`,
|
|
13
|
+
defaults to `'asc'`) in addition to plain strings, enabling per-property sort direction.
|
|
14
|
+
Existing string-based usage is unchanged.
|
|
15
|
+
|
|
10
16
|
## [5.13.0] - 2026-01-16
|
|
11
17
|
|
|
12
18
|
- Add comparator functions `localizedCompare` and `localizedCompareBy` to sort strings and objects
|
|
@@ -11,10 +11,19 @@ export function localizedCompare(a: string, b: string): number;
|
|
|
11
11
|
* Creates a comparator function for sorting objects by a specific property or properties.
|
|
12
12
|
* The property values are compared using localized string comparison.
|
|
13
13
|
* When given an array of properties, sorts by the first property, then by the second if equal, etc.
|
|
14
|
-
*
|
|
14
|
+
* Each property can be a string or an object with a `key` and optional `order` ('asc' | 'desc', defaults to 'asc').
|
|
15
|
+
* @param {string|{key: string, order?: 'asc'|'desc'}|Array<string|{key: string, order?: 'asc'|'desc'}>} prop - The property name(s) to compare by.
|
|
15
16
|
* @returns {(a: Object, b: Object) => number} A comparator function that accepts two objects and returns their sort order.
|
|
16
17
|
* @example
|
|
17
18
|
* arr.sort(localizedCompareBy('title'))
|
|
18
19
|
* arr.sort(localizedCompareBy(['lastName', 'firstName']))
|
|
20
|
+
* arr.sort(localizedCompareBy({ key: 'firstName', order: 'desc' }))
|
|
21
|
+
* arr.sort(localizedCompareBy([{ key: 'lastName', order: 'asc' }, { key: 'firstName', order: 'desc' }]))
|
|
19
22
|
*/
|
|
20
|
-
export function localizedCompareBy(prop: string |
|
|
23
|
+
export function localizedCompareBy(prop: string | {
|
|
24
|
+
key: string;
|
|
25
|
+
order?: 'asc' | 'desc';
|
|
26
|
+
} | Array<string | {
|
|
27
|
+
key: string;
|
|
28
|
+
order?: 'asc' | 'desc';
|
|
29
|
+
}>): (a: any, b: any) => number;
|
|
@@ -82,20 +82,28 @@ export function localizedCompare(a, b) {
|
|
|
82
82
|
* Creates a comparator function for sorting objects by a specific property or properties.
|
|
83
83
|
* The property values are compared using localized string comparison.
|
|
84
84
|
* When given an array of properties, sorts by the first property, then by the second if equal, etc.
|
|
85
|
-
*
|
|
85
|
+
* Each property can be a string or an object with a `key` and optional `order` ('asc' | 'desc', defaults to 'asc').
|
|
86
|
+
* @param {string|{key: string, order?: 'asc'|'desc'}|Array<string|{key: string, order?: 'asc'|'desc'}>} prop - The property name(s) to compare by.
|
|
86
87
|
* @returns {(a: Object, b: Object) => number} A comparator function that accepts two objects and returns their sort order.
|
|
87
88
|
* @example
|
|
88
89
|
* arr.sort(localizedCompareBy('title'))
|
|
89
90
|
* arr.sort(localizedCompareBy(['lastName', 'firstName']))
|
|
91
|
+
* arr.sort(localizedCompareBy({ key: 'firstName', order: 'desc' }))
|
|
92
|
+
* arr.sort(localizedCompareBy([{ key: 'lastName', order: 'asc' }, { key: 'firstName', order: 'desc' }]))
|
|
90
93
|
*/
|
|
91
94
|
export function localizedCompareBy(prop) {
|
|
92
|
-
const props = Array.isArray(prop) ? prop : [prop]
|
|
95
|
+
const props = (Array.isArray(prop) ? prop : [prop])
|
|
96
|
+
.map((p) => (typeof p === 'string'
|
|
97
|
+
? { key: p, order: 'asc' }
|
|
98
|
+
: { key: p.key, order: p.order ?? 'asc' }
|
|
99
|
+
));
|
|
93
100
|
|
|
94
101
|
return (a, b) => {
|
|
95
102
|
for (let i = 0; i < props.length; i += 1) {
|
|
96
|
-
const
|
|
103
|
+
const { key, order } = props[i];
|
|
104
|
+
const result = localizedCompare(a[key], b[key]);
|
|
97
105
|
if (result !== 0) {
|
|
98
|
-
return result;
|
|
106
|
+
return order === 'desc' ? -result : result;
|
|
99
107
|
}
|
|
100
108
|
}
|
|
101
109
|
return 0;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@soleil-se/app-util",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.14.0",
|
|
4
4
|
"description": "Utility functions for WebApps, RESTApps and Widgets in Sitevision.",
|
|
5
5
|
"main": "./common/index.js",
|
|
6
6
|
"author": "Soleil AB",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"@sitevision/api": "*"
|
|
24
24
|
},
|
|
25
25
|
"scripts": {
|
|
26
|
-
"create-type-definitions": "node ../../utils/createTypeDefinitions.js ./common/index.js ./client/index.js ./client/svelte/index.js ./client/svelte/3/index.js ./client/svelte/4/index.js ./client/svelte/5/index.js ./server/index.js ./server/svelte/index.js ./server/svelte/3/index.js ./server/svelte/4/index.js ./server/svelte/5/index.js ./server/app-data/index.js ./server/global-app-data/index.js"
|
|
26
|
+
"create-type-definitions": "node ../../utils/createTypeDefinitions.js ./common/index.js ./common/localized-compare/index.js ./client/index.js ./client/svelte/index.js ./client/svelte/3/index.js ./client/svelte/4/index.js ./client/svelte/5/index.js ./server/index.js ./server/svelte/index.js ./server/svelte/3/index.js ./server/svelte/4/index.js ./server/svelte/5/index.js ./server/app-data/index.js ./server/global-app-data/index.js"
|
|
27
27
|
},
|
|
28
|
-
"gitHead": "
|
|
28
|
+
"gitHead": "1685a5874b85ff0d0b005c815b7207cde49b3e72"
|
|
29
29
|
}
|