ismx-nexo-node-app 0.4.159 → 0.4.162
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/dist/js/business/BusinessState.js +3 -0
- package/dist/js/business/utils/ArrayUtils.js +21 -0
- package/dist/types/business/BusinessState.d.ts +1 -0
- package/dist/types/business/utils/ArrayUtils.d.ts +13 -0
- package/package.json +1 -1
- package/src/main/node/business/BusinessState.ts +4 -0
- package/src/main/node/business/utils/ArrayUtils.ts +19 -0
|
@@ -24,6 +24,9 @@ class BusinessState {
|
|
|
24
24
|
observe(listener, runOnObserve) {
|
|
25
25
|
return this.observeId("" + ++this.listenerCount, listener, runOnObserve);
|
|
26
26
|
}
|
|
27
|
+
observeRun(listener, runOnObserve) {
|
|
28
|
+
return this.observe(listener, true);
|
|
29
|
+
}
|
|
27
30
|
observeId(id, listener, runOnObserve) {
|
|
28
31
|
if (runOnObserve)
|
|
29
32
|
try {
|
|
@@ -131,5 +131,26 @@ class ArrayUtils {
|
|
|
131
131
|
static sorted(array, compareFn) {
|
|
132
132
|
return [...array].sort(compareFn);
|
|
133
133
|
}
|
|
134
|
+
/**
|
|
135
|
+
* Returns a new array sorted by a specified property key or by a value returned from a function.
|
|
136
|
+
* The sorting can be performed in ascending ('asc') or descending ('desc') order.
|
|
137
|
+
*
|
|
138
|
+
* @param {T[]} array - The array to sort.
|
|
139
|
+
* @param {keyof T | ((t: T) => number)} key - The property key to sort by, or a function that returns a value for sorting.
|
|
140
|
+
* If a property key is provided, each element is sorted based on that property.
|
|
141
|
+
* If a function is provided, each element is sorted based on the value returned by the function.
|
|
142
|
+
* @param {'asc' | 'desc'} [sortFn='desc'] - The sort direction: 'asc' for ascending order, 'desc' for descending order (default is 'desc').
|
|
143
|
+
* @return {T[]} A new array sorted by the given property or function result, in the specified order.
|
|
144
|
+
* @template T
|
|
145
|
+
*/
|
|
146
|
+
static sortedBy(array, key, sortFn = 'desc') {
|
|
147
|
+
let valueFn = typeof key === 'function' ? key : (t) => t[key];
|
|
148
|
+
if (sortFn === 'desc')
|
|
149
|
+
return ArrayUtils.sorted(array, (a, b) => valueFn(b) - valueFn(a));
|
|
150
|
+
if (sortFn === 'asc')
|
|
151
|
+
return ArrayUtils.sorted(array, (a, b) => valueFn(a) - valueFn(b));
|
|
152
|
+
else
|
|
153
|
+
return array;
|
|
154
|
+
}
|
|
134
155
|
}
|
|
135
156
|
exports.default = ArrayUtils;
|
|
@@ -6,6 +6,7 @@ export default class BusinessState<Model> {
|
|
|
6
6
|
private listenerCount;
|
|
7
7
|
init(): Promise<void>;
|
|
8
8
|
observe(listener: (data: Model) => any, runOnObserve?: boolean): string;
|
|
9
|
+
observeRun(listener: (data: Model) => any, runOnObserve?: boolean): string;
|
|
9
10
|
observeId(id: string, listener: (data: Model) => any, runOnObserve?: boolean): string;
|
|
10
11
|
remove(id: string): void;
|
|
11
12
|
protected notify(data?: Model): void;
|
|
@@ -85,4 +85,17 @@ export default abstract class ArrayUtils {
|
|
|
85
85
|
* @template T
|
|
86
86
|
*/
|
|
87
87
|
static sorted<T>(array: T[], compareFn?: (a: T, b: T) => number): T[];
|
|
88
|
+
/**
|
|
89
|
+
* Returns a new array sorted by a specified property key or by a value returned from a function.
|
|
90
|
+
* The sorting can be performed in ascending ('asc') or descending ('desc') order.
|
|
91
|
+
*
|
|
92
|
+
* @param {T[]} array - The array to sort.
|
|
93
|
+
* @param {keyof T | ((t: T) => number)} key - The property key to sort by, or a function that returns a value for sorting.
|
|
94
|
+
* If a property key is provided, each element is sorted based on that property.
|
|
95
|
+
* If a function is provided, each element is sorted based on the value returned by the function.
|
|
96
|
+
* @param {'asc' | 'desc'} [sortFn='desc'] - The sort direction: 'asc' for ascending order, 'desc' for descending order (default is 'desc').
|
|
97
|
+
* @return {T[]} A new array sorted by the given property or function result, in the specified order.
|
|
98
|
+
* @template T
|
|
99
|
+
*/
|
|
100
|
+
static sortedBy<T>(array: T[], key: keyof T | ((t: T) => number), sortFn?: 'asc' | 'desc'): T[];
|
|
88
101
|
}
|
package/package.json
CHANGED
|
@@ -14,6 +14,10 @@ export default class BusinessState<Model>
|
|
|
14
14
|
return this.observeId(""+ ++this.listenerCount, listener, runOnObserve);
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
+
observeRun(listener: (data: Model) => any, runOnObserve?: boolean): string {
|
|
18
|
+
return this.observe(listener, true);
|
|
19
|
+
}
|
|
20
|
+
|
|
17
21
|
observeId(id: string, listener: (data: Model) => any, runOnObserve?: boolean): string {
|
|
18
22
|
if (runOnObserve) try { listener(this.data); } catch {}
|
|
19
23
|
this.listeners[id] = listener;
|
|
@@ -124,4 +124,23 @@ export default abstract class ArrayUtils {
|
|
|
124
124
|
static sorted<T>(array: T[], compareFn?: (a: T, b: T) => number): T[] {
|
|
125
125
|
return [ ...array ].sort(compareFn);
|
|
126
126
|
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Returns a new array sorted by a specified property key or by a value returned from a function.
|
|
130
|
+
* The sorting can be performed in ascending ('asc') or descending ('desc') order.
|
|
131
|
+
*
|
|
132
|
+
* @param {T[]} array - The array to sort.
|
|
133
|
+
* @param {keyof T | ((t: T) => number)} key - The property key to sort by, or a function that returns a value for sorting.
|
|
134
|
+
* If a property key is provided, each element is sorted based on that property.
|
|
135
|
+
* If a function is provided, each element is sorted based on the value returned by the function.
|
|
136
|
+
* @param {'asc' | 'desc'} [sortFn='desc'] - The sort direction: 'asc' for ascending order, 'desc' for descending order (default is 'desc').
|
|
137
|
+
* @return {T[]} A new array sorted by the given property or function result, in the specified order.
|
|
138
|
+
* @template T
|
|
139
|
+
*/
|
|
140
|
+
static sortedBy<T>(array: T[], key: keyof T | ((t: T) => number), sortFn: 'asc' | 'desc' = 'desc'): T[] {
|
|
141
|
+
let valueFn: (t: T) => number = typeof key === 'function' ? key : (t) => t[key] as number;
|
|
142
|
+
if (sortFn === 'desc') return ArrayUtils.sorted(array, (a, b) => valueFn(b) - valueFn(a));
|
|
143
|
+
if (sortFn === 'asc') return ArrayUtils.sorted(array, (a, b) => valueFn(a) - valueFn(b));
|
|
144
|
+
else return array;
|
|
145
|
+
}
|
|
127
146
|
}
|