@gkucmierz/utils 1.12.1 → 1.14.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 ADDED
@@ -0,0 +1,58 @@
1
+
2
+ import {
3
+ bijective2num, bijective2numBI, num2bijective, num2bijectiveBI
4
+ } from './src/bijective-numeration.mjs'
5
+ import {
6
+ egcd
7
+ } from './src/egcd.mjs'
8
+ import {
9
+ factors, factorsBI
10
+ } from './src/factors.mjs'
11
+ import {
12
+ gcd, gcdBI
13
+ } from './src/gcd.mjs'
14
+ import {
15
+ getType
16
+ } from './src/get-type.mjs'
17
+ import {
18
+ heronsFormula, heronsFormulaBI
19
+ } from './src/herons-formula.mjs'
20
+ import {
21
+ lcm, lcmBI
22
+ } from './src/lcm.mjs'
23
+ import {
24
+ mod, modBI
25
+ } from './src/mod.mjs'
26
+ import {
27
+ phi, phiBI
28
+ } from './src/phi.mjs'
29
+ import {
30
+ powMod, powModBI
31
+ } from './src/pow-mod.mjs'
32
+ import {
33
+ array2range, range2array
34
+ } from './src/range-array.mjs'
35
+ import {
36
+ squareRoot, squareRootBI
37
+ } from './src/square-root.mjs'
38
+ import {
39
+ tonelliShanksBI
40
+ } from './src/tonelli-shanks.mjs'
41
+
42
+ export * from './src/bijective-numeration.mjs';
43
+ export * from './src/egcd.mjs';
44
+ export * from './src/factors.mjs';
45
+ export * from './src/gcd.mjs';
46
+ export * from './src/get-type.mjs';
47
+ export * from './src/herons-formula.mjs';
48
+ export * from './src/lcm.mjs';
49
+ export * from './src/mod.mjs';
50
+ export * from './src/phi.mjs';
51
+ export * from './src/pow-mod.mjs';
52
+ export * from './src/range-array.mjs';
53
+ export * from './src/square-root.mjs';
54
+ export * from './src/tonelli-shanks.mjs';
55
+
56
+ export default [
57
+ bijective2num, bijective2numBI, num2bijective, num2bijectiveBI, egcd, factors, factorsBI, gcd, gcdBI, getType, heronsFormula, heronsFormulaBI, lcm, lcmBI, mod, modBI, phi, phiBI, powMod, powModBI, array2range, range2array, squareRoot, squareRootBI, tonelliShanksBI
58
+ ];
package/package-lock.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@gkucmierz/utils",
3
- "version": "1.12.1",
3
+ "version": "1.14.0",
4
4
  "lockfileVersion": 2,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "@gkucmierz/utils",
9
- "version": "1.12.1",
9
+ "version": "1.14.0",
10
10
  "license": "MIT",
11
11
  "devDependencies": {
12
12
  "husky": "^8.0.1",
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "@gkucmierz/utils",
3
- "version": "1.12.1",
3
+ "version": "1.14.0",
4
4
  "description": "Usefull functions for solving programming tasks",
5
- "main": "src/main.mjs",
5
+ "main": "main.mjs",
6
6
  "scripts": {
7
7
  "test": "jasmine",
8
- "watch": "nodemon --exec 'npm test'"
8
+ "watch": "nodemon --exec 'npm test'",
9
+ "main": "node scripts/generate_main.mjs"
9
10
  },
10
11
  "repository": {
11
12
  "type": "git",
@@ -0,0 +1,43 @@
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
+
@@ -0,0 +1,41 @@
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
+ });
@@ -0,0 +1,8 @@
1
+
2
+ import * as all from '../main.mjs';
3
+
4
+ describe('main', () => {
5
+ it('default', () => {
6
+ expect(all.default).toBeDefined();
7
+ });
8
+ });
@@ -0,0 +1,13 @@
1
+
2
+ import {
3
+ squareRootBI,
4
+ } from './square-root.mjs';
5
+
6
+ // calculate triangle area given 3 sides
7
+
8
+ const getHeronsFormula = (four, sq) => (a, b, c) => {
9
+ return sq((a + b + c) * (-a + b + c) * (a - b + c) * (a + b - c)) / four;
10
+ };
11
+
12
+ export const heronsFormula = getHeronsFormula(4, n => n ** 0.5);
13
+ export const heronsFormulaBI = getHeronsFormula(4n, squareRootBI);
@@ -0,0 +1,15 @@
1
+
2
+
3
+ export const squareRoot = n => n ** 0.5;
4
+
5
+ // Newrton's formula
6
+ export const squareRootBI = n => {
7
+ let res = BigInt(n.toString().substr(0, n.toString().length / 2));
8
+ let last;
9
+ while (res * res - n !== 0n) {
10
+ last = res;
11
+ res = (res + n / res) / 2n;
12
+ if (res === last) return res;
13
+ }
14
+ return res;
15
+ };
package/src/main.mjs DELETED
@@ -1,25 +0,0 @@
1
-
2
- import fs from 'fs';
3
- import path from 'path';
4
- import { fileURLToPath } from 'url';
5
-
6
- const __filename = fileURLToPath(import.meta.url);
7
- const __dirname = path.dirname(__filename);
8
-
9
- const files = fs.readdirSync(__dirname);
10
-
11
- const utilsFiles = files
12
- .filter(name => name.match(/\.mjs$/))
13
- .filter(name => name !== 'main.mjs');
14
-
15
- const methods = {};
16
-
17
- for (const file of utilsFiles) {
18
- const obj = await import(`./${file}`);
19
- Object.keys(obj).map(key => {
20
- if (key in methods) throw Error('Duplicate method name');
21
- methods[key] = obj[key];
22
- });
23
- };
24
-
25
- export default methods;