1 | import _curry2 from "./internal/_curry2.js";
|
---|
2 | import _isInteger from "./internal/_isInteger.js";
|
---|
3 | /**
|
---|
4 | * `mathMod` behaves like the modulo operator should mathematically, unlike the
|
---|
5 | * `%` operator (and by extension, [`R.modulo`](#modulo)). So while
|
---|
6 | * `-17 % 5` is `-2`, `mathMod(-17, 5)` is `3`. `mathMod` requires Integer
|
---|
7 | * arguments, and returns NaN when the modulus is zero or negative.
|
---|
8 | *
|
---|
9 | * @func
|
---|
10 | * @memberOf R
|
---|
11 | * @since v0.3.0
|
---|
12 | * @category Math
|
---|
13 | * @sig Number -> Number -> Number
|
---|
14 | * @param {Number} m The dividend.
|
---|
15 | * @param {Number} p the modulus.
|
---|
16 | * @return {Number} The result of `b mod a`.
|
---|
17 | * @see R.modulo
|
---|
18 | * @example
|
---|
19 | *
|
---|
20 | * R.mathMod(-17, 5); //=> 3
|
---|
21 | * R.mathMod(17, 5); //=> 2
|
---|
22 | * R.mathMod(17, -5); //=> NaN
|
---|
23 | * R.mathMod(17, 0); //=> NaN
|
---|
24 | * R.mathMod(17.2, 5); //=> NaN
|
---|
25 | * R.mathMod(17, 5.3); //=> NaN
|
---|
26 | *
|
---|
27 | * const clock = R.mathMod(R.__, 12);
|
---|
28 | * clock(15); //=> 3
|
---|
29 | * clock(24); //=> 0
|
---|
30 | *
|
---|
31 | * const seventeenMod = R.mathMod(17);
|
---|
32 | * seventeenMod(3); //=> 2
|
---|
33 | * seventeenMod(4); //=> 1
|
---|
34 | * seventeenMod(10); //=> 7
|
---|
35 | */
|
---|
36 |
|
---|
37 | var mathMod =
|
---|
38 | /*#__PURE__*/
|
---|
39 | _curry2(function mathMod(m, p) {
|
---|
40 | if (!_isInteger(m)) {
|
---|
41 | return NaN;
|
---|
42 | }
|
---|
43 |
|
---|
44 | if (!_isInteger(p) || p < 1) {
|
---|
45 | return NaN;
|
---|
46 | }
|
---|
47 |
|
---|
48 | return (m % p + p) % p;
|
---|
49 | });
|
---|
50 |
|
---|
51 | export default mathMod; |
---|