@hkdigital/lib-sveltekit 0.0.50 → 0.0.52

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.
@@ -48,6 +48,16 @@ export function toArrayAsync(value: AsyncIterator<any, any, any> | mixed): any[]
48
48
  * @returns {string[]} array path (e.g. ["some", "path", "to"])
49
49
  */
50
50
  export function toArrayPath(path: string | string[], pathSeparator?: string): string[];
51
+ /**
52
+ * Push a value to an array if it is not null, undefined or an empty string
53
+ *
54
+ * @template {arrray} T
55
+ * @param {T} arr
56
+ * @param {*} value
57
+ *
58
+ * @returns {T} arr
59
+ */
60
+ export function pushNotEmpty<T extends arrray>(arr: T, value: any): T;
51
61
  /**
52
62
  * Loop over the supplied array and call the callback for every element
53
63
  * - The callback will receive the current element of the array as
@@ -145,6 +145,27 @@ export function toArrayPath(path, pathSeparator = PATH_SEPARATOR) {
145
145
 
146
146
  // -----------------------------------------------------------------------------
147
147
 
148
+ /**
149
+ * Push a value to an array if it is not null, undefined or an empty string
150
+ *
151
+ * @template {arrray} T
152
+ * @param {T} arr
153
+ * @param {*} value
154
+ *
155
+ * @returns {T} arr
156
+ */
157
+ export function pushNotEmpty(arr, value) {
158
+ expect.array(arr);
159
+
160
+ if (value !== null && value !== undefined && value !== '') {
161
+ arr.push(value);
162
+ }
163
+
164
+ return arr;
165
+ }
166
+
167
+ // -----------------------------------------------------------------------------
168
+
148
169
  /**
149
170
  * Loop over the supplied array and call the callback for every element
150
171
  * - The callback will receive the current element of the array as
@@ -18,3 +18,17 @@ export function once(callback: Function): Function;
18
18
  */
19
19
  export function debounce(fn: Function, intervalMs?: number): Function;
20
20
  export function noop(): void;
21
+ /**
22
+ * Defer the execution of a function
23
+ * - Uses the best 'setImmediate' implementation supported by the current
24
+ * runtime environment
25
+ *
26
+ * @param {function} fn - Function to execute
27
+ *
28
+ * --
29
+ *
30
+ * @note setImmediate is preferred over nextTick
31
+ *
32
+ * @see https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/
33
+ */
34
+ export const defer: any;
@@ -12,12 +12,65 @@
12
12
  *
13
13
  * sayHelloOnce();
14
14
  * sayHelloOnce();
15
+ *
16
+ * @example
17
+ *
18
+ * import { defer } from './process.js';
19
+ *
20
+ * defer( () => {
21
+ * console.log("The execution of the function has been defered");
22
+ * } );
15
23
  */
16
24
 
17
25
  /* ------------------------------------------------------------------ Imports */
18
26
 
19
27
  import * as expect from '../expect/index.js';
20
28
 
29
+ /* ---------------------------------------------------------------- Internals */
30
+
31
+ const NEXT_TICK_MESSAGE = 'hk-next-tick';
32
+
33
+ /**
34
+ * Detect and return the most suitable setImmediate implementation available
35
+ * on the current platform
36
+ */
37
+ function set_immediate_implementation() {
38
+ if (typeof global !== 'undefined') {
39
+ if (undefined !== global.setImmediate) {
40
+ return global.setImmediate;
41
+ }
42
+ } else if (typeof window !== 'undefined') {
43
+ if (window.postMessage && window.addEventListener) {
44
+ const queue = [];
45
+
46
+ window.addEventListener(
47
+ 'message',
48
+ (event) => {
49
+ const source = event.source;
50
+
51
+ if ((source === window || source === null) && event.data === NEXT_TICK_MESSAGE) {
52
+ event.stopPropagation();
53
+ if (queue.length > 0) {
54
+ const fn = queue.shift();
55
+ fn();
56
+ }
57
+ }
58
+ },
59
+ true
60
+ );
61
+
62
+ return function nextTickUsingPostMessage(fn) {
63
+ expect.function(fn);
64
+
65
+ queue.push(fn);
66
+ window.postMessage(NEXT_TICK_MESSAGE, '*');
67
+ };
68
+ }
69
+ }
70
+
71
+ throw new Error('No suitable [setImmediate] implementation available');
72
+ }
73
+
21
74
  /* ------------------------------------------------------------------ Exports */
22
75
 
23
76
  /**
@@ -99,6 +152,23 @@ export function debounce(fn, intervalMs = 200) {
99
152
 
100
153
  // -----------------------------------------------------------------------------
101
154
 
155
+ /**
156
+ * Defer the execution of a function
157
+ * - Uses the best 'setImmediate' implementation supported by the current
158
+ * runtime environment
159
+ *
160
+ * @param {function} fn - Function to execute
161
+ *
162
+ * --
163
+ *
164
+ * @note setImmediate is preferred over nextTick
165
+ *
166
+ * @see https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/
167
+ */
168
+ export const defer = set_immediate_implementation();
169
+
170
+ // -----------------------------------------------------------------------------
171
+
102
172
  /**
103
173
  * Adds a wrapper around a function that only calls the supplied function
104
174
  * if the (first) supplied argument to the returned function is not `null`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hkdigital/lib-sveltekit",
3
- "version": "0.0.50",
3
+ "version": "0.0.52",
4
4
  "author": "Jens Kleinhout, HKdigital (https://hkdigital.nl)",
5
5
  "license": "ISC",
6
6
  "repository": {