@gkucmierz/utils 1.28.7 → 1.29.1

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'
@@ -67,12 +73,14 @@ import {
67
73
  } from './src/tonelli-shanks.mjs'
68
74
 
69
75
  export * from './src/SetCnt.mjs';
76
+ export * from './src/Trie.mjs';
70
77
  export * from './src/base64.mjs';
71
78
  export * from './src/bijective-numeration.mjs';
72
79
  export * from './src/binary-search.mjs';
73
80
  export * from './src/copy-case.mjs';
74
81
  export * from './src/egcd.mjs';
75
82
  export * from './src/factors.mjs';
83
+ export * from './src/format-big-number.mjs';
76
84
  export * from './src/gcd.mjs';
77
85
  export * from './src/get-type.mjs';
78
86
  export * from './src/gpn.mjs';
@@ -90,5 +98,5 @@ export * from './src/square-root.mjs';
90
98
  export * from './src/tonelli-shanks.mjs';
91
99
 
92
100
  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
101
+ 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
94
102
  ];
package/package.json CHANGED
@@ -1,7 +1,36 @@
1
1
  {
2
2
  "name": "@gkucmierz/utils",
3
- "version": "1.28.7",
3
+ "version": "1.29.1",
4
4
  "description": "Usefull functions for solving programming tasks",
5
+ "keywords": [
6
+ "utils",
7
+ "algorithms",
8
+ "math",
9
+ "number-theory",
10
+ "gcd",
11
+ "lcm",
12
+ "factors",
13
+ "phi",
14
+ "mod",
15
+ "pow-mod",
16
+ "binary-search",
17
+ "heap",
18
+ "trie",
19
+ "base64",
20
+ "bijective",
21
+ "tonelli-shanks",
22
+ "square-root",
23
+ "memoize",
24
+ "range-array",
25
+ "javascript",
26
+ "node",
27
+ "competitive-programming",
28
+ "data-structures"
29
+ ],
30
+ "files": [
31
+ "main.mjs",
32
+ "src/"
33
+ ],
5
34
  "main": "main.mjs",
6
35
  "scripts": {
7
36
  "test": "jasmine",
@@ -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
+ };
@@ -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,21 +0,0 @@
1
- name: npm-publish
2
-
3
- on:
4
- push:
5
- tags:
6
- - 'v*'
7
- workflow_dispatch:
8
-
9
- jobs:
10
- publish:
11
- runs-on: ubuntu-latest
12
- steps:
13
- - uses: actions/checkout@v4
14
- - uses: actions/setup-node@v4
15
- with:
16
- node-version: '20.x'
17
- registry-url: 'https://registry.npmjs.org'
18
- - run: npm ci
19
- - run: npm publish --access public
20
- env:
21
- NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
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
- }
@@ -1,43 +0,0 @@
1
-
2
- import fs from 'fs';
3
- import path from 'path';
4
- import { fileURLToPath } from 'url';
5
-
6
- const MAIN_FILE = './main.mjs';
7
-
8
- const __filename = fileURLToPath(import.meta.url);
9
- const __dirname = path.dirname([path.dirname(__filename), '/../src/.'].join(''));
10
-
11
- const files = fs.readdirSync(__dirname);
12
-
13
- const utilsFiles = files
14
- .filter(name => name.match(/\.mjs$/))
15
- .filter(name => name !== 'main.mjs');
16
-
17
- const allMethods = {};
18
- const map = new Map();
19
-
20
- for (const file of utilsFiles) {
21
- const f = [__dirname, file].join('/');
22
- const obj = await import(f);
23
- map.set(file, Object.keys(obj));
24
- Object.keys(obj).map(key => {
25
- if (key in allMethods) throw Error('Duplicate method name');
26
- allMethods[key] = obj[key];
27
- });
28
- };
29
-
30
- fs.writeFileSync(MAIN_FILE, [
31
- '',
32
- ...[...map].map(([file, methods]) => {
33
- return `import {\n ${methods.join(', ')}\n} from './src/${file}'`;
34
- }),
35
- '',
36
- ...utilsFiles.map(file => `export * from './src/${file}';`),
37
- '',
38
- `export default [`,
39
- ` ${Object.keys(allMethods).join(', ')}`,
40
- `];`,
41
- '',
42
- ].join('\n'));
43
-
@@ -1,58 +0,0 @@
1
-
2
- import {
3
- SetCnt,
4
- } from '../src/SetCnt.mjs';
5
-
6
- const set = new Set([1,2,3,5])
7
- const map = new Map([[1,2],[3,5]])
8
- // console.log([...sc.entries()]);
9
-
10
- describe('SetCnt', () => {
11
- it('has', () => {
12
- const sc = new SetCnt([1,2,5,6,1,1]);
13
- expect(sc.has(1)).toBe(true);
14
- expect(sc.has(2)).toBe(true);
15
- expect(sc.has(5)).toBe(true);
16
- });
17
-
18
- it('add', () => {
19
- const sc = new SetCnt([1,2,5,6,1,1]);
20
- expect(sc.add(123)).toBe(sc);
21
- expect(sc.has(123)).toBe(true);
22
- });
23
-
24
- it('delete', () => {
25
- const sc = new SetCnt();
26
- expect(sc.add(123)).toBe(sc);
27
- expect(sc.add(123)).toBe(sc);
28
-
29
- expect(sc.delete(123)).toBe(true);
30
- expect(sc.has(123)).toBe(true);
31
-
32
- expect(sc.delete(123)).toBe(true);
33
- expect(sc.has(123)).toBe(false);
34
-
35
- expect(sc.delete(123)).toBe(false);
36
- });
37
-
38
- it('deleteAll', () => {
39
- const sc = new SetCnt();
40
- expect(sc.add(123)).toBe(sc);
41
- expect(sc.add(123)).toBe(sc);
42
-
43
- expect(sc.deleteAll(123)).toBe(true);
44
- expect(sc.has(123)).toBe(false);
45
- });
46
-
47
- it('cnt', () => {
48
- const sc = new SetCnt();
49
- expect(sc.cnt(123)).toBe(0);
50
-
51
- expect(sc.add(123)).toBe(sc);
52
- expect(sc.add(123)).toBe(sc);
53
-
54
- expect(sc.cnt(123)).toBe(2);
55
- expect(sc.deleteAll(123)).toBe(true);
56
- expect(sc.cnt(123)).toBe(0);
57
- });
58
- });
@@ -1,69 +0,0 @@
1
-
2
- import {
3
- Trie,
4
- } from '../src/Trie.mjs';
5
-
6
- describe('Trie', () => {
7
- it('check empty', () => {
8
- const trie = Trie();
9
- expect(trie.has('')).toBe(false);
10
- expect(trie.has('abc')).toBe(false);
11
- });
12
-
13
- it('init data', () => {
14
- const trie = Trie(['abc', '']);
15
- expect(trie.has('')).toBe(true);
16
- expect(trie.has('abc')).toBe(true);
17
- });
18
-
19
- it('add/readd data', () => {
20
- const trie = Trie();
21
- expect(trie.has('')).toBe(false);
22
- expect(trie.add('')).toBe(true);
23
- expect(trie.has('')).toBe(true);
24
- expect(trie.add('')).toBe(false);
25
- });
26
-
27
- it('list data', () => {
28
- const trie = Trie(['abc', '', 'abcdef', 'xyz']);
29
- const abc = ['abc', 'abcdef'];
30
- expect(trie.get('a')).toEqual(abc);
31
- expect(trie.get('ab')).toEqual(abc);
32
- expect(trie.get('abc')).toEqual(abc);
33
- expect(trie.get('aa')).toEqual([]);
34
- expect(trie.get('')).toEqual(['', 'abc', 'abcdef', 'xyz']);
35
- });
36
-
37
- it('remove node', () => {
38
- const trie = Trie(['a', 'ab', 'abc']);
39
-
40
- expect(trie.has('abc')).toEqual(true);
41
- expect(trie.remove('abc')).toEqual(true);
42
- expect(trie.remove('abc')).toEqual(false);
43
- expect(trie.has('abc')).toEqual(false);
44
-
45
- expect(trie.has('ab')).toEqual(true);
46
- expect(trie.remove('ab')).toEqual(true);
47
- expect(trie.remove('ab')).toEqual(false);
48
- expect(trie.has('ab')).toEqual(false);
49
-
50
- expect(trie.has('a')).toEqual(true);
51
- expect(trie.remove('a')).toEqual(true);
52
- expect(trie.remove('a')).toEqual(false);
53
- expect(trie.has('a')).toEqual(false);
54
- });
55
-
56
- it('keep child node after parent removal', () => {
57
- const trie = Trie(['a', 'abc']);
58
-
59
- expect(trie.has('a')).toEqual(true);
60
- expect(trie.remove('a')).toEqual(true);
61
- expect(trie.remove('a')).toEqual(false);
62
- expect(trie.has('a')).toEqual(false);
63
-
64
- expect(trie.has('abc')).toEqual(true);
65
- expect(trie.remove('abc')).toEqual(true);
66
- expect(trie.remove('abc')).toEqual(false);
67
- expect(trie.has('abc')).toEqual(false);
68
- })
69
- });
@@ -1,29 +0,0 @@
1
-
2
- import {
3
- toBase64,
4
- fromBase64,
5
- toBase64Url,
6
- fromBase64Url,
7
- } from '../src/base64.mjs';
8
-
9
- describe('base64', () => {
10
- it('toBase64', () => {
11
- expect(toBase64('🥚')).toBe('Ptha3Q==');
12
- expect(toBase64('🐔')).toBe('PdgU3A==');
13
- expect(toBase64('')).toBe('++8=');
14
- });
15
-
16
- it('fromBase64', () => {
17
- expect(fromBase64('Ptha3Q==')).toBe('🥚');
18
- expect(fromBase64('PdgU3A==')).toBe('🐔');
19
- expect(fromBase64('++8=')).toBe('');
20
- });
21
-
22
- it('toBase64Url', () => {
23
- expect(toBase64Url('')).toBe('--8=');
24
- });
25
-
26
- it('fromBase64', () => {
27
- expect(fromBase64Url('--8=')).toBe('');
28
- });
29
- });
@@ -1,123 +0,0 @@
1
-
2
- import {
3
- num2bijective,
4
- bijective2num,
5
- num2bijectiveBI,
6
- bijective2numBI,
7
- } from '../src/bijective-numeration.mjs';
8
-
9
- describe('bijective-numeration', () => {
10
- const alpha = 'abcdefghijklmnopqrstuvwxyz';
11
-
12
- const map = [
13
- [0, ''],
14
- [1, '1'],
15
- [2, '2'],
16
- [3, '11'],
17
- [4, '12'],
18
- [5, '21'],
19
- [6, '22'],
20
- [7, '111'],
21
- ];
22
-
23
- const mapAlpha = [
24
- [0, ''],
25
- [1, 'a'],
26
- [26, 'z'],
27
- [26 + 1, 'aa'],
28
- [26 * 26 + 26, 'zz'],
29
- [26 * 26 + 26 + 1, 'aaa'],
30
- ];
31
-
32
- it('num2bijective', () => {
33
- map.map(([num, bij]) => {
34
- expect(num2bijective(num)).toEqual(bij);
35
- });
36
- });
37
-
38
- it('num2bijective alpha', () => {
39
- mapAlpha.map(([num, bij]) => {
40
- expect(num2bijective(num, alpha)).toEqual(bij);
41
- });
42
- });
43
-
44
- it('bijective2num', () => {
45
- map.map(([num, bij]) => {
46
- expect(bijective2num(bij)).toEqual(num);
47
- });
48
- });
49
-
50
- it('bijective2num alpha', () => {
51
- mapAlpha.map(([num, bij]) => {
52
- expect(bijective2num(bij, alpha)).toEqual(num);
53
- });
54
- });
55
-
56
- it('random tests', () => {
57
- for (let i = 0; i < 1e3; ++i) {
58
- const n = Math.round(Math.random() * 1e6);
59
- const bj = num2bijective(n, alpha);
60
- expect(bijective2num(bj, alpha)).toEqual(n);
61
- }
62
- });
63
- });
64
-
65
- describe('bijective-numeration BigInt', () => {
66
- const alpha = 'abcdefghijklmnopqrstuvwxyz';
67
-
68
- const map = [
69
- [0n, ''],
70
- [1n, '1'],
71
- [2n, '2'],
72
- [3n, '11'],
73
- [4n, '12'],
74
- [5n, '21'],
75
- [6n, '22'],
76
- [7n, '111'],
77
- ];
78
-
79
- const mapAlpha = [
80
- [0n, ''],
81
- [1n, 'a'],
82
- [26n, 'z'],
83
- [26n + 1n, 'aa'],
84
- [26n * 26n + 26n, 'zz'],
85
- [26n * 26n + 26n + 1n, 'aaa'],
86
- ];
87
-
88
- it('num2bijectiveBI', () => {
89
- map.map(([num, bij]) => {
90
- expect(num2bijectiveBI(num)).toEqual(bij);
91
- });
92
- });
93
-
94
- it('num2bijectiveBI alpha', () => {
95
- mapAlpha.map(([num, bij]) => {
96
- expect(num2bijectiveBI(num, alpha)).toEqual(bij);
97
- });
98
- });
99
-
100
- it('bijective2numBI', () => {
101
- map.map(([num, bij]) => {
102
- expect(bijective2numBI(bij)).toEqual(num);
103
- });
104
- });
105
-
106
- it('bijective2numBI alpha', () => {
107
- mapAlpha.map(([num, bij]) => {
108
- expect(bijective2numBI(bij, alpha)).toEqual(num);
109
- });
110
- });
111
-
112
- it('random tests', () => {
113
- for (let i = 0; i < 1e3; ++i) {
114
- const b = BigInt(Math.round(Math.random() * 1000));
115
- const n = BigInt(Math.round(Math.random() * 100));
116
- const bi = b ** n;
117
-
118
- const bj = num2bijectiveBI(bi, alpha);
119
- expect(bijective2numBI(bj, alpha)).toEqual(bi);
120
- }
121
- });
122
-
123
- });
@@ -1,91 +0,0 @@
1
-
2
- import {
3
- binarySearchArr,
4
- binarySearchLE,
5
- binarySearchGE,
6
- binarySearchRangeIncl,
7
- } from '../src/binary-search.mjs';
8
-
9
- describe('binarySearchArr', () => {
10
- it('few cases', () => {
11
- expect(binarySearchArr([1, 2, 3, 4], 1)).toBe(0);
12
- expect(binarySearchArr([1, 2, 3, 4], 2)).toBe(1);
13
- expect(binarySearchArr([1, 2, 3, 4], 3)).toBe(2);
14
- expect(binarySearchArr([1, 2, 3, 4], 4)).toBe(3);
15
- expect(binarySearchArr([1, 2, 3, 4], 0)).toBe(-1);
16
- expect(binarySearchArr([1, 2, 3, 4], 5)).toBe(-1);
17
- });
18
- });
19
-
20
- describe('binarySearchLE', () => {
21
- it('mid group', () => {
22
- const arr = [0,1,2,2,3];
23
- expect(binarySearchLE(arr, -100)).toBe(-1);
24
- expect(binarySearchLE(arr, 0)).toBe(0);
25
- expect(binarySearchLE(arr, 1)).toBe(1);
26
- expect(binarySearchLE(arr, 2)).toBe(3);
27
- expect(binarySearchLE(arr, 3)).toBe(4);
28
- expect(binarySearchLE(arr, 100)).toBe(4);
29
- });
30
-
31
- it('begin group', () => {
32
- const arr = [2,2,3];
33
- expect(binarySearchLE(arr, -100)).toBe(-1);
34
- expect(binarySearchLE(arr, 2)).toBe(1);
35
- expect(binarySearchLE(arr, 3)).toBe(2);
36
- expect(binarySearchLE(arr, 100)).toBe(2);
37
- });
38
-
39
- it('end group', () => {
40
- const arr = [1,2,2];
41
- expect(binarySearchLE(arr, -100)).toBe(-1);
42
- expect(binarySearchLE(arr, 1)).toBe(0);
43
- expect(binarySearchLE(arr, 2)).toBe(2);
44
- expect(binarySearchLE(arr, 100)).toBe(2);
45
- });
46
- });
47
-
48
- describe('binarySearchGE', () => {
49
- it('mid group', () => {
50
- const arr = [0,1,2,2,3];
51
- expect(binarySearchGE(arr, -100)).toBe(0);
52
- expect(binarySearchGE(arr, 0)).toBe(0);
53
- expect(binarySearchGE(arr, 1)).toBe(1);
54
- expect(binarySearchGE(arr, 2)).toBe(2);
55
- expect(binarySearchGE(arr, 3)).toBe(4);
56
- expect(binarySearchGE(arr, 100)).toBe(5);
57
- });
58
-
59
- it('begin group', () => {
60
- const arr = [2,2,3];
61
- expect(binarySearchGE(arr, -100)).toBe(0);
62
- expect(binarySearchGE(arr, 2)).toBe(0);
63
- expect(binarySearchGE(arr, 3)).toBe(2);
64
- expect(binarySearchGE(arr, 100)).toBe(3);
65
- });
66
-
67
- it('end group', () => {
68
- const arr = [1,2,2];
69
- expect(binarySearchGE(arr, -100)).toBe(0);
70
- expect(binarySearchGE(arr, 1)).toBe(0);
71
- expect(binarySearchGE(arr, 2)).toBe(1);
72
- expect(binarySearchGE(arr, 100)).toBe(3);
73
- });
74
- });
75
-
76
- describe('binarySearchRangeIncl', () => {
77
- it('basic', () => {
78
- const arr = [0,1,1,3];
79
- const check = [-10, ...arr, 10];
80
- const expected = [
81
- [0, -1],
82
- [0, 0], [1, 2], [1, 2], [3, 3],
83
- [4, 3],
84
- ];
85
-
86
- for (let i = 0; i < check.length; ++i) {
87
- expect(binarySearchRangeIncl(arr, check[i])).toEqual(expected[i]);
88
- }
89
- });
90
- });
91
-
@@ -1,12 +0,0 @@
1
-
2
- import {
3
- copyCase,
4
- } from '../src/copy-case.mjs';
5
-
6
- describe('copy-case', () => {
7
- it('few tests', () => {
8
- expect(copyCase('styczeń', 'january')).toBe('styczeń');
9
- expect(copyCase('styczeń', 'January')).toBe('Styczeń');
10
- expect(copyCase('styczeń', 'JANUARY')).toBe('STYCZEŃ');
11
- });
12
- });
@@ -1,35 +0,0 @@
1
-
2
- import {
3
- factors,
4
- factorsBI,
5
- } from '../src/factors.mjs';
6
-
7
- describe('factors', () => {
8
- it('Number', () => {
9
- expect(factors(0)).toEqual([]);
10
- expect(factors(1)).toEqual([]);
11
- expect(factors(2)).toEqual([2]);
12
- expect(factors(3)).toEqual([3]);
13
- expect(factors(4)).toEqual([2, 2]);
14
- expect(factors(5)).toEqual([5]);
15
- expect(factors(6)).toEqual([2, 3]);
16
- expect(factors(7)).toEqual([7]);
17
- expect(factors(8)).toEqual([2, 2, 2]);
18
- expect(factors(9)).toEqual([3, 3]);
19
- expect(factors(10)).toEqual([2, 5]);
20
- });
21
-
22
- it('BigInt', () => {
23
- expect(factorsBI(0n)).toEqual([]);
24
- expect(factorsBI(1n)).toEqual([]);
25
- expect(factorsBI(2n)).toEqual([2n]);
26
- expect(factorsBI(3n)).toEqual([3n]);
27
- expect(factorsBI(4n)).toEqual([2n, 2n]);
28
- expect(factorsBI(5n)).toEqual([5n]);
29
- expect(factorsBI(6n)).toEqual([2n, 3n]);
30
- expect(factorsBI(7n)).toEqual([7n]);
31
- expect(factorsBI(8n)).toEqual([2n, 2n, 2n]);
32
- expect(factorsBI(9n)).toEqual([3n, 3n]);
33
- expect(factorsBI(10n)).toEqual([2n, 5n]);
34
- });
35
- });
package/spec/gcd.spec.mjs DELETED
@@ -1,27 +0,0 @@
1
-
2
- import {
3
- gcd,
4
- gcdBI,
5
- } from '../src/gcd.mjs';
6
-
7
- describe('gcd', () => {
8
- it('Number', () => {
9
- expect(gcd(42, 56)).toEqual(14);
10
- expect(gcd(461952, 116298)).toEqual(18);
11
- expect(gcd(7966496, 314080416)).toEqual(32);
12
- expect(gcd(24826148, 45296490)).toEqual(526);
13
- expect(gcd(12, 0)).toEqual(12);
14
- expect(gcd(0, 0)).toEqual(0);
15
- expect(gcd(0, 9)).toEqual(9);
16
- });
17
-
18
- it('BigInt', () => {
19
- expect(gcdBI(42n, 56n)).toEqual(14n);
20
- expect(gcdBI(461952n, 116298n)).toEqual(18n);
21
- expect(gcdBI(7966496n, 314080416n)).toEqual(32n);
22
- expect(gcdBI(24826148n, 45296490n)).toEqual(526n);
23
- expect(gcdBI(12n, 0n)).toEqual(12n);
24
- expect(gcdBI(0n, 0n)).toEqual(0n);
25
- expect(gcdBI(0n, 9n)).toEqual(9n);
26
- });
27
- });
@@ -1,75 +0,0 @@
1
-
2
- import { getType } from '../src/get-type.mjs';
3
-
4
- describe('get-type', () => {
5
-
6
- it('should undefined', () => {
7
- expect(getType(undefined)).toEqual('undefined');
8
- });
9
-
10
- it('should null', () => {
11
- expect(getType(null)).toEqual('null');
12
- });
13
-
14
- it('should boolean', () => {
15
- expect(getType(true)).toEqual('boolean');
16
- expect(getType(false)).toEqual('boolean');
17
- expect(getType(new Boolean)).toEqual('boolean');
18
- });
19
-
20
- it('should number', () => {
21
- expect(getType(1)).toEqual('number');
22
- expect(getType(1e3)).toEqual('number');
23
- expect(getType(1_000)).toEqual('number');
24
- expect(getType(NaN)).toEqual('number');
25
- expect(getType(Infinity)).toEqual('number');
26
- });
27
-
28
- it('should bigint', () => {
29
- expect(getType(1n)).toEqual('bigint');
30
- });
31
-
32
- it('should string', () => {
33
- expect(getType('hello')).toEqual('string');
34
- expect(getType(new String)).toEqual('string');
35
- });
36
-
37
- it('should Symbol', () => {
38
- expect(getType(Symbol())).toEqual('symbol');
39
- });
40
-
41
- it('should function', () => {
42
- expect(getType(() => 0)).toEqual('function');
43
- expect(getType(function() { })).toEqual('function');
44
- expect(getType(new Function)).toEqual('function');
45
- });
46
-
47
- it('should generatorfunction', () => {
48
- expect(getType(function*() { })).toEqual('generatorfunction');
49
- });
50
-
51
- it('should date', () => {
52
- expect(getType(new Date)).toEqual('date');
53
- });
54
-
55
- it('should array', () => {
56
- expect(getType([])).toEqual('array');
57
- expect(getType(new Array)).toEqual('array');
58
- });
59
-
60
- it('should object', () => {
61
- expect(getType({})).toEqual('object');
62
- expect(getType(new Object)).toEqual('object');
63
- expect(getType(new Proxy({}, {}))).toEqual('object');
64
- });
65
-
66
- it('should map', () => {
67
- expect(getType(new Map)).toEqual('map');
68
- expect(getType(new WeakMap)).toEqual('weakmap');
69
- });
70
-
71
- it('should set', () => {
72
- expect(getType(new Set)).toEqual('set');
73
- expect(getType(new WeakSet)).toEqual('weakset');
74
- });
75
- });
package/spec/gpn.spec.mjs DELETED
@@ -1,35 +0,0 @@
1
-
2
- import {
3
- gpn,
4
- gpnBI,
5
- } from '../src/gpn.mjs';
6
-
7
- describe('gpn', () => {
8
- it('first terms', () => {
9
- expect(gpn(0)).toBe(0);
10
- expect(gpn(1)).toBe(1);
11
- expect(gpn(2)).toBe(2);
12
- expect(gpn(3)).toBe(5);
13
- expect(gpn(4)).toBe(7);
14
- expect(gpn(5)).toBe(12);
15
- expect(gpn(6)).toBe(15);
16
- expect(gpn(7)).toBe(22);
17
- expect(gpn(8)).toBe(26);
18
- expect(gpn(9)).toBe(35);
19
- });
20
- });
21
-
22
- describe('gpn BI', () => {
23
- it('first terms', () => {
24
- expect(gpnBI(0n)).toBe(0n);
25
- expect(gpnBI(1n)).toBe(1n);
26
- expect(gpnBI(2n)).toBe(2n);
27
- expect(gpnBI(3n)).toBe(5n);
28
- expect(gpnBI(4n)).toBe(7n);
29
- expect(gpnBI(5n)).toBe(12n);
30
- expect(gpnBI(6n)).toBe(15n);
31
- expect(gpnBI(7n)).toBe(22n);
32
- expect(gpnBI(8n)).toBe(26n);
33
- expect(gpnBI(9n)).toBe(35n);
34
- });
35
- });
@@ -1,32 +0,0 @@
1
-
2
- import { Heap } from '../src/heap.mjs';
3
-
4
- describe('Heap', () => {
5
- it('size', () => {
6
- const heap = Heap();
7
- expect(heap.size()).toBe(0);
8
- heap.add(1);
9
- expect(heap.size()).toBe(1);
10
- heap.add(1);
11
- heap.add(1);
12
- expect(heap.size()).toBe(3);
13
- });
14
-
15
- it('take', () => {
16
- const heap = Heap();
17
- heap.add(2);
18
- heap.add(1);
19
- heap.add(3);
20
- expect(heap.take()).toBe(1);
21
- expect(heap.take()).toBe(2);
22
- expect(heap.take()).toBe(3);
23
- });
24
-
25
- it('valFn', () => {
26
- const heap = Heap(obj => obj.value);
27
- heap.add({ value: 42, other: 'second' });
28
- heap.add({ value: 23, other: 'first' });
29
- expect(heap.take().other).toBe('first');
30
- expect(heap.take().other).toBe('second');
31
- });
32
- });
@@ -1,41 +0,0 @@
1
-
2
- import {
3
- heronsFormula,
4
- heronsFormulaBI,
5
- } from '../src/herons-formula.mjs';
6
-
7
- describe('herons-formula', () => {
8
- it('Integer', () => {
9
- expect(heronsFormula(5, 12, 13)).toEqual(30);
10
- expect(heronsFormula(6, 8, 10)).toEqual(24);
11
- expect(heronsFormula(7, 15, 20)).toEqual(42);
12
- expect(heronsFormula(17, 17, 30)).toEqual(120);
13
- expect(heronsFormula(13, 37, 30)).toEqual(180);
14
- expect(heronsFormula(6, 25, 29)).toEqual(60);
15
- expect(heronsFormula(73, 9, 80)).toEqual(216);
16
- expect(heronsFormula(12, 35, 37)).toEqual(210);
17
- expect(heronsFormula(120, 109, 13)).toEqual(396);
18
- expect(heronsFormula(9, 10, 17)).toEqual(36);
19
- });
20
-
21
- it('Float', () => {
22
- expect(heronsFormula(2, 3, 4)).toEqual(2.9047375096555625);
23
- expect(heronsFormula(7, 10, 12)).toEqual(34.977671449083054);
24
- expect(heronsFormula(6, 11, 12)).toEqual(32.839572165300815);
25
- expect(heronsFormula(25, 25, 45)).toEqual(245.1880655741629);
26
- expect(heronsFormula(10, 11, 18)).toEqual(48.59976851796724);
27
- });
28
-
29
- it('BigInt', () => {
30
- expect(heronsFormulaBI(5n, 12n, 13n)).toEqual(30n);
31
- expect(heronsFormulaBI(6n, 8n, 10n)).toEqual(24n);
32
- expect(heronsFormulaBI(7n, 15n, 20n)).toEqual(42n);
33
- expect(heronsFormulaBI(17n, 17n, 30n)).toEqual(120n);
34
- expect(heronsFormulaBI(13n, 37n, 30n)).toEqual(180n);
35
- expect(heronsFormulaBI(6n, 25n, 29n)).toEqual(60n);
36
- expect(heronsFormulaBI(73n, 9n, 80n)).toEqual(216n);
37
- expect(heronsFormulaBI(12n, 35n, 37n)).toEqual(210n);
38
- expect(heronsFormulaBI(120n, 109n, 13n)).toEqual(396n);
39
- expect(heronsFormulaBI(9n, 10n, 17n)).toEqual(36n);
40
- });
41
- });
@@ -1,33 +0,0 @@
1
-
2
- import { ListNode } from '../src/list-node.mjs';
3
-
4
- describe('ListNode', () => {
5
- it('constructor', () => {
6
- expect(new ListNode().val).toEqual(0);
7
- expect(new ListNode().next).toEqual(null);
8
- expect(new ListNode(123).val).toEqual(123);
9
- expect(new ListNode(123).next).toEqual(null);
10
- expect(new ListNode(123, 'next').next).toEqual('next');
11
- });
12
-
13
- it('toArr', () => {
14
- expect(new ListNode().toArr()).toEqual([0]);
15
- expect(new ListNode(123).toArr()).toEqual([123]);
16
- expect(new ListNode(1, new ListNode(2)).toArr()).toEqual([1, 2]);
17
- });
18
-
19
- it('fromArr', () => {
20
- expect(ListNode.fromArr([1, 2])).toEqual(new ListNode(1, new ListNode(2)));
21
- });
22
-
23
- it('both', () => {
24
- const arr = [1, 2, 3, 4, 5];
25
- expect(ListNode.fromArr(arr).toArr()).toEqual(arr);
26
- });
27
-
28
- it('cyclic reference', () => {
29
- const node = ListNode.fromArr([1, 2]);
30
- node.next.next = node;
31
- expect(() => node.toArr()).toThrow(new Error('Cyclic reference detected'));
32
- });
33
- });
@@ -1,8 +0,0 @@
1
-
2
- import * as all from '../main.mjs';
3
-
4
- describe('main', () => {
5
- it('default', () => {
6
- expect(all.default).toBeDefined();
7
- });
8
- });
@@ -1,19 +0,0 @@
1
-
2
- import {
3
- matrixAsArray,
4
- } from '../src/matrix.mjs';
5
-
6
- describe('matrix', () => {
7
- it('matrixAsArray', () => {
8
- const m = [
9
- [1, 2],
10
- [3, 4],
11
- ];
12
- const arr = matrixAsArray(m);
13
- expect(arr.length).toBe(4);
14
- expect(arr[0]).toBe(1);
15
- expect(arr[1]).toBe(2);
16
- expect(arr[2]).toBe(3);
17
- expect(arr[3]).toBe(4);
18
- });
19
- });
@@ -1,46 +0,0 @@
1
-
2
- import {
3
- memoize,
4
- } from '../src/memoize.mjs';
5
-
6
- describe('memoize', () => {
7
- it('called once', () => {
8
- let cnt = 0;
9
- const add = memoize((a, b) => {
10
- ++cnt;
11
- return a + b;
12
- });
13
-
14
- expect(add(1, 2)).toBe(3);
15
- expect(cnt).toBe(1);
16
- expect(add(1, 2)).toBe(3);
17
- expect(cnt).toBe(1);
18
-
19
- expect(add(2, 1)).toBe(3);
20
- expect(cnt).toBe(2);
21
- });
22
-
23
- it('variable args length', () => {
24
- const fn = memoize((...args) => args.length);
25
-
26
- expect(fn()).toBe(0);
27
- expect(fn(1)).toBe(1);
28
- expect(fn(1, 2)).toBe(2);
29
- expect(fn(1, 2, 3)).toBe(3);
30
- });
31
-
32
- it('different empty object arrays', () => {
33
- let cnt = 0;
34
- const fn = memoize(() => ++cnt);
35
- const emptyArr = [];
36
- fn(emptyArr);
37
- expect(cnt).toBe(1);
38
- fn(emptyArr);
39
- expect(cnt).toBe(1);
40
- fn([]);
41
- expect(cnt).toBe(2);
42
- fn([]);
43
- expect(cnt).toBe(3);
44
- });
45
- });
46
-
package/spec/mod.spec.mjs DELETED
@@ -1,39 +0,0 @@
1
-
2
- import {
3
- mod,
4
- modBI,
5
- } from '../src/mod.mjs';
6
-
7
- describe('mod', () => {
8
- it('mod', () => {
9
- expect(mod(4, 100)).toEqual(4);
10
- expect(mod(104, 100)).toEqual(4);
11
- expect(mod(4 - 100, 100)).toEqual(4);
12
- expect(mod(4 + 96, 100)).toEqual(0);
13
- expect(mod(-4 - 96, 100)).toEqual(0);
14
- });
15
-
16
- it('mod sign', () => {
17
- expect(mod(4, 100)).toEqual(4);
18
- expect(mod(-4, 100)).toEqual(96);
19
- expect(mod(4, -100)).toEqual(-96);
20
- expect(mod(-4, -100)).toEqual(-4);
21
- });
22
- });
23
-
24
- describe('mod BI', () => {
25
- it('mod', () => {
26
- expect(modBI(4n, 100n)).toEqual(4n);
27
- expect(modBI(104n, 100n)).toEqual(4n);
28
- expect(modBI(4n - 100n, 100n)).toEqual(4n);
29
- expect(modBI(4n + 96n, 100n)).toEqual(0n);
30
- expect(modBI(-4n - 96n, 100n)).toEqual(0n);
31
- });
32
-
33
- it('mod sign', () => {
34
- expect(modBI(4n, 100n)).toEqual(4n);
35
- expect(modBI(-4n, 100n)).toEqual(96n);
36
- expect(modBI(4n, -100n)).toEqual(-96n);
37
- expect(modBI(-4n, -100n)).toEqual(-4n);
38
- });
39
- });
package/spec/phi.spec.mjs DELETED
@@ -1,60 +0,0 @@
1
-
2
- import {
3
- phi,
4
- phiBI,
5
- } from '../src/phi.mjs';
6
-
7
- describe('phi', () => {
8
- it('Number x1', () => {
9
- expect(phi(1)).toBe(1);
10
- expect(phi(11)).toBe(10);
11
- expect(phi(21)).toBe(12);
12
- expect(phi(31)).toBe(30);
13
- expect(phi(41)).toBe(40);
14
- expect(phi(51)).toBe(32);
15
- expect(phi(61)).toBe(60);
16
- expect(phi(71)).toBe(70);
17
- expect(phi(81)).toBe(54);
18
- expect(phi(91)).toBe(72);
19
- });
20
-
21
- it('Number x5', () => {
22
- expect(phi(5)).toBe(4);
23
- expect(phi(15)).toBe(8);
24
- expect(phi(25)).toBe(20);
25
- expect(phi(35)).toBe(24);
26
- expect(phi(45)).toBe(24);
27
- expect(phi(55)).toBe(40);
28
- expect(phi(65)).toBe(48);
29
- expect(phi(75)).toBe(40);
30
- expect(phi(85)).toBe(64);
31
- expect(phi(95)).toBe(72);
32
- });
33
-
34
- it('BigInt x1', () => {
35
- expect(phiBI(1n)).toBe(1n);
36
- expect(phiBI(11n)).toBe(10n);
37
- expect(phiBI(21n)).toBe(12n);
38
- expect(phiBI(31n)).toBe(30n);
39
- expect(phiBI(41n)).toBe(40n);
40
- expect(phiBI(51n)).toBe(32n);
41
- expect(phiBI(61n)).toBe(60n);
42
- expect(phiBI(71n)).toBe(70n);
43
- expect(phiBI(81n)).toBe(54n);
44
- expect(phiBI(91n)).toBe(72n);
45
- });
46
-
47
- it('BigInt x5', () => {
48
- expect(phiBI(5n)).toBe(4n);
49
- expect(phiBI(15n)).toBe(8n);
50
- expect(phiBI(25n)).toBe(20n);
51
- expect(phiBI(35n)).toBe(24n);
52
- expect(phiBI(45n)).toBe(24n);
53
- expect(phiBI(55n)).toBe(40n);
54
- expect(phiBI(65n)).toBe(48n);
55
- expect(phiBI(75n)).toBe(40n);
56
- expect(phiBI(85n)).toBe(64n);
57
- expect(phiBI(95n)).toBe(72n);
58
- });
59
- });
60
-
@@ -1,21 +0,0 @@
1
-
2
- import {
3
- powMod,
4
- powModBI,
5
- } from '../src/pow-mod.mjs';
6
-
7
- describe('pow-mod', () => {
8
- it('powMod', () => {
9
- expect(powMod(2, 1)).toEqual(2);
10
- expect(powMod(2, 1, 10)).toEqual(2);
11
- expect(powMod(2, 1, 2)).toEqual(0);
12
- expect(powMod(2, 0, 2)).toEqual(1);
13
- });
14
-
15
- it('powModBI', () => {
16
- expect(powModBI(2n, 1n)).toEqual(2n);
17
- expect(powModBI(2n, 1n, 10n)).toEqual(2n);
18
- expect(powModBI(2n, 1n, 2n)).toEqual(0n);
19
- expect(powModBI(2n, 0n, 2n)).toEqual(1n);
20
- });
21
- });
@@ -1,19 +0,0 @@
1
-
2
- import {
3
- range2array,
4
- array2range,
5
- } from '../src/range-array.mjs';
6
-
7
- describe('range-array', () => {
8
- it('array2range', () => {
9
- expect(array2range([])).toEqual([]);
10
- expect(array2range([1,3,4,5,7,9,10])).toEqual([[1,1],[3,5],[7,7],[9,10]]);
11
- expect(array2range([10,12,13,14,15,16,17,20,22,23,27])).toEqual([[10,10],[12,17],[20,20],[22,23],[27,27]]);
12
- });
13
-
14
- it('range2array', () => {
15
- expect(range2array([])).toEqual([]);
16
- expect(range2array([[1],[3,5],[7],[9,10]])).toEqual([1,3,4,5,7,9,10]);
17
- expect(range2array([[10],[12,17],[20],[22,23],[27]])).toEqual([10,12,13,14,15,16,17,20,22,23,27]);
18
- });
19
- });
@@ -1,22 +0,0 @@
1
-
2
- import {
3
- squareRoot,
4
- squareRootBI,
5
- } from '../src/square-root.mjs';
6
-
7
- describe('square-root', () => {
8
- it('squareRootBI small', () => {
9
- expect(squareRootBI(0n)).toEqual(0n);
10
- expect(squareRootBI(1n)).toEqual(1n);
11
- expect(squareRootBI(2n)).toEqual(1n);
12
- expect(squareRootBI(3n)).toEqual(1n);
13
- expect(squareRootBI(4n)).toEqual(2n);
14
- });
15
-
16
- it('squareRootBI 0-1e3', () => {
17
- for (let i = 0; i < 1e3; ++i) {
18
- const sqf = BigInt(Math.floor(i ** 0.5));
19
- expect(squareRootBI(BigInt(i))).toEqual(sqf);
20
- }
21
- });
22
- });
@@ -1,16 +0,0 @@
1
- {
2
- "spec_dir": "spec",
3
- "spec_files": [
4
- "**/*[sS]pec.?(m)js"
5
- ],
6
- "helpers": [
7
- "helpers/**/*.?(m)js"
8
- ],
9
- "env": {
10
- "stopSpecOnExpectationFailure": false,
11
- "random": true
12
- },
13
- "client": {
14
- "captureConsole": false
15
- }
16
- }
@@ -1,15 +0,0 @@
1
-
2
- import {
3
- tonelliShanksBI,
4
- } from '../src/tonelli-shanks.mjs';
5
-
6
- describe('tonelli-shanks', () => {
7
- it('tonelliShanksBI', () => {
8
- const a = 8479994658316772151941616510097127087554541274812435112009425778595495359700244470400642403747058566807127814165396640215844192327900454116257979487432016769329970767046735091249898678088061634796559556704959846424131820416048436501387617211770124292793308079214153179977624440438616958575058361193975686620046439877308339989295604537867493683872778843921771307305602776398786978353866231661453376056771972069776398999013769588936194859344941268223184197231368887060609212875507518936172060702209557124430477137421847130682601666968691651447236917018634902407704797328509461854842432015009878011354022108661461024768n;
9
- const p = 30531851861994333252675935111487950694414332763909083514133769861350960895076504687261369815735742549428789138300843082086550059082835141454526618160634109969195486322015775943030060449557090064811940139431735209185996454739163555910726493597222646855506445602953689527405362207926990442391705014604777038685880527537489845359101552442292804398472642356609304810680731556542002301547846635101455995732584071355903010856718680732337369128498655255277003643669031694516851390505923416710601212618443109844041514942401969629158975457079026906304328749039997262960301209158175920051890620947063936347307238412281568760161n;
10
- const r = tonelliShanksBI(a, p);
11
-
12
- expect((r * r) % p).toEqual(a);
13
- });
14
-
15
- });