@gkucmierz/utils 1.28.3 → 1.28.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.
Files changed (54) hide show
  1. package/README.md +55 -4
  2. package/package.json +31 -2
  3. package/src/SetCnt.mjs +52 -6
  4. package/src/Trie.mjs +35 -2
  5. package/src/base64.mjs +24 -0
  6. package/src/bijective-numeration.mjs +24 -0
  7. package/src/binary-search.mjs +25 -5
  8. package/src/copy-case.mjs +5 -5
  9. package/src/egcd.mjs +8 -0
  10. package/src/factors.mjs +6 -8
  11. package/src/gcd.mjs +8 -8
  12. package/src/get-type.mjs +6 -2
  13. package/src/gpn.mjs +14 -0
  14. package/src/heap.mjs +26 -0
  15. package/src/herons-formula.mjs +17 -0
  16. package/src/lcm.mjs +13 -0
  17. package/src/list-node.mjs +19 -0
  18. package/src/matrix.mjs +6 -3
  19. package/src/memoize.mjs +5 -5
  20. package/src/mod.mjs +8 -14
  21. package/src/phi.mjs +13 -0
  22. package/src/pow-mod.mjs +12 -12
  23. package/src/range-array.mjs +12 -0
  24. package/src/square-root.mjs +6 -8
  25. package/src/tonelli-shanks.mjs +7 -2
  26. package/.github/workflows/deploy-docs.yml +0 -22
  27. package/.github/workflows/npm-publish.yml +0 -25
  28. package/.husky/pre-commit +0 -4
  29. package/jsdoc.json +0 -10
  30. package/package-lock.json +0 -1422
  31. package/scripts/generate_main.mjs +0 -43
  32. package/spec/SetCnt.spec.mjs +0 -58
  33. package/spec/Trie.spec.mjs +0 -69
  34. package/spec/base64.spec.mjs +0 -29
  35. package/spec/bijective-numeration.spec.mjs +0 -123
  36. package/spec/binary-search.spec.mjs +0 -91
  37. package/spec/copy-case.spec.mjs +0 -12
  38. package/spec/factors.spec.mjs +0 -35
  39. package/spec/gcd.spec.mjs +0 -27
  40. package/spec/get-type.spec.mjs +0 -75
  41. package/spec/gpn.spec.mjs +0 -35
  42. package/spec/heap.spec.mjs +0 -32
  43. package/spec/herons-formula.spec.mjs +0 -41
  44. package/spec/list-node.spec.mjs +0 -33
  45. package/spec/main.spec.mjs +0 -8
  46. package/spec/matrix.spec.mjs +0 -19
  47. package/spec/memoize.spec.mjs +0 -46
  48. package/spec/mod.spec.mjs +0 -39
  49. package/spec/phi.spec.mjs +0 -60
  50. package/spec/pow-mod.spec.mjs +0 -21
  51. package/spec/range-array.spec.mjs +0 -19
  52. package/spec/square-root.spec.mjs +0 -22
  53. package/spec/support/jasmine.json +0 -16
  54. package/spec/tonelli-shanks.spec.mjs +0 -15
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,10 +1,10 @@
1
1
 
2
2
  /**
3
- * Memoize function, that builds tree structure for arguments
4
- * and compares arguments by reference
5
- * @method
6
- * @param {Function} Function to memoize
7
- * @return {Function} Memoized function
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
8
  */
9
9
  export const memoize = fn => {
10
10
  const maps = [];
package/src/mod.mjs CHANGED
@@ -10,23 +10,17 @@ const getMod = ZERO => {
10
10
  };
11
11
 
12
12
  /**
13
- * Python like modulo implementation.
14
- * It behaves different than JavaScript %
15
- * with negative values
16
- * @method
17
- * @param {Number} Dividend
18
- * @param {Number} Divisor
19
- * @return {Number} Modulus
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.
20
17
  */
21
18
  export const mod = getMod(0);
22
19
 
23
20
  /**
24
- * Python like modulo implementation.
25
- * It behaves different than JavaScript %
26
- * with negative values
27
- * @method
28
- * @param {BigInt} Dividend
29
- * @param {BigInt} Divisor
30
- * @return {BigInt} Modulus
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.
31
25
  */
32
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
@@ -19,21 +19,21 @@ const getPowMod = (ZERO, ONE, TWO, floor) => {
19
19
  };
20
20
 
21
21
  /**
22
- * Power like python implementation with optional modulus
23
- * @method
24
- * @param {Number} Base
25
- * @param {Number} Exponent
26
- * @param [Number] Modulus
27
- * @return {Number} Power
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
28
  */
29
29
  export const powMod = getPowMod(0, 1, 2, n => Math.floor(n));
30
30
 
31
31
  /**
32
- * Power like python implementation with optional modulus
33
- * @method
34
- * @param {BigInt} Base
35
- * @param {BigInt} Exponent
36
- * @param [BigInt] Modulus
37
- * @return {BigInt} Power
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
38
  */
39
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;
@@ -1,22 +0,0 @@
1
- name: Deploy docs
2
- on:
3
- push:
4
- branches:
5
- - main # Change this to your default branch
6
- jobs:
7
- build-and-deploy:
8
- concurrency: ci-${{ github.ref }} # Recommended if you intend to make multiple deployments in quick succession.
9
- runs-on: ubuntu-latest
10
- steps:
11
- - name: Checkout 🛎️
12
- uses: actions/checkout@v4
13
-
14
- - name: Install and Build 🔧 # This example project is built using npm and outputs the result to the 'build' folder. Replace with the commands required to build your project, or remove this step entirely if your site is pre-built.
15
- run: |
16
- npm i
17
- npm run docs:build
18
-
19
- - name: Deploy 🚀
20
- uses: JamesIves/github-pages-deploy-action@v4
21
- with:
22
- folder: docs # The folder the action should deploy.
@@ -1,25 +0,0 @@
1
- name: npm-publish
2
- on:
3
- push:
4
- branches:
5
- - main # Change this to your default branch
6
- jobs:
7
- npm-publish:
8
- name: npm-publish
9
- runs-on: ubuntu-latest
10
- steps:
11
- - name: Checkout repository
12
- uses: actions/checkout@v2
13
- - name: Publish if version has been updated
14
- uses: pascalgn/npm-publish-action@1.3.9
15
- with: # All of theses inputs are optional
16
- tag_name: "v%s"
17
- tag_message: "v%s"
18
- create_tag: "true"
19
- commit_pattern: "^Release (\\S+)"
20
- workspace: "."
21
- publish_command: "yarn"
22
- publish_args: "--non-interactive --access public"
23
- env: # More info about the environment variables in the README
24
- GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Leave this as is, it's automatically generated
25
- NPM_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }} # You need to set this in your repo settings
package/.husky/pre-commit DELETED
@@ -1,4 +0,0 @@
1
- #!/usr/bin/env sh
2
- . "$(dirname -- "$0")/_/husky.sh"
3
-
4
- npm test
package/jsdoc.json DELETED
@@ -1,10 +0,0 @@
1
- {
2
- "source": {
3
- "include": ["src"],
4
- "includePattern": ".+\\.(js(doc|x)?|mjs)$"
5
- },
6
- "opts": {
7
- "destination": "./docs/",
8
- "template": "node_modules/docdash"
9
- }
10
- }