@hkdigital/lib-sveltekit 0.0.46 → 0.0.48
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/README.md +17 -1
- package/dist/classes/data/Selector.d.ts +8 -6
- package/dist/classes/data/Selector.js +6 -4
- package/dist/classes/promise/HkPromise.d.ts +116 -0
- package/dist/classes/promise/HkPromise.js +387 -0
- package/dist/classes/promise/index.d.ts +1 -0
- package/dist/classes/promise/index.js +1 -0
- package/dist/util/array/index.d.ts +8 -6
- package/dist/util/array/index.js +6 -4
- package/dist/util/function/index.d.ts +20 -0
- package/dist/util/{function.js__ → function/index.js} +35 -37
- package/package.json +9 -1
package/README.md
CHANGED
@@ -29,9 +29,25 @@ pnpm add @hkdigital/lib-sveltekit
|
|
29
29
|
|
30
30
|
### Update
|
31
31
|
|
32
|
+
We use a global installion of the `ncu` package to upgrade our `package.json`. Install `ncu` first if you don't have it yet
|
33
|
+
|
34
|
+
```bash
|
35
|
+
npm install -g npm-check-updates
|
36
|
+
```
|
37
|
+
|
38
|
+
Upgrading works as follows:
|
39
|
+
|
32
40
|
```bash
|
33
41
|
ncu "@hkdigital/*" -u && pnpm install
|
34
42
|
```
|
43
|
+
We use a wildcard to upgrade all installed `node_modules` in the scope `@hkdigital`.
|
44
|
+
|
45
|
+
You can also add this command to your project. To do so, add the lines to the bottom of the `scripts` section of your `package.json`.
|
46
|
+
|
47
|
+
```bash
|
48
|
+
"upgrade:hklib": "ncu '@hkdigital/*' -u && pnpm install",
|
49
|
+
"upgrade:all": "ncu -u && pnpm install"
|
50
|
+
```
|
35
51
|
|
36
52
|
### Import JS & Svelte
|
37
53
|
|
@@ -107,5 +123,5 @@ npm publish --access public
|
|
107
123
|
pnpm run publish:npm
|
108
124
|
```
|
109
125
|
|
110
|
-
|
126
|
+
## Contribute
|
111
127
|
If your wish to contribute to this library, please contact us [HKdigital](https://hkdigital.nl/contact). Alternatively, the license permits you to fork the library and publish under an alternative name. Change the package name in [package.json](./package.json) to do so.
|
@@ -11,18 +11,20 @@ export default class Selector {
|
|
11
11
|
/**
|
12
12
|
* Returns the first item from the list of items that matches the selector
|
13
13
|
*
|
14
|
-
* @
|
14
|
+
* @template {object} T
|
15
|
+
* @param {T[]|null} items
|
15
16
|
*
|
16
|
-
* @returns {
|
17
|
+
* @returns {T|null} item or null if not found
|
17
18
|
*/
|
18
|
-
findFirst(items:
|
19
|
+
findFirst<T extends unknown>(items: T[] | null): T | null;
|
19
20
|
/**
|
20
21
|
* Returns all items from the list of items that match the selector
|
21
22
|
*
|
22
|
-
* @
|
23
|
+
* @template {object} T
|
24
|
+
* @param {T[]|null} items
|
23
25
|
*
|
24
|
-
* @returns {
|
26
|
+
* @returns {T|null} item or null if not found
|
25
27
|
*/
|
26
|
-
findAll(items:
|
28
|
+
findAll<T extends unknown>(items: T[] | null): T | null;
|
27
29
|
#private;
|
28
30
|
}
|
@@ -53,9 +53,10 @@ export default class Selector {
|
|
53
53
|
/**
|
54
54
|
* Returns the first item from the list of items that matches the selector
|
55
55
|
*
|
56
|
-
* @
|
56
|
+
* @template {object} T
|
57
|
+
* @param {T[]|null} items
|
57
58
|
*
|
58
|
-
* @returns {
|
59
|
+
* @returns {T|null} item or null if not found
|
59
60
|
*/
|
60
61
|
findFirst(items) {
|
61
62
|
if (!items) {
|
@@ -76,9 +77,10 @@ export default class Selector {
|
|
76
77
|
/**
|
77
78
|
* Returns all items from the list of items that match the selector
|
78
79
|
*
|
79
|
-
* @
|
80
|
+
* @template {object} T
|
81
|
+
* @param {T[]|null} items
|
80
82
|
*
|
81
|
-
* @returns {
|
83
|
+
* @returns {T|null} item or null if not found
|
82
84
|
*/
|
83
85
|
findAll(items) {
|
84
86
|
const result = [];
|
@@ -0,0 +1,116 @@
|
|
1
|
+
/**
|
2
|
+
* HkPromise extends the default javascript Promise class
|
3
|
+
*/
|
4
|
+
export default class HkPromise extends Promise<any> {
|
5
|
+
constructor(initFn: any);
|
6
|
+
/**
|
7
|
+
* Get value of property [resolved]
|
8
|
+
*
|
9
|
+
* @returns {boolean} true if the promise has been resolved
|
10
|
+
*/
|
11
|
+
get resolved(): boolean;
|
12
|
+
/**
|
13
|
+
* Get value of property [rejected]
|
14
|
+
*
|
15
|
+
* @returns {boolean} true if the promise was rejected
|
16
|
+
*/
|
17
|
+
get rejected(): boolean;
|
18
|
+
/**
|
19
|
+
* Get value of property [pending]
|
20
|
+
*
|
21
|
+
* @returns {boolean} true if the promise is still pending
|
22
|
+
*/
|
23
|
+
get pending(): boolean;
|
24
|
+
/**
|
25
|
+
* Get value of property [cancelled]
|
26
|
+
*
|
27
|
+
* @returns {boolean} true if the promise was cancelled
|
28
|
+
*/
|
29
|
+
get cancelled(): boolean;
|
30
|
+
/**
|
31
|
+
* Get value of property [timeout]
|
32
|
+
*
|
33
|
+
* @returns {boolean} true if the promise was cancelled due to a timeout
|
34
|
+
*/
|
35
|
+
get timeout(): boolean;
|
36
|
+
/**
|
37
|
+
* Resolve the promise
|
38
|
+
*
|
39
|
+
* @param {mixed} [value] - Value to pass to the "then" callbacks
|
40
|
+
*
|
41
|
+
* @returns {object} this
|
42
|
+
*/
|
43
|
+
resolve(...args: any[]): object;
|
44
|
+
/**
|
45
|
+
* Resolve the promise if the promise is still pending
|
46
|
+
*
|
47
|
+
* @param {mixed} [value] - Value to pass to the "catch" callbacks
|
48
|
+
*
|
49
|
+
* @returns {object} this
|
50
|
+
*/
|
51
|
+
tryResolve(...args: any[]): object;
|
52
|
+
/**
|
53
|
+
* Reject the promise
|
54
|
+
*
|
55
|
+
* @param {Object} [errorOrInfo]
|
56
|
+
* Object to pass to the "catch" callbacks, usually an Error object
|
57
|
+
*
|
58
|
+
* @returns {object} this
|
59
|
+
*/
|
60
|
+
reject(...args: any[]): object;
|
61
|
+
/**
|
62
|
+
* Reject the promise if the promise is still pending
|
63
|
+
*
|
64
|
+
* @param {Object} [errorOrInfo]
|
65
|
+
* Object to pass to the "catch" callbacks, usually an Error object
|
66
|
+
*
|
67
|
+
* @returns {object} this
|
68
|
+
*/
|
69
|
+
tryReject(...args: any[]): object;
|
70
|
+
/**
|
71
|
+
* Reject the promise and set this.cancelled=true
|
72
|
+
*
|
73
|
+
* @param {Object} [errorOrInfo]
|
74
|
+
* Object to pass to the "catch" callbacks, usually an Error object
|
75
|
+
*
|
76
|
+
* @returns {object} this
|
77
|
+
*/
|
78
|
+
cancel(errorOrInfo?: any, ...args: any[]): object;
|
79
|
+
/**
|
80
|
+
* Reject the promise and set this.cancelled=true
|
81
|
+
*
|
82
|
+
* @param {Object} [errorOrInfo]
|
83
|
+
* Object to pass to the "catch" callbacks, usually an Error object
|
84
|
+
*
|
85
|
+
* @returns {object} this
|
86
|
+
*/
|
87
|
+
tryCancel(...args: any[]): object;
|
88
|
+
/**
|
89
|
+
* Specify the number of milliseconds until the promise should time out.
|
90
|
+
* - When a timeout occurs: the promise is cancelled and the following
|
91
|
+
* properties are both set
|
92
|
+
*
|
93
|
+
* this.timeout=true
|
94
|
+
* this.cancelled=true
|
95
|
+
*
|
96
|
+
* @param {number} ms
|
97
|
+
* Number of milliseconds after which the promise should time out
|
98
|
+
*
|
99
|
+
* @param {string} [message="Timeout"]
|
100
|
+
* Message of the error that will be thrown when the timeout occurs
|
101
|
+
*/
|
102
|
+
setTimeout(ms: number, message?: string): this;
|
103
|
+
/**
|
104
|
+
* Register a callback that is called when the promise resolves
|
105
|
+
*
|
106
|
+
* @param {function} callback
|
107
|
+
*/
|
108
|
+
then(...args: any[]): Promise<any>;
|
109
|
+
/**
|
110
|
+
* Register a callback that is called when the promise rejects, is
|
111
|
+
* cancelled or times out
|
112
|
+
*
|
113
|
+
* @param {function} callback
|
114
|
+
*/
|
115
|
+
catch(...args: any[]): Promise<any>;
|
116
|
+
}
|
@@ -0,0 +1,387 @@
|
|
1
|
+
/**
|
2
|
+
* Hkpromise.js
|
3
|
+
*
|
4
|
+
* @description
|
5
|
+
* HkPromise extends the default Promise class. A HkPromise offers some
|
6
|
+
* additional methods, e.g. resolve, reject and setTimeout, which makes it
|
7
|
+
* easier to use than the build in Promise class in some code constructions.
|
8
|
+
*
|
9
|
+
* @example
|
10
|
+
*
|
11
|
+
* import HkPromise from "./HkPromise.js";
|
12
|
+
*
|
13
|
+
* function() {
|
14
|
+
* const promise = new HkPromise();
|
15
|
+
*
|
16
|
+
* setTimeout( promise.resolve, 1000 );
|
17
|
+
*
|
18
|
+
* return promise;
|
19
|
+
* }
|
20
|
+
*/
|
21
|
+
|
22
|
+
/* ------------------------------------------------------------------ Imports */
|
23
|
+
|
24
|
+
import * as expect from '../../util/expect/index.js';
|
25
|
+
|
26
|
+
import { noop } from '../../util/function/index.js';
|
27
|
+
|
28
|
+
/* ---------------------------------------------------------------- Internals */
|
29
|
+
|
30
|
+
const resolved$ = Symbol('resolved');
|
31
|
+
const rejected$ = Symbol('rejected');
|
32
|
+
const pending$ = Symbol('pending');
|
33
|
+
|
34
|
+
const timeout$ = Symbol('timeout');
|
35
|
+
const cancelled$ = Symbol('cancelled');
|
36
|
+
|
37
|
+
const resolveFn$ = Symbol('resolveFn');
|
38
|
+
const rejectFn$ = Symbol('rejectFn');
|
39
|
+
|
40
|
+
const timeoutTimer$ = Symbol('timeoutTimer');
|
41
|
+
|
42
|
+
const hasThen$ = Symbol('hasThen');
|
43
|
+
|
44
|
+
/* ------------------------------------------------------------------- Export */
|
45
|
+
|
46
|
+
/**
|
47
|
+
* HkPromise extends the default javascript Promise class
|
48
|
+
*/
|
49
|
+
export default class HkPromise extends Promise {
|
50
|
+
constructor(initFn) {
|
51
|
+
let _resolveFn;
|
52
|
+
let _rejectFn;
|
53
|
+
|
54
|
+
super((resolveFn, rejectFn) => {
|
55
|
+
//
|
56
|
+
// @note if initFn cannot be called an exception will be thrown:
|
57
|
+
// TypeError: Promise resolve or reject function is not callable
|
58
|
+
//
|
59
|
+
if (initFn) {
|
60
|
+
initFn(resolveFn, rejectFn);
|
61
|
+
}
|
62
|
+
|
63
|
+
_resolveFn = resolveFn;
|
64
|
+
_rejectFn = rejectFn;
|
65
|
+
});
|
66
|
+
|
67
|
+
// @note some values are not initialized on purpose,
|
68
|
+
// to save time during promise creation
|
69
|
+
|
70
|
+
this[resolveFn$] = _resolveFn;
|
71
|
+
this[rejectFn$] = _rejectFn;
|
72
|
+
|
73
|
+
// this[ resolved$ ] = false;
|
74
|
+
// this[ rejected$ ] = false;
|
75
|
+
|
76
|
+
this[pending$] = true;
|
77
|
+
|
78
|
+
// this[ cancelled$ ] = false;
|
79
|
+
// this[ timeout$ ] = false;
|
80
|
+
|
81
|
+
// this[ timeoutTimer$ ] = undefined;
|
82
|
+
}
|
83
|
+
|
84
|
+
// -------------------------------------------------------------------- Method
|
85
|
+
|
86
|
+
/**
|
87
|
+
* Get value of property [resolved]
|
88
|
+
*
|
89
|
+
* @returns {boolean} true if the promise has been resolved
|
90
|
+
*/
|
91
|
+
get resolved() {
|
92
|
+
return this[resolved$] ? true : false;
|
93
|
+
}
|
94
|
+
|
95
|
+
// -------------------------------------------------------------------- Method
|
96
|
+
|
97
|
+
/**
|
98
|
+
* Get value of property [rejected]
|
99
|
+
*
|
100
|
+
* @returns {boolean} true if the promise was rejected
|
101
|
+
*/
|
102
|
+
get rejected() {
|
103
|
+
return this[rejected$] ? true : false;
|
104
|
+
}
|
105
|
+
|
106
|
+
// -------------------------------------------------------------------- Method
|
107
|
+
|
108
|
+
/**
|
109
|
+
* Get value of property [pending]
|
110
|
+
*
|
111
|
+
* @returns {boolean} true if the promise is still pending
|
112
|
+
*/
|
113
|
+
get pending() {
|
114
|
+
return this[pending$];
|
115
|
+
}
|
116
|
+
|
117
|
+
// -------------------------------------------------------------------- Method
|
118
|
+
|
119
|
+
/**
|
120
|
+
* Get value of property [cancelled]
|
121
|
+
*
|
122
|
+
* @returns {boolean} true if the promise was cancelled
|
123
|
+
*/
|
124
|
+
get cancelled() {
|
125
|
+
return this[cancelled$] ? true : false;
|
126
|
+
}
|
127
|
+
|
128
|
+
// -------------------------------------------------------------------- Method
|
129
|
+
|
130
|
+
/**
|
131
|
+
* Get value of property [timeout]
|
132
|
+
*
|
133
|
+
* @returns {boolean} true if the promise was cancelled due to a timeout
|
134
|
+
*/
|
135
|
+
get timeout() {
|
136
|
+
return this[timeout$] ? true : false;
|
137
|
+
}
|
138
|
+
|
139
|
+
// -------------------------------------------------------------------- Method
|
140
|
+
|
141
|
+
/**
|
142
|
+
* Resolve the promise
|
143
|
+
*
|
144
|
+
* @param {mixed} [value] - Value to pass to the "then" callbacks
|
145
|
+
*
|
146
|
+
* @returns {object} this
|
147
|
+
*/
|
148
|
+
resolve(/* value */) {
|
149
|
+
// -- Check current Promise state
|
150
|
+
|
151
|
+
if (!this[pending$]) {
|
152
|
+
if (this[resolved$]) {
|
153
|
+
throw new Error('Cannot resolve Promise. Promise has already resolved');
|
154
|
+
} else {
|
155
|
+
throw new Error('Cannot resolve Promise. Promise has already been rejected');
|
156
|
+
}
|
157
|
+
}
|
158
|
+
|
159
|
+
// -- Clear timeout timer (if any)
|
160
|
+
|
161
|
+
if (undefined !== this[timeoutTimer$]) {
|
162
|
+
clearTimeout(this[timeoutTimer$]);
|
163
|
+
|
164
|
+
this[timeoutTimer$] = undefined;
|
165
|
+
}
|
166
|
+
|
167
|
+
// -- Set flags and call resolve function
|
168
|
+
|
169
|
+
this[resolved$] = true;
|
170
|
+
this[pending$] = false;
|
171
|
+
|
172
|
+
this[resolveFn$](...arguments);
|
173
|
+
|
174
|
+
return this;
|
175
|
+
}
|
176
|
+
|
177
|
+
// -------------------------------------------------------------------- Method
|
178
|
+
|
179
|
+
/**
|
180
|
+
* Resolve the promise if the promise is still pending
|
181
|
+
*
|
182
|
+
* @param {mixed} [value] - Value to pass to the "catch" callbacks
|
183
|
+
*
|
184
|
+
* @returns {object} this
|
185
|
+
*/
|
186
|
+
tryResolve(/* value */) {
|
187
|
+
if (this[pending$]) {
|
188
|
+
this.resolve(...arguments);
|
189
|
+
}
|
190
|
+
|
191
|
+
return this;
|
192
|
+
}
|
193
|
+
|
194
|
+
// -------------------------------------------------------------------- Method
|
195
|
+
|
196
|
+
/**
|
197
|
+
* Reject the promise
|
198
|
+
*
|
199
|
+
* @param {Object} [errorOrInfo]
|
200
|
+
* Object to pass to the "catch" callbacks, usually an Error object
|
201
|
+
*
|
202
|
+
* @returns {object} this
|
203
|
+
*/
|
204
|
+
reject(/* errorOrInfo */) {
|
205
|
+
if (!this[hasThen$]) {
|
206
|
+
//
|
207
|
+
// No then (or await) has been used
|
208
|
+
// add catch to prevent useless unhandled promise rejection
|
209
|
+
//
|
210
|
+
this.catch(noop);
|
211
|
+
}
|
212
|
+
|
213
|
+
// -- Check current Promise state
|
214
|
+
|
215
|
+
if (!this[pending$]) {
|
216
|
+
if (this[resolved$]) {
|
217
|
+
throw new Error('Cannot reject Promise. Promise has already resolved');
|
218
|
+
} else {
|
219
|
+
throw new Error('Cannot reject Promise. Promise has already been rejected');
|
220
|
+
}
|
221
|
+
}
|
222
|
+
|
223
|
+
// -- Clear timeout timer (if any)
|
224
|
+
|
225
|
+
if (undefined !== this[timeoutTimer$]) {
|
226
|
+
clearTimeout(this[timeoutTimer$]);
|
227
|
+
|
228
|
+
this[timeoutTimer$] = undefined;
|
229
|
+
}
|
230
|
+
|
231
|
+
// -- Set flags and call reject function
|
232
|
+
|
233
|
+
this[rejected$] = true;
|
234
|
+
this[pending$] = false;
|
235
|
+
|
236
|
+
this[rejectFn$](...arguments);
|
237
|
+
|
238
|
+
return this;
|
239
|
+
}
|
240
|
+
|
241
|
+
// -------------------------------------------------------------------- Method
|
242
|
+
|
243
|
+
/**
|
244
|
+
* Reject the promise if the promise is still pending
|
245
|
+
*
|
246
|
+
* @param {Object} [errorOrInfo]
|
247
|
+
* Object to pass to the "catch" callbacks, usually an Error object
|
248
|
+
*
|
249
|
+
* @returns {object} this
|
250
|
+
*/
|
251
|
+
tryReject(/* errorOrInfo */) {
|
252
|
+
if (this[pending$]) {
|
253
|
+
this.reject(...arguments);
|
254
|
+
}
|
255
|
+
|
256
|
+
return this;
|
257
|
+
}
|
258
|
+
|
259
|
+
// -------------------------------------------------------------------- Method
|
260
|
+
|
261
|
+
/**
|
262
|
+
* Reject the promise and set this.cancelled=true
|
263
|
+
*
|
264
|
+
* @param {Object} [errorOrInfo]
|
265
|
+
* Object to pass to the "catch" callbacks, usually an Error object
|
266
|
+
*
|
267
|
+
* @returns {object} this
|
268
|
+
*/
|
269
|
+
cancel(errorOrInfo) {
|
270
|
+
if (errorOrInfo) {
|
271
|
+
if (!(errorOrInfo instanceof Object)) {
|
272
|
+
throw new Error('Invalid parameter [errorOrInfo] (expected (error) object');
|
273
|
+
}
|
274
|
+
} else {
|
275
|
+
errorOrInfo = new Error('Cancelled');
|
276
|
+
}
|
277
|
+
|
278
|
+
errorOrInfo.cancelled = true;
|
279
|
+
|
280
|
+
this[cancelled$] = true;
|
281
|
+
this.reject(...arguments);
|
282
|
+
|
283
|
+
return this;
|
284
|
+
}
|
285
|
+
|
286
|
+
// -------------------------------------------------------------------- Method
|
287
|
+
|
288
|
+
/**
|
289
|
+
* Reject the promise and set this.cancelled=true
|
290
|
+
*
|
291
|
+
* @param {Object} [errorOrInfo]
|
292
|
+
* Object to pass to the "catch" callbacks, usually an Error object
|
293
|
+
*
|
294
|
+
* @returns {object} this
|
295
|
+
*/
|
296
|
+
tryCancel(/*errorOrInfo*/) {
|
297
|
+
if (this[pending$]) {
|
298
|
+
this.cancel(...arguments);
|
299
|
+
}
|
300
|
+
|
301
|
+
return this;
|
302
|
+
}
|
303
|
+
|
304
|
+
// -------------------------------------------------------------------- Method
|
305
|
+
|
306
|
+
/**
|
307
|
+
* Specify the number of milliseconds until the promise should time out.
|
308
|
+
* - When a timeout occurs: the promise is cancelled and the following
|
309
|
+
* properties are both set
|
310
|
+
*
|
311
|
+
* this.timeout=true
|
312
|
+
* this.cancelled=true
|
313
|
+
*
|
314
|
+
* @param {number} ms
|
315
|
+
* Number of milliseconds after which the promise should time out
|
316
|
+
*
|
317
|
+
* @param {string} [message="Timeout"]
|
318
|
+
* Message of the error that will be thrown when the timeout occurs
|
319
|
+
*/
|
320
|
+
setTimeout(ms, message = 'Timeout') {
|
321
|
+
expect.number(ms);
|
322
|
+
expect.string(message);
|
323
|
+
|
324
|
+
// -- Check current Promise state
|
325
|
+
|
326
|
+
if (!this[pending$]) {
|
327
|
+
if (this[resolved$]) {
|
328
|
+
throw new Error('Cannot set timeout. Promise has already resolved');
|
329
|
+
} else {
|
330
|
+
throw new Error('Cannot set timeout. Promise has already been rejected');
|
331
|
+
}
|
332
|
+
}
|
333
|
+
|
334
|
+
// -- Clear existing timeout (if any)
|
335
|
+
|
336
|
+
if (undefined !== this[timeoutTimer$]) {
|
337
|
+
clearTimeout(this[timeoutTimer$]);
|
338
|
+
}
|
339
|
+
|
340
|
+
// -- Set timeout
|
341
|
+
|
342
|
+
const err = new Error(message);
|
343
|
+
|
344
|
+
this[timeoutTimer$] = setTimeout(() => {
|
345
|
+
if (!this[pending$]) {
|
346
|
+
// Promise has already been resolved (should not happen)
|
347
|
+
return;
|
348
|
+
}
|
349
|
+
|
350
|
+
this[timeout$] = true;
|
351
|
+
this[cancelled$] = true;
|
352
|
+
|
353
|
+
err.timeout = true;
|
354
|
+
err.cancelled = true;
|
355
|
+
|
356
|
+
this.reject(err);
|
357
|
+
}, ms);
|
358
|
+
|
359
|
+
// return this -> chainable method
|
360
|
+
return this;
|
361
|
+
}
|
362
|
+
|
363
|
+
// -------------------------------------------------------------------- Method
|
364
|
+
|
365
|
+
/**
|
366
|
+
* Register a callback that is called when the promise resolves
|
367
|
+
*
|
368
|
+
* @param {function} callback
|
369
|
+
*/
|
370
|
+
then(/* callback */) {
|
371
|
+
this[hasThen$] = true;
|
372
|
+
|
373
|
+
return super.then(...arguments);
|
374
|
+
}
|
375
|
+
|
376
|
+
// -------------------------------------------------------------------- Method
|
377
|
+
|
378
|
+
/**
|
379
|
+
* Register a callback that is called when the promise rejects, is
|
380
|
+
* cancelled or times out
|
381
|
+
*
|
382
|
+
* @param {function} callback
|
383
|
+
*/
|
384
|
+
catch(/* callback */) {
|
385
|
+
return super.catch(...arguments);
|
386
|
+
}
|
387
|
+
} // end class
|
@@ -0,0 +1 @@
|
|
1
|
+
export { default as HkPromise } from "./HkPromise.js";
|
@@ -0,0 +1 @@
|
|
1
|
+
export { default as HkPromise } from './HkPromise.js';
|
@@ -135,22 +135,24 @@ export function sortByPathValue(items: any[], path: string[] | string, compareFn
|
|
135
135
|
* Find the first item in the list of objects that matches the selector
|
136
136
|
* - All items in the supplied array must be objects
|
137
137
|
*
|
138
|
-
* @
|
138
|
+
* @template {object} T
|
139
|
+
* @param {T[]} arr
|
139
140
|
* @param {object|null} selector
|
140
141
|
*
|
141
|
-
* @returns {
|
142
|
+
* @returns {T|null} first matched item
|
142
143
|
*/
|
143
|
-
export function findFirst(arr:
|
144
|
+
export function findFirst<T extends unknown>(arr: T[], selector: object | null): T | null;
|
144
145
|
/**
|
145
146
|
* Returns all items from the list of items that match the selector
|
146
147
|
* - All items in the supplied array must be objects
|
147
148
|
*
|
148
|
-
* @
|
149
|
+
* @template {object} T
|
150
|
+
* @param {T[]} arr
|
149
151
|
* @param {object|null} selector
|
150
152
|
*
|
151
|
-
* @returns {
|
153
|
+
* @returns {T[]} matching items
|
152
154
|
*/
|
153
|
-
export function findAll(arr:
|
155
|
+
export function findAll<T extends unknown>(arr: T[], selector: object | null): T[];
|
154
156
|
/**
|
155
157
|
* Convert array to an object using a list of keys for each index
|
156
158
|
*
|
package/dist/util/array/index.js
CHANGED
@@ -378,10 +378,11 @@ export function sortByPathValue(items, path, compareFn = smallestFirst) {
|
|
378
378
|
* Find the first item in the list of objects that matches the selector
|
379
379
|
* - All items in the supplied array must be objects
|
380
380
|
*
|
381
|
-
* @
|
381
|
+
* @template {object} T
|
382
|
+
* @param {T[]} arr
|
382
383
|
* @param {object|null} selector
|
383
384
|
*
|
384
|
-
* @returns {
|
385
|
+
* @returns {T|null} first matched item
|
385
386
|
*/
|
386
387
|
export function findFirst(arr, selector) {
|
387
388
|
const selectorObj = new Selector(selector);
|
@@ -395,10 +396,11 @@ export function findFirst(arr, selector) {
|
|
395
396
|
* Returns all items from the list of items that match the selector
|
396
397
|
* - All items in the supplied array must be objects
|
397
398
|
*
|
398
|
-
* @
|
399
|
+
* @template {object} T
|
400
|
+
* @param {T[]} arr
|
399
401
|
* @param {object|null} selector
|
400
402
|
*
|
401
|
-
* @returns {
|
403
|
+
* @returns {T[]} matching items
|
402
404
|
*/
|
403
405
|
export function findAll(arr, selector) {
|
404
406
|
const selectorObj = new Selector(selector);
|
@@ -0,0 +1,20 @@
|
|
1
|
+
/**
|
2
|
+
* Wraps a function so that the callback function will be called only once
|
3
|
+
*
|
4
|
+
* @param {function} callback
|
5
|
+
*
|
6
|
+
* @returns {function} callback wrapped in `once` function
|
7
|
+
*/
|
8
|
+
export function once(callback: Function): Function;
|
9
|
+
/**
|
10
|
+
* Returns a debounced function
|
11
|
+
* - The original function is not called more than once during the
|
12
|
+
* specified interval
|
13
|
+
*
|
14
|
+
* @param {function} fn
|
15
|
+
* @param {number} [intervalMs=200]
|
16
|
+
*
|
17
|
+
* @returns {function} debounced function
|
18
|
+
*/
|
19
|
+
export function debounce(fn: Function, intervalMs?: number): Function;
|
20
|
+
export function noop(): void;
|
@@ -16,7 +16,7 @@
|
|
16
16
|
|
17
17
|
/* ------------------------------------------------------------------ Imports */
|
18
18
|
|
19
|
-
import * as
|
19
|
+
import * as expect from '../expect/index.js';
|
20
20
|
|
21
21
|
/* ------------------------------------------------------------------ Exports */
|
22
22
|
|
@@ -36,18 +36,16 @@ export const noop = () => {};
|
|
36
36
|
* @returns {function} callback wrapped in `once` function
|
37
37
|
*/
|
38
38
|
export function once(callback) {
|
39
|
-
|
39
|
+
expect.function(callback);
|
40
40
|
|
41
|
-
|
41
|
+
let ignore = false;
|
42
42
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
}
|
50
|
-
};
|
43
|
+
return function () {
|
44
|
+
if (!ignore) {
|
45
|
+
ignore = true;
|
46
|
+
callback(...arguments);
|
47
|
+
}
|
48
|
+
};
|
51
49
|
}
|
52
50
|
|
53
51
|
// -----------------------------------------------------------------------------
|
@@ -63,40 +61,40 @@ export function once(callback) {
|
|
63
61
|
* @returns {function} debounced function
|
64
62
|
*/
|
65
63
|
export function debounce(fn, intervalMs = 200) {
|
66
|
-
|
67
|
-
|
64
|
+
let idleTimer;
|
65
|
+
let lastArguments;
|
68
66
|
|
69
|
-
|
67
|
+
// console.log("debounce");
|
70
68
|
|
71
|
-
|
72
|
-
|
69
|
+
return function debounced() {
|
70
|
+
// console.log("debounced");
|
73
71
|
|
74
|
-
|
75
|
-
|
72
|
+
if (idleTimer) {
|
73
|
+
// console.log("idleTimer running");
|
76
74
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
75
|
+
// The function has been called recently
|
76
|
+
lastArguments = arguments;
|
77
|
+
return;
|
78
|
+
}
|
81
79
|
|
82
|
-
|
83
|
-
|
80
|
+
idleTimer = setTimeout(() => {
|
81
|
+
// console.log("idleTimer finished", lastArguments);
|
84
82
|
|
85
|
-
|
83
|
+
idleTimer = null;
|
86
84
|
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
85
|
+
if (lastArguments) {
|
86
|
+
//
|
87
|
+
// At least one call has been "debounced"
|
88
|
+
// -> make call with last arguments, so function always receives
|
89
|
+
// the arguments of the last call to the function
|
90
|
+
//
|
91
|
+
fn(...lastArguments);
|
92
|
+
lastArguments = undefined;
|
93
|
+
}
|
94
|
+
}, intervalMs);
|
97
95
|
|
98
|
-
|
99
|
-
|
96
|
+
fn(...arguments);
|
97
|
+
};
|
100
98
|
}
|
101
99
|
|
102
100
|
// -----------------------------------------------------------------------------
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@hkdigital/lib-sveltekit",
|
3
|
-
"version": "0.0.
|
3
|
+
"version": "0.0.48",
|
4
4
|
"author": "Jens Kleinhout, HKdigital (https://hkdigital.nl)",
|
5
5
|
"license": "ISC",
|
6
6
|
"repository": {
|
@@ -54,6 +54,10 @@
|
|
54
54
|
"types": "./dist/classes/data/index.d.ts",
|
55
55
|
"svelte": "./dist/classes/data/index.js"
|
56
56
|
},
|
57
|
+
"./classes/promise": {
|
58
|
+
"types": "./dist/classes/promise/index.d.ts",
|
59
|
+
"svelte": "./dist/classes/promise/index.js"
|
60
|
+
},
|
57
61
|
"./classes/streams": {
|
58
62
|
"types": "./dist/classes/streams/index.d.ts",
|
59
63
|
"svelte": "./dist/classes/streams/index.js"
|
@@ -122,6 +126,10 @@
|
|
122
126
|
"types": "./dist/util/expect/index.d.ts",
|
123
127
|
"svelte": "./dist/util/expect/index.js"
|
124
128
|
},
|
129
|
+
"./util/function": {
|
130
|
+
"types": "./dist/util/function/index.d.ts",
|
131
|
+
"svelte": "./dist/util/function/index.js"
|
132
|
+
},
|
125
133
|
"./util/is": {
|
126
134
|
"types": "./dist/util/is/index.d.ts",
|
127
135
|
"svelte": "./dist/util/is/index.js"
|