@gkucmierz/utils 1.8.0 → 1.9.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.8.0",
3
+ "version": "1.9.0",
4
4
  "lockfileVersion": 2,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "@gkucmierz/utils",
9
- "version": "1.8.0",
9
+ "version": "1.9.0",
10
10
  "license": "MIT",
11
11
  "devDependencies": {
12
12
  "husky": "^8.0.1",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gkucmierz/utils",
3
- "version": "1.8.0",
3
+ "version": "1.9.0",
4
4
  "description": "Usefull functions for solving programming tasks",
5
5
  "main": "src/main.mjs",
6
6
  "scripts": {
@@ -0,0 +1,35 @@
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
+ });
13
+
14
+ it('mod sign', () => {
15
+ expect(mod(4, 100)).toEqual(4);
16
+ expect(mod(-4, 100)).toEqual(96);
17
+ expect(mod(4, -100)).toEqual(-96);
18
+ expect(mod(-4, -100)).toEqual(-4);
19
+ });
20
+ });
21
+
22
+ describe('mod BI', () => {
23
+ it('mod', () => {
24
+ expect(modBI(4n, 100n)).toEqual(4n);
25
+ expect(modBI(104n, 100n)).toEqual(4n);
26
+ expect(modBI(4n - 100n, 100n)).toEqual(4n);
27
+ });
28
+
29
+ it('mod sign', () => {
30
+ expect(modBI(4n, 100n)).toEqual(4n);
31
+ expect(modBI(-4n, 100n)).toEqual(96n);
32
+ expect(modBI(4n, -100n)).toEqual(-96n);
33
+ expect(modBI(-4n, -100n)).toEqual(-4n);
34
+ });
35
+ });
package/src/mod.mjs ADDED
@@ -0,0 +1,14 @@
1
+
2
+ // python like mod implementation
3
+
4
+ const getMod = ZERO => {
5
+ return (dividend, divisor) => {
6
+ if ((dividend < ZERO) ^ (divisor < ZERO)) {
7
+ return divisor - (-dividend % divisor);
8
+ }
9
+ return dividend % divisor;
10
+ };
11
+ };
12
+
13
+ export const mod = getMod(0);
14
+ export const modBI = getMod(0n);