@gkucmierz/utils 1.1.7 → 1.2.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/package-lock.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gkucmierz/utils",
|
|
3
|
-
"version": "1.1
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"lockfileVersion": 2,
|
|
5
5
|
"requires": true,
|
|
6
6
|
"packages": {
|
|
7
7
|
"": {
|
|
8
8
|
"name": "@gkucmierz/utils",
|
|
9
|
-
"version": "1.1
|
|
9
|
+
"version": "1.2.1",
|
|
10
10
|
"license": "MIT",
|
|
11
11
|
"devDependencies": {
|
|
12
12
|
"husky": "^8.0.1",
|
package/package.json
CHANGED
|
@@ -0,0 +1,105 @@
|
|
|
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
|
+
});
|
|
57
|
+
|
|
58
|
+
describe('bijective-numeration BigInt', () => {
|
|
59
|
+
const alpha = 'abcdefghijklmnopqrstuvwxyz';
|
|
60
|
+
|
|
61
|
+
const map = [
|
|
62
|
+
[0n, ''],
|
|
63
|
+
[1n, '1'],
|
|
64
|
+
[2n, '2'],
|
|
65
|
+
[3n, '11'],
|
|
66
|
+
[4n, '12'],
|
|
67
|
+
[5n, '21'],
|
|
68
|
+
[6n, '22'],
|
|
69
|
+
[7n, '111'],
|
|
70
|
+
];
|
|
71
|
+
|
|
72
|
+
const mapAlpha = [
|
|
73
|
+
[0n, ''],
|
|
74
|
+
[1n, 'a'],
|
|
75
|
+
[26n, 'z'],
|
|
76
|
+
[26n + 1n, 'aa'],
|
|
77
|
+
[26n * 26n + 26n, 'zz'],
|
|
78
|
+
[26n * 26n + 26n + 1n, 'aaa'],
|
|
79
|
+
];
|
|
80
|
+
|
|
81
|
+
it('num2bijectiveBI', () => {
|
|
82
|
+
map.map(([num, bij]) => {
|
|
83
|
+
expect(num2bijectiveBI(num)).toEqual(bij);
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('num2bijectiveBI alpha', () => {
|
|
88
|
+
mapAlpha.map(([num, bij]) => {
|
|
89
|
+
expect(num2bijectiveBI(num, alpha)).toEqual(bij);
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('bijective2numBI', () => {
|
|
94
|
+
map.map(([num, bij]) => {
|
|
95
|
+
expect(bijective2numBI(bij)).toEqual(num);
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it('bijective2numBI alpha', () => {
|
|
100
|
+
mapAlpha.map(([num, bij]) => {
|
|
101
|
+
expect(bijective2numBI(bij, alpha)).toEqual(num);
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
});
|
|
@@ -5,13 +5,13 @@ import {
|
|
|
5
5
|
} from '../src/range-array.mjs';
|
|
6
6
|
|
|
7
7
|
describe('range-array', () => {
|
|
8
|
-
it('array2range',
|
|
8
|
+
it('array2range', () => {
|
|
9
9
|
expect(array2range([])).toEqual([]);
|
|
10
10
|
expect(array2range([1,3,4,5,7,9,10])).toEqual([[1,1],[3,5],[7,7],[9,10]]);
|
|
11
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
12
|
});
|
|
13
13
|
|
|
14
|
-
it('range2array',
|
|
14
|
+
it('range2array', () => {
|
|
15
15
|
expect(range2array([])).toEqual([]);
|
|
16
16
|
expect(range2array([[1],[3,5],[7],[9,10]])).toEqual([1,3,4,5,7,9,10]);
|
|
17
17
|
expect(range2array([[10],[12,17],[20],[22,23],[27]])).toEqual([10,12,13,14,15,16,17,20,22,23,27]);
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
|
|
2
|
+
export const num2bijective = (num, alpha = '12') => {
|
|
3
|
+
const len = alpha.length;
|
|
4
|
+
let c = 0;
|
|
5
|
+
let x = 1;
|
|
6
|
+
while (num >= x) {
|
|
7
|
+
c++;
|
|
8
|
+
num -= x;
|
|
9
|
+
x *= len;
|
|
10
|
+
}
|
|
11
|
+
const res = [];
|
|
12
|
+
for (let i = 0; i < c; i++) {
|
|
13
|
+
const rem = num % len;
|
|
14
|
+
res.unshift(alpha.charAt(num % len));
|
|
15
|
+
num = (num - rem) / len;
|
|
16
|
+
}
|
|
17
|
+
return res.join('');
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export const bijective2num = (str, alpha = '12') => {
|
|
21
|
+
const map = new Map([...alpha].map((c, i) => [c, i]));
|
|
22
|
+
let res = 0;
|
|
23
|
+
const len = alpha.length;
|
|
24
|
+
return [...str].reduce((res, c) => {
|
|
25
|
+
return res * len + map.get(c) + 1;
|
|
26
|
+
}, 0);
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export const num2bijectiveBI = (num, alpha = '12') => {
|
|
30
|
+
const len = BigInt(alpha.length);
|
|
31
|
+
let c = 0n;
|
|
32
|
+
let x = 1n;
|
|
33
|
+
while (num >= x) {
|
|
34
|
+
c++;
|
|
35
|
+
num -= x;
|
|
36
|
+
x *= len;
|
|
37
|
+
}
|
|
38
|
+
const res = [];
|
|
39
|
+
for (let i = 0n; i < c; i++) {
|
|
40
|
+
const rem = num % len;
|
|
41
|
+
res.unshift(alpha.charAt(Number(num % len)));
|
|
42
|
+
num = (num - rem) / len;
|
|
43
|
+
}
|
|
44
|
+
return res.join('');
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export const bijective2numBI = (str, alpha = '12') => {
|
|
48
|
+
const map = new Map([...alpha].map((c, i) => [c, BigInt(i)]));
|
|
49
|
+
let res = 0n;
|
|
50
|
+
const len = BigInt(alpha.length);
|
|
51
|
+
return [...str].reduce((res, c) => {
|
|
52
|
+
return res * len + map.get(c) + 1n;
|
|
53
|
+
}, 0n);
|
|
54
|
+
};
|