@gkucmierz/utils 1.28.2 → 1.28.3
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 +2 -2
- package/package.json +1 -1
- package/spec/memoize.spec.mjs +14 -0
- package/src/memoize.mjs +7 -0
- package/src/mod.mjs +19 -2
- package/src/pow-mod.mjs +16 -0
package/package-lock.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gkucmierz/utils",
|
|
3
|
-
"version": "1.28.
|
|
3
|
+
"version": "1.28.3",
|
|
4
4
|
"lockfileVersion": 2,
|
|
5
5
|
"requires": true,
|
|
6
6
|
"packages": {
|
|
7
7
|
"": {
|
|
8
8
|
"name": "@gkucmierz/utils",
|
|
9
|
-
"version": "1.28.
|
|
9
|
+
"version": "1.28.3",
|
|
10
10
|
"license": "MIT",
|
|
11
11
|
"devDependencies": {
|
|
12
12
|
"docdash": "^2.0.2",
|
package/package.json
CHANGED
package/spec/memoize.spec.mjs
CHANGED
|
@@ -28,5 +28,19 @@ describe('memoize', () => {
|
|
|
28
28
|
expect(fn(1, 2)).toBe(2);
|
|
29
29
|
expect(fn(1, 2, 3)).toBe(3);
|
|
30
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
|
+
});
|
|
31
45
|
});
|
|
32
46
|
|
package/src/memoize.mjs
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
1
|
|
|
2
|
+
/**
|
|
3
|
+
* Memoize function, that builds tree structure for arguments
|
|
4
|
+
* and compares arguments by reference
|
|
5
|
+
* @method
|
|
6
|
+
* @param {Function} Function to memoize
|
|
7
|
+
* @return {Function} Memoized function
|
|
8
|
+
*/
|
|
2
9
|
export const memoize = fn => {
|
|
3
10
|
const maps = [];
|
|
4
11
|
|
package/src/mod.mjs
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
|
|
2
|
-
// python like mod implementation
|
|
3
|
-
|
|
4
2
|
const getMod = ZERO => {
|
|
5
3
|
return (dividend, divisor) => {
|
|
6
4
|
if ((dividend < ZERO) ^ (divisor < ZERO)) {
|
|
@@ -11,5 +9,24 @@ const getMod = ZERO => {
|
|
|
11
9
|
};
|
|
12
10
|
};
|
|
13
11
|
|
|
12
|
+
/**
|
|
13
|
+
* Python like modulo implementation.
|
|
14
|
+
* It behaves different than JavaScript %
|
|
15
|
+
* with negative values
|
|
16
|
+
* @method
|
|
17
|
+
* @param {Number} Dividend
|
|
18
|
+
* @param {Number} Divisor
|
|
19
|
+
* @return {Number} Modulus
|
|
20
|
+
*/
|
|
14
21
|
export const mod = getMod(0);
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Python like modulo implementation.
|
|
25
|
+
* It behaves different than JavaScript %
|
|
26
|
+
* with negative values
|
|
27
|
+
* @method
|
|
28
|
+
* @param {BigInt} Dividend
|
|
29
|
+
* @param {BigInt} Divisor
|
|
30
|
+
* @return {BigInt} Modulus
|
|
31
|
+
*/
|
|
15
32
|
export const modBI = getMod(0n);
|
package/src/pow-mod.mjs
CHANGED
|
@@ -18,6 +18,22 @@ const getPowMod = (ZERO, ONE, TWO, floor) => {
|
|
|
18
18
|
};
|
|
19
19
|
};
|
|
20
20
|
|
|
21
|
+
/**
|
|
22
|
+
* Power like python implementation with optional modulus
|
|
23
|
+
* @method
|
|
24
|
+
* @param {Number} Base
|
|
25
|
+
* @param {Number} Exponent
|
|
26
|
+
* @param [Number] Modulus
|
|
27
|
+
* @return {Number} Power
|
|
28
|
+
*/
|
|
21
29
|
export const powMod = getPowMod(0, 1, 2, n => Math.floor(n));
|
|
22
30
|
|
|
31
|
+
/**
|
|
32
|
+
* Power like python implementation with optional modulus
|
|
33
|
+
* @method
|
|
34
|
+
* @param {BigInt} Base
|
|
35
|
+
* @param {BigInt} Exponent
|
|
36
|
+
* @param [BigInt] Modulus
|
|
37
|
+
* @return {BigInt} Power
|
|
38
|
+
*/
|
|
23
39
|
export const powModBI = getPowMod(0n, 1n, 2n, n => n);
|