@gkucmierz/utils 1.28.8 → 1.30.0

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
@@ -2,6 +2,9 @@
2
2
  import {
3
3
  SetCnt
4
4
  } from './src/SetCnt.mjs'
5
+ import {
6
+ Trie
7
+ } from './src/Trie.mjs'
5
8
  import {
6
9
  fromBase64, fromBase64Url, toBase64, toBase64Url
7
10
  } from './src/base64.mjs'
@@ -9,7 +12,7 @@ import {
9
12
  bijective2num, bijective2numBI, num2bijective, num2bijectiveBI
10
13
  } from './src/bijective-numeration.mjs'
11
14
  import {
12
- binarySearchArr
15
+ binarySearchArr, binarySearchGE, binarySearchLE, binarySearchRangeIncl
13
16
  } from './src/binary-search.mjs'
14
17
  import {
15
18
  copyCase
@@ -20,6 +23,9 @@ import {
20
23
  import {
21
24
  factors, factorsBI
22
25
  } from './src/factors.mjs'
26
+ import {
27
+ formatBigNumber, formatBigNumberBI, wrapFn
28
+ } from './src/format-big-number.mjs'
23
29
  import {
24
30
  gcd, gcdBI
25
31
  } from './src/gcd.mjs'
@@ -65,14 +71,19 @@ import {
65
71
  import {
66
72
  tonelliShanksBI
67
73
  } from './src/tonelli-shanks.mjs'
74
+ import {
75
+ naturalSearch
76
+ } from './src/natural-search.mjs'
68
77
 
69
78
  export * from './src/SetCnt.mjs';
79
+ export * from './src/Trie.mjs';
70
80
  export * from './src/base64.mjs';
71
81
  export * from './src/bijective-numeration.mjs';
72
82
  export * from './src/binary-search.mjs';
73
83
  export * from './src/copy-case.mjs';
74
84
  export * from './src/egcd.mjs';
75
85
  export * from './src/factors.mjs';
86
+ export * from './src/format-big-number.mjs';
76
87
  export * from './src/gcd.mjs';
77
88
  export * from './src/get-type.mjs';
78
89
  export * from './src/gpn.mjs';
@@ -88,7 +99,8 @@ export * from './src/pow-mod.mjs';
88
99
  export * from './src/range-array.mjs';
89
100
  export * from './src/square-root.mjs';
90
101
  export * from './src/tonelli-shanks.mjs';
102
+ export * from './src/natural-search.mjs';
91
103
 
92
104
  export default [
93
- SetCnt, fromBase64, fromBase64Url, toBase64, toBase64Url, bijective2num, bijective2numBI, num2bijective, num2bijectiveBI, binarySearchArr, copyCase, egcd, factors, factorsBI, gcd, gcdBI, getType, gpn, gpnBI, Heap, heronsFormula, heronsFormulaBI, lcm, lcmBI, ListNode, matrixAsArray, memoize, mod, modBI, phi, phiBI, powMod, powModBI, array2range, range2array, squareRoot, squareRootBI, tonelliShanksBI
105
+ SetCnt, Trie, fromBase64, fromBase64Url, toBase64, toBase64Url, bijective2num, bijective2numBI, num2bijective, num2bijectiveBI, binarySearchArr, binarySearchGE, binarySearchLE, binarySearchRangeIncl, copyCase, egcd, factors, factorsBI, formatBigNumber, formatBigNumberBI, wrapFn, gcd, gcdBI, getType, gpn, gpnBI, Heap, heronsFormula, heronsFormulaBI, lcm, lcmBI, ListNode, matrixAsArray, memoize, mod, modBI, phi, phiBI, powMod, powModBI, array2range, range2array, squareRoot, squareRootBI, tonelliShanksBI, naturalSearch
94
106
  ];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gkucmierz/utils",
3
- "version": "1.28.8",
3
+ "version": "1.30.0",
4
4
  "description": "Usefull functions for solving programming tasks",
5
5
  "keywords": [
6
6
  "utils",
@@ -0,0 +1,49 @@
1
+
2
+ /**
3
+ * Formats a BigNumber, Number or string representation of a number
4
+ * by separating thousands with a provided separator.
5
+ *
6
+ * @param {number|string|bigint} num - The number to format.
7
+ * @param {string} [separator=''] - The string to use as a thousand separator.
8
+ * @param {function(string): string} [wrapFn=_=>_] - Optional function to wrap each part of the number.
9
+ * @returns {string} The formatted number.
10
+ */
11
+ const formatBigNumberBoth = (num, separator = '', wrapFn = _ => _) => {
12
+ const str = String(num);
13
+ const rev = [...str].reverse().join('');
14
+ const match = rev.match(/(\d*\.\-?)|(\d{3}\-?)|(\d{1,3}\-?)|(\-)/g);
15
+ const revInside = (match && match.join('') === rev)
16
+ ? match.map(part => [...part].reverse().join(''))
17
+ : [str];
18
+ const wrap = revInside.map(wrapFn);
19
+ return wrap.reverse().join(separator);
20
+ };
21
+
22
+ /**
23
+ * Formats a BigNumber, Number or string representation of a number
24
+ * by separating thousands with a provided separator.
25
+ * @param {number|string|bigint} num - The number to format.
26
+ * @param {string} [separator=''] - The string to use as a thousand separator.
27
+ * @param {function(string): string} [wrapFn=_=>_] - Optional function to wrap each part of the number.
28
+ * @returns {string} The formatted number.
29
+ */
30
+ export const formatBigNumber = formatBigNumberBoth;
31
+
32
+ /**
33
+ * Formats a BigNumber, Number or string representation of a number
34
+ * by separating thousands with a provided separator. (Alias for BigInt logic)
35
+ * @param {number|string|bigint} num - The number to format.
36
+ * @param {string} [separator=''] - The string to use as a thousand separator.
37
+ * @param {function(string): string} [wrapFn=_=>_] - Optional function to wrap each part of the number.
38
+ * @returns {string} The formatted number.
39
+ */
40
+ export const formatBigNumberBI = formatBigNumberBoth;
41
+
42
+ export const wrapFn = () => {
43
+ let even = true;
44
+ return part => {
45
+ const cls = even ? 'even' : 'odd';
46
+ even = !even;
47
+ return `<span class="${cls}">${part}</span>`;
48
+ };
49
+ };
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Natural Search Algorithm by gkucmierz
3
+ * Mathematically finds the exponential boundary and binary searches bounds for a conditional function.
4
+ * Useful for finding bounds in sigmoidal functions without known boundaries.
5
+ * @param {Function} cond - The condition evaluator function.
6
+ * @param {Boolean} [retFirstTrue=true] - If true, returns the first match bounding condition true.
7
+ * @returns {Number} The optimized integer step boundary that satisfies the condition structure.
8
+ */
9
+ export const naturalSearch = (cond, retFirstTrue = true) => {
10
+ let min = 1;
11
+ let max = 1;
12
+ while(1) {
13
+ const stop = cond(max);
14
+ if (stop) break;
15
+ min = max;
16
+ max *= 2;
17
+ }
18
+ let mid;
19
+ while (1) {
20
+ mid = Math.floor((min + max) / 2);
21
+ const stop = cond(mid);
22
+ if (stop) {
23
+ max = mid;
24
+ } else {
25
+ min = mid;
26
+ }
27
+ const diff = max - min;
28
+ if (max - min <= 1) {
29
+ return retFirstTrue ? max : min;
30
+ }
31
+ }
32
+ };