@hkdigital/lib-sveltekit 0.0.47 → 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 CHANGED
@@ -42,12 +42,11 @@ ncu "@hkdigital/*" -u && pnpm install
42
42
  ```
43
43
  We use a wildcard to upgrade all installed `node_modules` in the scope `@hkdigital`.
44
44
 
45
- You can also add this command to your project's `package.json`. E.g. add the lines:
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
46
 
47
47
  ```bash
48
- "scripts": {
49
- "upgrade:hklib": "ncu '@hkdigital/*' -u && pnpm install",
50
- "upgrade:all": "ncu -u && pnpm install"
48
+ "upgrade:hklib": "ncu '@hkdigital/*' -u && pnpm install",
49
+ "upgrade:all": "ncu -u && pnpm install"
51
50
  ```
52
51
 
53
52
  ### Import JS & Svelte
@@ -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';
@@ -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 Types from '$lib/typedef/base.js';
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
- Types.Number(callback);
39
+ expect.function(callback);
40
40
 
41
- expectFunction(callback, 'Missing or invalid parameter [callback]');
41
+ let ignore = false;
42
42
 
43
- let ignore = false;
44
-
45
- return function () {
46
- if (!ignore) {
47
- ignore = true;
48
- callback(...arguments);
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
- let idleTimer;
67
- let lastArguments;
64
+ let idleTimer;
65
+ let lastArguments;
68
66
 
69
- // console.log("debounce");
67
+ // console.log("debounce");
70
68
 
71
- return function debounced() {
72
- // console.log("debounced");
69
+ return function debounced() {
70
+ // console.log("debounced");
73
71
 
74
- if (idleTimer) {
75
- // console.log("idleTimer running");
72
+ if (idleTimer) {
73
+ // console.log("idleTimer running");
76
74
 
77
- // The function has been called recently
78
- lastArguments = arguments;
79
- return;
80
- }
75
+ // The function has been called recently
76
+ lastArguments = arguments;
77
+ return;
78
+ }
81
79
 
82
- idleTimer = setTimeout(() => {
83
- // console.log("idleTimer finished", lastArguments);
80
+ idleTimer = setTimeout(() => {
81
+ // console.log("idleTimer finished", lastArguments);
84
82
 
85
- idleTimer = null;
83
+ idleTimer = null;
86
84
 
87
- if (lastArguments) {
88
- //
89
- // At least one call has been "debounced"
90
- // -> make call with last arguments, so function always receives
91
- // the arguments of the last call to the function
92
- //
93
- fn(...lastArguments);
94
- lastArguments = undefined;
95
- }
96
- }, intervalMs);
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
- fn(...arguments);
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.47",
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"