@gkucmierz/utils 3.0.0 → 3.0.2

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/main.mjs CHANGED
@@ -42,7 +42,7 @@ import {
42
42
  getType
43
43
  } from './src/get-type.mjs'
44
44
  import {
45
- goldenRatio, goldenRatioBI
45
+ goldenRatio, goldenRatioBI, goldenRatioStr
46
46
  } from './src/golden-ratio.mjs'
47
47
  import {
48
48
  gpn, gpnBI
@@ -166,5 +166,5 @@ export * from './src/square-root.mjs';
166
166
  export * from './src/tonelli-shanks.mjs';
167
167
 
168
168
  export default [
169
- 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, goldenRatio, goldenRatioBI, gpn, gpnBI, bin2gray, gray2bin, Heap, heronsFormula, heronsFormulaBI, createLangtonsAnt, createUnlimitedGrid, lcm, lcmBI, ListNode, lucasLehmerBI, axisAngleToMatrix4, crossProduct, dotProduct, getRotationMatrixFromVectors, multiplyMatrix4, normalize, projectToTrackball, matrixAsArray, measurePerformance, memoize, mod, modBI, nChooseK, naturalSearch, nelderMead, particleSwarmOptimization, permutations, permutationsIterator, phi, phiBI, powMod, powModBI, randNormal, array2range, range2array, setSafeInterval, simulatedAnnealing, squareRoot, squareRootBI, tonelliShanksBI
169
+ 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, goldenRatio, goldenRatioBI, goldenRatioStr, gpn, gpnBI, bin2gray, gray2bin, Heap, heronsFormula, heronsFormulaBI, createLangtonsAnt, createUnlimitedGrid, lcm, lcmBI, ListNode, lucasLehmerBI, axisAngleToMatrix4, crossProduct, dotProduct, getRotationMatrixFromVectors, multiplyMatrix4, normalize, projectToTrackball, matrixAsArray, measurePerformance, memoize, mod, modBI, nChooseK, naturalSearch, nelderMead, particleSwarmOptimization, permutations, permutationsIterator, phi, phiBI, powMod, powModBI, randNormal, array2range, range2array, setSafeInterval, simulatedAnnealing, squareRoot, squareRootBI, tonelliShanksBI
170
170
  ];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gkucmierz/utils",
3
- "version": "3.0.0",
3
+ "version": "3.0.2",
4
4
  "type": "module",
5
5
  "description": "Usefull functions for solving programming tasks",
6
6
  "keywords": [
@@ -10,7 +10,7 @@ export const binarySearchArr = (arr, target) => {
10
10
  let [a, b] = [0, arr.length];
11
11
  let lm;
12
12
  while (b - a > 0) {
13
- const mid = (a + b) / 2 | 0;
13
+ const mid = Math.trunc((a + b) / 2);
14
14
  const val = arr[mid];
15
15
  if (target < val) {
16
16
  b = mid;
@@ -36,7 +36,7 @@ export const binarySearchLE = (arr, target) => {
36
36
  let [a, b] = [0, arr.length];
37
37
  let lm;
38
38
  while (b - a > 0) {
39
- const mid = (a + b) / 2 | 0;
39
+ const mid = Math.trunc((a + b) / 2);
40
40
  const val = arr[mid];
41
41
  if (target < val) {
42
42
  b = mid;
@@ -60,7 +60,7 @@ export const binarySearchGE = (arr, target) => {
60
60
  let [a, b] = [0, arr.length];
61
61
  let lm;
62
62
  while (b - a > 0) {
63
- const mid = (a + b) / 2 | 0;
63
+ const mid = Math.trunc((a + b) / 2);
64
64
  const val = arr[mid];
65
65
  if (target > val) {
66
66
  a = mid;
@@ -93,7 +93,7 @@ const binarySearchL = (arr, target) => {
93
93
  let [a, b] = [0, arr.length];
94
94
  let lm;
95
95
  while (b - a > 0) {
96
- const mid = (a + b) / 2 | 0;
96
+ const mid = Math.trunc((a + b) / 2);
97
97
  const val = arr[mid];
98
98
  if (target <= val) {
99
99
  b = mid;
@@ -111,7 +111,7 @@ const binarySearchG = (arr, target) => {
111
111
  let [a, b] = [0, arr.length];
112
112
  let lm;
113
113
  while (b - a > 0) {
114
- const mid = (a + b) / 2 | 0;
114
+ const mid = Math.trunc((a + b) / 2);
115
115
  const val = arr[mid];
116
116
  if (target >= val) {
117
117
  a = mid;
package/src/egcd.mjs CHANGED
@@ -12,7 +12,7 @@ export const egcd = (a, b) => {
12
12
  let [x, y] = [0, 1];
13
13
  let [u, v] = [1, 0];
14
14
  while (a !== 0) {
15
- const [q, r] = [b/a | 0, b%a];
15
+ const [q, r] = [Math.trunc(b / a), b % a];
16
16
  const [m, n] = [x - u * q, y - v * q];
17
17
  [b, a, x, y, u, v] = [a, r, u, v, m, n];
18
18
  }
@@ -9,11 +9,9 @@ export const goldenRatio = ((5 ** 0.5) + 1) / 2;
9
9
 
10
10
  /**
11
11
  * Calculates the Golden Ratio (phi) to an arbitrary precision using BigInt.
12
- * Returns a boxed BigInt object with an overridden toString() method to
13
- * automatically format the output with the correct decimal point placement.
14
12
  *
15
13
  * @param {number|bigint} precision - The number of decimal places to calculate.
16
- * @returns {BigInt} A boxed BigInt representing the Golden Ratio scaled by 10^precision.
14
+ * @returns {BigInt} A primitive BigInt representing the Golden Ratio scaled by 10^precision.
17
15
  * @throws {Error} If precision is negative.
18
16
  * @see {@link https://instacode.app/run/FASwtgDg9gTgLgAgN4IOZQDYBMCmA7AJQEM4QoAhASQQF8EAzGKMBAIgAFUBrAVwGMwIHDABeAeh6kMAZ1YBuYMHTZ8xUhUoKgA|▶ Try it live in Instacode}
19
17
  */
@@ -24,17 +22,22 @@ export const goldenRatioBI = precision => {
24
22
 
25
23
  // (sqrt(5 * exp^2) + exp) / 2
26
24
  const result = (squareRootBI(5n * exp * exp) + exp) / 2n;
25
+ return result;
26
+ };
27
+
28
+ /**
29
+ * Calculates the Golden Ratio (phi) to an arbitrary precision and returns it as a formatted string.
30
+ * @param {number|bigint} precision - The number of decimal places to calculate.
31
+ * @returns {string} The formatted Golden Ratio string (e.g., '1.618...').
32
+ * @throws {Error} If precision is negative.
33
+ * @see {@link https://instacode.app/run/FASwtgDg9gTgLgAgN4IOZQDYBMCmA7AJQEM4QoBlOGBAXwQDMYowEAiAAVQGsBXAYzAgcMAF4B6HqQwBnVgG5gwdNnzFSFKgqA|▶ Try it live in Instacode}
34
+ */
35
+ export const goldenRatioStr = precision => {
36
+ if (precision < 0) throw new Error('Precision cannot be negative');
37
+ const str = goldenRatioBI(precision).toString();
38
+ if (precision === 0 || precision === 0n) return str;
27
39
 
28
- // Wrap primitive BigInt in an Object (Boxing)
29
- const boxedResult = Object(result);
30
- boxedResult.toString = () => {
31
- const str = result.toString();
32
- if (precision === 0) return str;
33
-
34
- // The Golden Ratio is ~1.618, so the integer part is always '1'.
35
- // The length of str is exactly precision + 1.
36
- return str.slice(0, 1) + '.' + str.slice(1);
37
- };
38
-
39
- return boxedResult;
40
+ // The Golden Ratio is ~1.618, so the integer part is always '1'.
41
+ // The length of str is exactly precision + 1.
42
+ return str.slice(0, 1) + '.' + str.slice(1);
40
43
  };
package/src/heap.mjs CHANGED
@@ -18,7 +18,7 @@ export class Heap {
18
18
 
19
19
  #up(idx) {
20
20
  while (idx > 1) {
21
- const ni = idx / 2 | 0;
21
+ const ni = Math.trunc(idx / 2);
22
22
  if (this.#valFn(this.#arr[idx]) < this.#valFn(this.#arr[ni])) {
23
23
  [this.#arr[idx], this.#arr[ni]] = [this.#arr[ni], this.#arr[idx]];
24
24
  }
package/src/pow-mod.mjs CHANGED
@@ -3,6 +3,7 @@
3
3
 
4
4
  const getPowMod = (ZERO, ONE, TWO, floor) => {
5
5
  return (base, exponent, modulus = null) => {
6
+ if (exponent < ZERO) throw new RangeError('Negative exponent is not supported');
6
7
  if (modulus === null) return base ** exponent;
7
8
  if (modulus === ONE) return ZERO;
8
9
  let result = ONE;