@gkucmierz/utils 1.1.6 → 1.2.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/package-lock.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gkucmierz/utils",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"lockfileVersion": 2,
|
|
5
5
|
"requires": true,
|
|
6
6
|
"packages": {
|
|
7
7
|
"": {
|
|
8
8
|
"name": "@gkucmierz/utils",
|
|
9
|
-
"version": "1.
|
|
9
|
+
"version": "1.2.0",
|
|
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
|
+
});
|
package/spec/get-type.spec.mjs
CHANGED
|
@@ -2,15 +2,74 @@
|
|
|
2
2
|
import { getType } from '../src/get-type.mjs';
|
|
3
3
|
|
|
4
4
|
describe('get-type', () => {
|
|
5
|
-
|
|
5
|
+
|
|
6
|
+
it('should undefined', () => {
|
|
6
7
|
expect(getType(undefined)).toEqual('undefined');
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it('should null', () => {
|
|
7
11
|
expect(getType(null)).toEqual('null');
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it('should boolean', () => {
|
|
8
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', () => {
|
|
9
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', () => {
|
|
10
29
|
expect(getType(1n)).toEqual('bigint');
|
|
11
|
-
|
|
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', () => {
|
|
12
38
|
expect(getType(Symbol())).toEqual('symbol');
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('should function', () => {
|
|
13
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', () => {
|
|
14
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');
|
|
15
74
|
});
|
|
16
75
|
});
|
|
@@ -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
|
+
};
|