@etsoo/shared 1.2.11 → 1.2.13
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/__tests__/ArrayUtils.ts +31 -1
- package/__tests__/ExtendUtils.ts +2 -6
- package/lib/cjs/ArrayUtils.js +3 -6
- package/lib/cjs/ExtendUtils.d.ts +8 -0
- package/lib/cjs/ExtendUtils.js +47 -8
- package/lib/mjs/ArrayUtils.js +3 -6
- package/lib/mjs/ExtendUtils.d.ts +8 -0
- package/lib/mjs/ExtendUtils.js +47 -8
- package/package.json +4 -4
- package/src/ArrayUtils.ts +2 -3
- package/src/ExtendUtils.ts +54 -13
package/__tests__/ArrayUtils.ts
CHANGED
|
@@ -63,7 +63,7 @@ test('Tests for maxItem / minItem', () => {
|
|
|
63
63
|
expect(emptyItems.maxItem('amount')).toBeUndefined();
|
|
64
64
|
});
|
|
65
65
|
|
|
66
|
-
test('Tests for sortIds', () => {
|
|
66
|
+
test('Tests for sortIds 1', () => {
|
|
67
67
|
const source = [
|
|
68
68
|
{
|
|
69
69
|
id: 'zh-Hans',
|
|
@@ -91,3 +91,33 @@ test('Tests for sortIds', () => {
|
|
|
91
91
|
const ids = source.map((s) => s.id);
|
|
92
92
|
expect(ids).toStrictEqual(['en', 'zh-Hans', 'zh-Hant', 'fr', 'de']);
|
|
93
93
|
});
|
|
94
|
+
|
|
95
|
+
test('Tests for sortIds 2', () => {
|
|
96
|
+
const source = [
|
|
97
|
+
{ id: 'AUD', label: '澳元' },
|
|
98
|
+
{ id: 'CAD', label: '加元' },
|
|
99
|
+
{ id: 'CNY', label: '人民币' },
|
|
100
|
+
{ id: 'EUR', label: '欧元' },
|
|
101
|
+
{ id: 'GBP', label: '英镑' },
|
|
102
|
+
{ id: 'HKD', label: '港币' },
|
|
103
|
+
{ id: 'JPY', label: '日元' },
|
|
104
|
+
{ id: 'NZD', label: '纽币' },
|
|
105
|
+
{ id: 'SGD', label: '新币' },
|
|
106
|
+
{ id: 'USD', label: '美元' }
|
|
107
|
+
];
|
|
108
|
+
source.sortByProperty('id', ['USD', 'CNY']);
|
|
109
|
+
|
|
110
|
+
const ids = source.map((s) => s.id);
|
|
111
|
+
expect(ids).toStrictEqual([
|
|
112
|
+
'USD',
|
|
113
|
+
'CNY',
|
|
114
|
+
'AUD',
|
|
115
|
+
'CAD',
|
|
116
|
+
'EUR',
|
|
117
|
+
'GBP',
|
|
118
|
+
'HKD',
|
|
119
|
+
'JPY',
|
|
120
|
+
'NZD',
|
|
121
|
+
'SGD'
|
|
122
|
+
]);
|
|
123
|
+
});
|
package/__tests__/ExtendUtils.ts
CHANGED
|
@@ -27,19 +27,15 @@ test('Tests for delayedExecutor', () => {
|
|
|
27
27
|
// Arrange
|
|
28
28
|
const f = jest.fn();
|
|
29
29
|
|
|
30
|
-
jest.useFakeTimers();
|
|
31
|
-
jest.spyOn(global, 'setTimeout');
|
|
32
|
-
|
|
33
30
|
const e = ExtendUtils.delayedExecutor(f, 50);
|
|
34
31
|
|
|
35
32
|
e.call(1, false, 'a');
|
|
36
33
|
expect(e.isRunning()).toBeTruthy();
|
|
37
34
|
|
|
38
35
|
e.call(2, true, 'b');
|
|
39
|
-
expect(setTimeout).toHaveBeenCalledTimes(2);
|
|
40
36
|
|
|
41
|
-
|
|
42
|
-
|
|
37
|
+
expect(f).toBeCalledTimes(0);
|
|
38
|
+
e.clear();
|
|
43
39
|
expect(e.isRunning()).toBeFalsy();
|
|
44
40
|
});
|
|
45
41
|
|
package/lib/cjs/ArrayUtils.js
CHANGED
|
@@ -47,12 +47,9 @@ Array.prototype.sortByProperty = function (property, values) {
|
|
|
47
47
|
const bi = values.indexOf(b[property]);
|
|
48
48
|
if (ai === bi)
|
|
49
49
|
return 0;
|
|
50
|
-
if (ai < 0)
|
|
51
|
-
return bi;
|
|
52
|
-
|
|
53
|
-
return -ai;
|
|
54
|
-
else
|
|
55
|
-
return ai - bi;
|
|
50
|
+
if (ai < 0 || bi < 0)
|
|
51
|
+
return bi === 0 ? 1 : bi;
|
|
52
|
+
return ai - bi;
|
|
56
53
|
});
|
|
57
54
|
};
|
|
58
55
|
Array.prototype.sum = function (field) {
|
package/lib/cjs/ExtendUtils.d.ts
CHANGED
|
@@ -27,4 +27,12 @@ export declare namespace ExtendUtils {
|
|
|
27
27
|
* @param delay Delay miniseconds
|
|
28
28
|
*/
|
|
29
29
|
function sleep(delay?: number): Promise<unknown>;
|
|
30
|
+
/**
|
|
31
|
+
* Wait for condition meets and execute callback
|
|
32
|
+
* requestAnimationFrame to replace setTimeout
|
|
33
|
+
* @param callback Callback
|
|
34
|
+
* @param checkReady Check ready, when it's a number, similar to setTimeout
|
|
35
|
+
* @returns cancel callback
|
|
36
|
+
*/
|
|
37
|
+
function waitFor(callback: () => void, checkReady: ((spanTime: number) => boolean) | number): () => void;
|
|
30
38
|
}
|
package/lib/cjs/ExtendUtils.js
CHANGED
|
@@ -30,7 +30,7 @@ var ExtendUtils;
|
|
|
30
30
|
* @returns Result
|
|
31
31
|
*/
|
|
32
32
|
function delayedExecutor(func, delayMiliseconds) {
|
|
33
|
-
let
|
|
33
|
+
let cancel;
|
|
34
34
|
return {
|
|
35
35
|
/**
|
|
36
36
|
* Call the function
|
|
@@ -39,18 +39,19 @@ var ExtendUtils;
|
|
|
39
39
|
*/
|
|
40
40
|
call(miliseconds, ...args) {
|
|
41
41
|
this.clear();
|
|
42
|
-
|
|
42
|
+
cancel = waitFor(() => {
|
|
43
43
|
func(...args);
|
|
44
|
-
|
|
45
|
-
}, miliseconds !== null && miliseconds !== void 0 ? miliseconds : delayMiliseconds
|
|
44
|
+
cancel = undefined;
|
|
45
|
+
}, miliseconds !== null && miliseconds !== void 0 ? miliseconds : delayMiliseconds);
|
|
46
46
|
},
|
|
47
47
|
/**
|
|
48
48
|
* Clear
|
|
49
49
|
*/
|
|
50
50
|
clear() {
|
|
51
51
|
if (this.isRunning()) {
|
|
52
|
-
|
|
53
|
-
|
|
52
|
+
if (cancel)
|
|
53
|
+
cancel();
|
|
54
|
+
cancel = undefined;
|
|
54
55
|
}
|
|
55
56
|
},
|
|
56
57
|
/**
|
|
@@ -58,7 +59,7 @@ var ExtendUtils;
|
|
|
58
59
|
* @returns Result
|
|
59
60
|
*/
|
|
60
61
|
isRunning() {
|
|
61
|
-
return
|
|
62
|
+
return cancel != null;
|
|
62
63
|
}
|
|
63
64
|
};
|
|
64
65
|
}
|
|
@@ -76,8 +77,46 @@ var ExtendUtils;
|
|
|
76
77
|
*/
|
|
77
78
|
function sleep(delay = 0) {
|
|
78
79
|
return new Promise((resolve) => {
|
|
79
|
-
|
|
80
|
+
waitFor(() => resolve, delay);
|
|
80
81
|
});
|
|
81
82
|
}
|
|
82
83
|
ExtendUtils.sleep = sleep;
|
|
84
|
+
/**
|
|
85
|
+
* Wait for condition meets and execute callback
|
|
86
|
+
* requestAnimationFrame to replace setTimeout
|
|
87
|
+
* @param callback Callback
|
|
88
|
+
* @param checkReady Check ready, when it's a number, similar to setTimeout
|
|
89
|
+
* @returns cancel callback
|
|
90
|
+
*/
|
|
91
|
+
function waitFor(callback, checkReady) {
|
|
92
|
+
let startTime;
|
|
93
|
+
let requestID;
|
|
94
|
+
function doWait(time) {
|
|
95
|
+
// Reset request id
|
|
96
|
+
requestID = undefined;
|
|
97
|
+
// First time
|
|
98
|
+
if (startTime == null)
|
|
99
|
+
startTime = time;
|
|
100
|
+
// Ignore
|
|
101
|
+
if (startTime === 0)
|
|
102
|
+
return;
|
|
103
|
+
const spanTime = startTime == null || time == null ? 0 : time - startTime;
|
|
104
|
+
if (time != null &&
|
|
105
|
+
(typeof checkReady === 'number'
|
|
106
|
+
? spanTime >= checkReady
|
|
107
|
+
: checkReady(spanTime))) {
|
|
108
|
+
callback();
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
requestID = requestAnimationFrame(doWait);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
doWait();
|
|
115
|
+
return () => {
|
|
116
|
+
if (requestID)
|
|
117
|
+
cancelAnimationFrame(requestID);
|
|
118
|
+
startTime = undefined;
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
ExtendUtils.waitFor = waitFor;
|
|
83
122
|
})(ExtendUtils || (exports.ExtendUtils = ExtendUtils = {}));
|
package/lib/mjs/ArrayUtils.js
CHANGED
|
@@ -41,12 +41,9 @@ Array.prototype.sortByProperty = function (property, values) {
|
|
|
41
41
|
const bi = values.indexOf(b[property]);
|
|
42
42
|
if (ai === bi)
|
|
43
43
|
return 0;
|
|
44
|
-
if (ai < 0)
|
|
45
|
-
return bi;
|
|
46
|
-
|
|
47
|
-
return -ai;
|
|
48
|
-
else
|
|
49
|
-
return ai - bi;
|
|
44
|
+
if (ai < 0 || bi < 0)
|
|
45
|
+
return bi === 0 ? 1 : bi;
|
|
46
|
+
return ai - bi;
|
|
50
47
|
});
|
|
51
48
|
};
|
|
52
49
|
Array.prototype.sum = function (field) {
|
package/lib/mjs/ExtendUtils.d.ts
CHANGED
|
@@ -27,4 +27,12 @@ export declare namespace ExtendUtils {
|
|
|
27
27
|
* @param delay Delay miniseconds
|
|
28
28
|
*/
|
|
29
29
|
function sleep(delay?: number): Promise<unknown>;
|
|
30
|
+
/**
|
|
31
|
+
* Wait for condition meets and execute callback
|
|
32
|
+
* requestAnimationFrame to replace setTimeout
|
|
33
|
+
* @param callback Callback
|
|
34
|
+
* @param checkReady Check ready, when it's a number, similar to setTimeout
|
|
35
|
+
* @returns cancel callback
|
|
36
|
+
*/
|
|
37
|
+
function waitFor(callback: () => void, checkReady: ((spanTime: number) => boolean) | number): () => void;
|
|
30
38
|
}
|
package/lib/mjs/ExtendUtils.js
CHANGED
|
@@ -27,7 +27,7 @@ export var ExtendUtils;
|
|
|
27
27
|
* @returns Result
|
|
28
28
|
*/
|
|
29
29
|
function delayedExecutor(func, delayMiliseconds) {
|
|
30
|
-
let
|
|
30
|
+
let cancel;
|
|
31
31
|
return {
|
|
32
32
|
/**
|
|
33
33
|
* Call the function
|
|
@@ -36,18 +36,19 @@ export var ExtendUtils;
|
|
|
36
36
|
*/
|
|
37
37
|
call(miliseconds, ...args) {
|
|
38
38
|
this.clear();
|
|
39
|
-
|
|
39
|
+
cancel = waitFor(() => {
|
|
40
40
|
func(...args);
|
|
41
|
-
|
|
42
|
-
}, miliseconds !== null && miliseconds !== void 0 ? miliseconds : delayMiliseconds
|
|
41
|
+
cancel = undefined;
|
|
42
|
+
}, miliseconds !== null && miliseconds !== void 0 ? miliseconds : delayMiliseconds);
|
|
43
43
|
},
|
|
44
44
|
/**
|
|
45
45
|
* Clear
|
|
46
46
|
*/
|
|
47
47
|
clear() {
|
|
48
48
|
if (this.isRunning()) {
|
|
49
|
-
|
|
50
|
-
|
|
49
|
+
if (cancel)
|
|
50
|
+
cancel();
|
|
51
|
+
cancel = undefined;
|
|
51
52
|
}
|
|
52
53
|
},
|
|
53
54
|
/**
|
|
@@ -55,7 +56,7 @@ export var ExtendUtils;
|
|
|
55
56
|
* @returns Result
|
|
56
57
|
*/
|
|
57
58
|
isRunning() {
|
|
58
|
-
return
|
|
59
|
+
return cancel != null;
|
|
59
60
|
}
|
|
60
61
|
};
|
|
61
62
|
}
|
|
@@ -73,8 +74,46 @@ export var ExtendUtils;
|
|
|
73
74
|
*/
|
|
74
75
|
function sleep(delay = 0) {
|
|
75
76
|
return new Promise((resolve) => {
|
|
76
|
-
|
|
77
|
+
waitFor(() => resolve, delay);
|
|
77
78
|
});
|
|
78
79
|
}
|
|
79
80
|
ExtendUtils.sleep = sleep;
|
|
81
|
+
/**
|
|
82
|
+
* Wait for condition meets and execute callback
|
|
83
|
+
* requestAnimationFrame to replace setTimeout
|
|
84
|
+
* @param callback Callback
|
|
85
|
+
* @param checkReady Check ready, when it's a number, similar to setTimeout
|
|
86
|
+
* @returns cancel callback
|
|
87
|
+
*/
|
|
88
|
+
function waitFor(callback, checkReady) {
|
|
89
|
+
let startTime;
|
|
90
|
+
let requestID;
|
|
91
|
+
function doWait(time) {
|
|
92
|
+
// Reset request id
|
|
93
|
+
requestID = undefined;
|
|
94
|
+
// First time
|
|
95
|
+
if (startTime == null)
|
|
96
|
+
startTime = time;
|
|
97
|
+
// Ignore
|
|
98
|
+
if (startTime === 0)
|
|
99
|
+
return;
|
|
100
|
+
const spanTime = startTime == null || time == null ? 0 : time - startTime;
|
|
101
|
+
if (time != null &&
|
|
102
|
+
(typeof checkReady === 'number'
|
|
103
|
+
? spanTime >= checkReady
|
|
104
|
+
: checkReady(spanTime))) {
|
|
105
|
+
callback();
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
requestID = requestAnimationFrame(doWait);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
doWait();
|
|
112
|
+
return () => {
|
|
113
|
+
if (requestID)
|
|
114
|
+
cancelAnimationFrame(requestID);
|
|
115
|
+
startTime = undefined;
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
ExtendUtils.waitFor = waitFor;
|
|
80
119
|
})(ExtendUtils || (ExtendUtils = {}));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@etsoo/shared",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.13",
|
|
4
4
|
"description": "TypeScript shared utilities and functions",
|
|
5
5
|
"main": "lib/cjs/index.js",
|
|
6
6
|
"module": "lib/mjs/index.js",
|
|
@@ -54,10 +54,10 @@
|
|
|
54
54
|
},
|
|
55
55
|
"homepage": "https://github.com/ETSOO/Shared#readme",
|
|
56
56
|
"devDependencies": {
|
|
57
|
-
"@types/jest": "^29.5.
|
|
57
|
+
"@types/jest": "^29.5.5",
|
|
58
58
|
"@types/lodash.isequal": "^4.5.6",
|
|
59
|
-
"jest": "^29.
|
|
60
|
-
"jest-environment-jsdom": "^29.
|
|
59
|
+
"jest": "^29.7.0",
|
|
60
|
+
"jest-environment-jsdom": "^29.7.0",
|
|
61
61
|
"ts-jest": "^29.1.1",
|
|
62
62
|
"typescript": "^5.2.2"
|
|
63
63
|
},
|
package/src/ArrayUtils.ts
CHANGED
|
@@ -151,9 +151,8 @@ Array.prototype.sortByProperty = function <T, P extends keyof T>(
|
|
|
151
151
|
const bi = values.indexOf(b[property]);
|
|
152
152
|
|
|
153
153
|
if (ai === bi) return 0;
|
|
154
|
-
if (ai < 0) return bi;
|
|
155
|
-
|
|
156
|
-
else return ai - bi;
|
|
154
|
+
if (ai < 0 || bi < 0) return bi === 0 ? 1 : bi;
|
|
155
|
+
return ai - bi;
|
|
157
156
|
});
|
|
158
157
|
};
|
|
159
158
|
|
package/src/ExtendUtils.ts
CHANGED
|
@@ -31,7 +31,7 @@ export namespace ExtendUtils {
|
|
|
31
31
|
func: (...args: P) => void,
|
|
32
32
|
delayMiliseconds: number
|
|
33
33
|
): DelayedExecutorType<P> {
|
|
34
|
-
let
|
|
34
|
+
let cancel: (() => void) | undefined;
|
|
35
35
|
return {
|
|
36
36
|
/**
|
|
37
37
|
* Call the function
|
|
@@ -40,14 +40,10 @@ export namespace ExtendUtils {
|
|
|
40
40
|
*/
|
|
41
41
|
call(miliseconds?: number, ...args: P) {
|
|
42
42
|
this.clear();
|
|
43
|
-
|
|
44
|
-
(...args
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
},
|
|
48
|
-
miliseconds ?? delayMiliseconds,
|
|
49
|
-
...args
|
|
50
|
-
);
|
|
43
|
+
cancel = waitFor(() => {
|
|
44
|
+
func(...args);
|
|
45
|
+
cancel = undefined;
|
|
46
|
+
}, miliseconds ?? delayMiliseconds);
|
|
51
47
|
},
|
|
52
48
|
|
|
53
49
|
/**
|
|
@@ -55,8 +51,8 @@ export namespace ExtendUtils {
|
|
|
55
51
|
*/
|
|
56
52
|
clear() {
|
|
57
53
|
if (this.isRunning()) {
|
|
58
|
-
|
|
59
|
-
|
|
54
|
+
if (cancel) cancel();
|
|
55
|
+
cancel = undefined;
|
|
60
56
|
}
|
|
61
57
|
},
|
|
62
58
|
|
|
@@ -65,7 +61,7 @@ export namespace ExtendUtils {
|
|
|
65
61
|
* @returns Result
|
|
66
62
|
*/
|
|
67
63
|
isRunning() {
|
|
68
|
-
return
|
|
64
|
+
return cancel != null;
|
|
69
65
|
}
|
|
70
66
|
};
|
|
71
67
|
}
|
|
@@ -85,7 +81,52 @@ export namespace ExtendUtils {
|
|
|
85
81
|
*/
|
|
86
82
|
export function sleep(delay = 0) {
|
|
87
83
|
return new Promise((resolve) => {
|
|
88
|
-
|
|
84
|
+
waitFor(() => resolve, delay);
|
|
89
85
|
});
|
|
90
86
|
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Wait for condition meets and execute callback
|
|
90
|
+
* requestAnimationFrame to replace setTimeout
|
|
91
|
+
* @param callback Callback
|
|
92
|
+
* @param checkReady Check ready, when it's a number, similar to setTimeout
|
|
93
|
+
* @returns cancel callback
|
|
94
|
+
*/
|
|
95
|
+
export function waitFor(
|
|
96
|
+
callback: () => void,
|
|
97
|
+
checkReady: ((spanTime: number) => boolean) | number
|
|
98
|
+
) {
|
|
99
|
+
let startTime: number | undefined;
|
|
100
|
+
let requestID: number | undefined;
|
|
101
|
+
function doWait(time?: number) {
|
|
102
|
+
// Reset request id
|
|
103
|
+
requestID = undefined;
|
|
104
|
+
|
|
105
|
+
// First time
|
|
106
|
+
if (startTime == null) startTime = time;
|
|
107
|
+
|
|
108
|
+
// Ignore
|
|
109
|
+
if (startTime === 0) return;
|
|
110
|
+
|
|
111
|
+
const spanTime =
|
|
112
|
+
startTime == null || time == null ? 0 : time - startTime;
|
|
113
|
+
if (
|
|
114
|
+
time != null &&
|
|
115
|
+
(typeof checkReady === 'number'
|
|
116
|
+
? spanTime >= checkReady
|
|
117
|
+
: checkReady(spanTime))
|
|
118
|
+
) {
|
|
119
|
+
callback();
|
|
120
|
+
} else {
|
|
121
|
+
requestID = requestAnimationFrame(doWait);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
doWait();
|
|
126
|
+
|
|
127
|
+
return () => {
|
|
128
|
+
if (requestID) cancelAnimationFrame(requestID);
|
|
129
|
+
startTime = undefined;
|
|
130
|
+
};
|
|
131
|
+
}
|
|
91
132
|
}
|