@gkucmierz/utils 1.28.2 → 1.28.7

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/src/lcm.mjs CHANGED
@@ -10,5 +10,18 @@ const getLcm = gcd => {
10
10
  return (a, b) => a * b / gcd(a, b);
11
11
  }
12
12
 
13
+ /**
14
+ * Calculates the Least Common Multiple (LCM) of two numbers.
15
+ * @param {number} a - The first number.
16
+ * @param {number} b - The second number.
17
+ * @returns {number} The LCM of a and b.
18
+ */
13
19
  export const lcm = getLcm(gcd);
20
+
21
+ /**
22
+ * Calculates the Least Common Multiple (LCM) of two BigInts.
23
+ * @param {bigint} a - The first BigInt.
24
+ * @param {bigint} b - The second BigInt.
25
+ * @returns {bigint} The LCM of a and b.
26
+ */
14
27
  export const lcmBI = getLcm(gcdBI);
package/src/list-node.mjs CHANGED
@@ -1,11 +1,25 @@
1
1
 
2
2
 
3
+ /**
4
+ * Represents a node in a singly linked list.
5
+ */
3
6
  export class ListNode {
7
+ /**
8
+ * Creates a new ListNode.
9
+ * @param {*} [val=0] - The value of the node.
10
+ * @param {ListNode} [next=null] - The next node in the list.
11
+ */
4
12
  constructor(val = 0, next = null) {
5
13
  this.val = val;
6
14
  this.next = next;
7
15
  }
8
16
 
17
+ /**
18
+ * Converts the linked list to an array.
19
+ * Detects cycles and throws an error if one is found.
20
+ * @returns {Array} An array containing the values of the list nodes.
21
+ * @throws {Error} If a cyclic reference is detected.
22
+ */
9
23
  toArr() {
10
24
  const ret = [];
11
25
  let node = this;
@@ -19,6 +33,11 @@ export class ListNode {
19
33
  return ret;
20
34
  }
21
35
 
36
+ /**
37
+ * Creates a linked list from an iterable.
38
+ * @param {Iterable} iter - The iterable to create the list from.
39
+ * @returns {ListNode|null} The head of the created linked list, or null if empty.
40
+ */
22
41
  static fromArr(iter) {
23
42
  const head = new ListNode;
24
43
  let node = head;
package/src/matrix.mjs CHANGED
@@ -1,8 +1,11 @@
1
1
 
2
2
 
3
- // represent matrix as array using Proxy
4
- // todo: iterator can be added
5
-
3
+ /**
4
+ * Represents a 2D matrix as a flat 1D array using a Proxy.
5
+ * Allows accessing elements using a single index (row-major order).
6
+ * @param {Array<Array<*>>} matrix - The 2D matrix to wrap.
7
+ * @returns {Proxy} A proxy that behaves like a flat array.
8
+ */
6
9
  export const matrixAsArray = matrix => {
7
10
  const [h, w] = [matrix.length, matrix[0].length];
8
11
  return new Proxy([], {
package/src/memoize.mjs CHANGED
@@ -1,4 +1,11 @@
1
1
 
2
+ /**
3
+ * Creates a memoized version of a function.
4
+ * Uses a tree structure to store results based on arguments.
5
+ * Compares arguments by reference.
6
+ * @param {Function} fn - The function to memoize.
7
+ * @returns {Function} The memoized function.
8
+ */
2
9
  export const memoize = fn => {
3
10
  const maps = [];
4
11
 
package/src/mod.mjs CHANGED
@@ -1,6 +1,4 @@
1
1
 
2
- // python like mod implementation
3
-
4
2
  const getMod = ZERO => {
5
3
  return (dividend, divisor) => {
6
4
  if ((dividend < ZERO) ^ (divisor < ZERO)) {
@@ -11,5 +9,18 @@ const getMod = ZERO => {
11
9
  };
12
10
  };
13
11
 
12
+ /**
13
+ * Calculates the modulo of two numbers (Python-like behavior for negative numbers).
14
+ * @param {number} dividend - The dividend.
15
+ * @param {number} divisor - The divisor.
16
+ * @returns {number} The result of the modulo operation.
17
+ */
14
18
  export const mod = getMod(0);
19
+
20
+ /**
21
+ * Calculates the modulo of two BigInts (Python-like behavior for negative numbers).
22
+ * @param {bigint} dividend - The dividend.
23
+ * @param {bigint} divisor - The divisor.
24
+ * @returns {bigint} The result of the modulo operation.
25
+ */
15
26
  export const modBI = getMod(0n);
package/src/phi.mjs CHANGED
@@ -31,7 +31,20 @@ const getPhiEuler = factors => {
31
31
  };
32
32
  };
33
33
 
34
+ /**
35
+ * Euler's Totient Function (Phi).
36
+ * Counts the positive integers less than or equal to n that are relatively prime to n.
37
+ * Uses Euler's product formula based on prime factorization.
38
+ * @param {number} n - The number to calculate phi for.
39
+ * @returns {number} The value of phi(n).
40
+ */
34
41
  export const phi = getPhiEuler(factors);
42
+
43
+ /**
44
+ * Euler's Totient Function (Phi) - BigInt version.
45
+ * @param {bigint} n - The BigInt to calculate phi for.
46
+ * @returns {bigint} The value of phi(n).
47
+ */
35
48
  export const phiBI = getPhiEuler(factorsBI);
36
49
 
37
50
  // export const phi = getPhi(1, 2, gcd);
package/src/pow-mod.mjs CHANGED
@@ -18,6 +18,22 @@ const getPowMod = (ZERO, ONE, TWO, floor) => {
18
18
  };
19
19
  };
20
20
 
21
+ /**
22
+ * Calculates the power of a base to an exponent, optionally modulo a number.
23
+ * Similar to Python's pow(base, exponent, modulus).
24
+ * @param {number} base - The base number.
25
+ * @param {number} exponent - The exponent.
26
+ * @param {number} [modulus] - The optional modulus.
27
+ * @returns {number} The result of (base ** exponent) % modulus.
28
+ */
21
29
  export const powMod = getPowMod(0, 1, 2, n => Math.floor(n));
22
30
 
31
+ /**
32
+ * Calculates the power of a base to an exponent, optionally modulo a BigInt.
33
+ * Similar to Python's pow(base, exponent, modulus).
34
+ * @param {bigint} base - The base BigInt.
35
+ * @param {bigint} exponent - The exponent BigInt.
36
+ * @param {bigint} [modulus] - The optional modulus BigInt.
37
+ * @returns {bigint} The result of (base ** exponent) % modulus.
38
+ */
23
39
  export const powModBI = getPowMod(0n, 1n, 2n, n => n);
@@ -1,6 +1,11 @@
1
1
 
2
2
  // https://www.codewars.com/kata/57d83dfc950d842dcb00005b/train/javascript
3
3
 
4
+ /**
5
+ * Converts a list of ranges into a flat array of numbers.
6
+ * @param {Array<Array<number>>} ranges - An array of ranges, where each range is [min, max] or [min].
7
+ * @returns {Array<number>} An array containing all numbers in the specified ranges.
8
+ */
4
9
  export const range2array = ranges => {
5
10
  return ranges.reduce((arr, range) => {
6
11
  const min = range[0];
@@ -11,6 +16,13 @@ export const range2array = ranges => {
11
16
  }, []);
12
17
  };
13
18
 
19
+ /**
20
+ * Converts a sorted array of numbers into a list of ranges.
21
+ * Consecutive numbers are grouped into a single range [min, max].
22
+ * Isolated numbers are represented as [num, num].
23
+ * @param {Array<number>} arr - The sorted array of numbers.
24
+ * @returns {Array<Array<number>>} An array of ranges.
25
+ */
14
26
  export const array2range = arr => {
15
27
  const ranges = [];
16
28
  let last = arr[0];
@@ -1,18 +1,16 @@
1
1
 
2
2
 
3
3
  /**
4
- * Native square root
5
- * @function
6
- * @param {Number} number - js double number
7
- * @return {Number} result
4
+ * Calculates the square root of a number.
5
+ * @param {number} n - The number to calculate the square root of.
6
+ * @returns {number} The square root of n.
8
7
  */
9
8
  export const squareRoot = n => n ** 0.5;
10
9
 
11
10
  /**
12
- * Calculate square root using Newton's formula
13
- * @function
14
- * @param {BigInt} number - big integer number
15
- * @return {BigInt} result
11
+ * Calculates the integer square root of a BigInt using Newton's method.
12
+ * @param {bigint} n - The BigInt to calculate the square root of.
13
+ * @returns {bigint} The integer square root of n.
16
14
  */
17
15
  export const squareRootBI = n => {
18
16
  if (n === 0n) return 0n;
@@ -3,8 +3,13 @@ import {
3
3
  powModBI,
4
4
  } from './pow-mod.mjs';
5
5
 
6
- /* Takes as input an odd prime p and n < p and returns r
7
- * such that r * r = n [mod p]. */
6
+ /**
7
+ * Computes the modular square root of n modulo p using the Tonelli-Shanks algorithm.
8
+ * Solves the congruence r^2 = n (mod p) where p is an odd prime.
9
+ * @param {bigint} n - The number to find the square root of.
10
+ * @param {bigint} p - The modulus (must be an odd prime).
11
+ * @returns {bigint} The square root r such that r^2 = n (mod p), or 0n if no solution exists.
12
+ */
8
13
  export const tonelliShanksBI = (n, p) => {
9
14
  let s = 0n;
10
15
  let q = p - 1n;