@gkucmierz/utils 2.0.7 → 2.0.8

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
@@ -55,6 +55,7 @@ This library provides a wide range of mathematical functions and data structures
55
55
  - `bijectiveNumeration`: Bijective base-k numeration system.
56
56
 
57
57
  - **Developer Utilities**:
58
+ - `setSafeInterval`: A safe, asynchronous alternative to `setInterval` that prevents overlapping executions and supports immediate yielding.
58
59
  - `consumeIteratorNonBlocking`: Yield consumption macro-tasks queue resolver for heavy computations.
59
60
  - `memoize`: Function memoization based on arguments.
60
61
  - `binarySearch`: Various binary search implementations (exact, lower bound, upper bound).
package/main.mjs CHANGED
@@ -101,6 +101,9 @@ import {
101
101
  import {
102
102
  array2range, range2array
103
103
  } from './src/range-array.mjs'
104
+ import {
105
+ setSafeInterval
106
+ } from './src/set-safe-interval.mjs'
104
107
  import {
105
108
  simulatedAnnealing
106
109
  } from './src/simulated-annealing.mjs'
@@ -145,10 +148,11 @@ export * from './src/phi.mjs';
145
148
  export * from './src/pow-mod.mjs';
146
149
  export * from './src/rand-normal.mjs';
147
150
  export * from './src/range-array.mjs';
151
+ export * from './src/set-safe-interval.mjs';
148
152
  export * from './src/simulated-annealing.mjs';
149
153
  export * from './src/square-root.mjs';
150
154
  export * from './src/tonelli-shanks.mjs';
151
155
 
152
156
  export default [
153
- SetCnt, Trie, arrayHistogram, fromBase64, fromBase64Url, toBase64, toBase64Url, bijective2num, bijective2numBI, num2bijective, num2bijectiveBI, binarySearchArr, binarySearchGE, binarySearchLE, binarySearchRangeIncl, combinations, combinationsIterator, consumeIteratorNonBlocking, copyCase, egcd, factors, factorsBI, formatBigNumber, formatBigNumberBI, wrapFn, gcd, gcdBI, getType, gpn, gpnBI, bin2gray, gray2bin, Heap, heronsFormula, heronsFormulaBI, lcm, lcmBI, ListNode, lucasLehmerBI, axisAngleToMatrix4, crossProduct, dotProduct, getRotationMatrixFromVectors, multiplyMatrix4, normalize, projectToTrackball, matrixAsArray, memoize, mod, modBI, nChooseK, naturalSearch, nelderMead, particleSwarmOptimization, permutations, permutationsIterator, phi, phiBI, powMod, powModBI, randNormal, array2range, range2array, simulatedAnnealing, squareRoot, squareRootBI, tonelliShanksBI
157
+ SetCnt, Trie, arrayHistogram, fromBase64, fromBase64Url, toBase64, toBase64Url, bijective2num, bijective2numBI, num2bijective, num2bijectiveBI, binarySearchArr, binarySearchGE, binarySearchLE, binarySearchRangeIncl, combinations, combinationsIterator, consumeIteratorNonBlocking, copyCase, egcd, factors, factorsBI, formatBigNumber, formatBigNumberBI, wrapFn, gcd, gcdBI, getType, gpn, gpnBI, bin2gray, gray2bin, Heap, heronsFormula, heronsFormulaBI, lcm, lcmBI, ListNode, lucasLehmerBI, axisAngleToMatrix4, crossProduct, dotProduct, getRotationMatrixFromVectors, multiplyMatrix4, normalize, projectToTrackball, matrixAsArray, memoize, mod, modBI, nChooseK, naturalSearch, nelderMead, particleSwarmOptimization, permutations, permutationsIterator, phi, phiBI, powMod, powModBI, randNormal, array2range, range2array, setSafeInterval, simulatedAnnealing, squareRoot, squareRootBI, tonelliShanksBI
154
158
  ];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gkucmierz/utils",
3
- "version": "2.0.7",
3
+ "version": "2.0.8",
4
4
  "type": "module",
5
5
  "description": "Usefull functions for solving programming tasks",
6
6
  "keywords": [
@@ -34,6 +34,9 @@
34
34
  "primality-test",
35
35
  "prime",
36
36
  "range-array",
37
+ "set-safe-interval",
38
+ "setinterval",
39
+ "settimeout",
37
40
  "square-root",
38
41
  "tonelli-shanks",
39
42
  "trie",
package/src/math3d.mjs CHANGED
@@ -20,9 +20,9 @@ export const projectToTrackball = (x, y, radius = 1) => {
20
20
  const d2 = x*x + y*y;
21
21
  const r2 = radius * radius;
22
22
  if (d2 <= r2 * 0.5) {
23
- return normalize([x, y, Math.sqrt(r2 - d2)]);
23
+ return normalize([x, y, Math.sqrt(r2 - d2)]);
24
24
  } else {
25
- return normalize([x, y, (r2 * 0.5) / Math.sqrt(d2)]);
25
+ return normalize([x, y, (r2 * 0.5) / Math.sqrt(d2)]);
26
26
  }
27
27
  };
28
28
 
@@ -47,7 +47,7 @@ export const multiplyMatrix4 = (a, b) => {
47
47
  for (let r = 0; r < 4; r++) {
48
48
  let sum = 0;
49
49
  for (let i = 0; i < 4; i++) {
50
- sum += a[i*4 + r] * b[c*4 + i]; // Note: column-major alignment
50
+ sum += a[i*4 + r] * b[c*4 + i]; // Note: column-major alignment
51
51
  }
52
52
  out[c*4 + r] = sum;
53
53
  }
@@ -58,7 +58,7 @@ export const multiplyMatrix4 = (a, b) => {
58
58
  export const getRotationMatrixFromVectors = (u, v) => {
59
59
  const axis = crossProduct(u, v);
60
60
  if (axis[0] === 0 && axis[1] === 0 && axis[2] === 0) {
61
- return [1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1]; // Identity
61
+ return [1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1]; // Identity
62
62
  }
63
63
  const dot = dotProduct(u, v);
64
64
  const angle = Math.acos(Math.max(-1, Math.min(1, dot)));
@@ -1,3 +1,10 @@
1
+ /**
2
+ * Generates a random number from a normal (Gaussian) distribution.
3
+ * Uses the Box-Muller transform.
4
+ * @param {number} [mean=0] - The mean of the normal distribution.
5
+ * @param {number} [sigma=1] - The standard deviation of the normal distribution.
6
+ * @returns {number} A random number from the specified normal distribution.
7
+ */
1
8
  export const randNormal = (mean = 0, sigma = 1) => {
2
9
  const y1 = Math.random();
3
10
  const y2 = Math.random();
@@ -0,0 +1,44 @@
1
+ /**
2
+ * A safe alternative to setInterval that uses recursive setTimeout.
3
+ * It waits for the callback (even if it's async) to finish before scheduling the next tick,
4
+ * preventing overlapping executions and event loop blocking.
5
+ *
6
+ * @param {Function} cb - Function to execute (can be async)
7
+ * @param {number} delay - Delay in ms between executions
8
+ * @param {boolean} [immediate=true] - Whether to fire the first execution immediately
9
+ * @param {boolean} [nextTick=false] - If immediate is true, whether to yield to the event loop before the first execution
10
+ * @returns {Function} A function to clear the interval
11
+ */
12
+ export const setSafeInterval = (cb, delay, immediate = true, nextTick = false) => {
13
+ let isCleared = false;
14
+ let timerId = null;
15
+
16
+ const loop = async () => {
17
+ if (isCleared) return;
18
+
19
+ try {
20
+ await cb();
21
+ } catch (e) {
22
+ console.error('SafeInterval callback error:', e);
23
+ }
24
+
25
+ if (!isCleared) {
26
+ timerId = setTimeout(loop, delay);
27
+ }
28
+ };
29
+
30
+ if (immediate) {
31
+ if (nextTick) {
32
+ timerId = setTimeout(loop, 0);
33
+ } else {
34
+ loop();
35
+ }
36
+ } else {
37
+ timerId = setTimeout(loop, delay);
38
+ }
39
+
40
+ return () => {
41
+ isCleared = true;
42
+ clearTimeout(timerId);
43
+ };
44
+ };