Skip to content

Commit 42d9e8c

Browse files
committed
[Refactor] ES2020+: modulo: support BigInt inputs
1 parent 9c40412 commit 42d9e8c

8 files changed

Lines changed: 82 additions & 7 deletions

File tree

.gitattributes

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,6 @@
734734
/2020/YearFromTime.js spackled linguist-generated=true
735735
/2020/max.js spackled linguist-generated=true
736736
/2020/min.js spackled linguist-generated=true
737-
/2020/modulo.js spackled linguist-generated=true
738737
/2020/msFromTime.js spackled linguist-generated=true
739738
/2020/thisBooleanValue.js spackled linguist-generated=true
740739
/2020/thisNumberValue.js spackled linguist-generated=true

2020/modulo.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,16 @@
22

33
var mod = require('../helpers/mod');
44

5-
// https://262.ecma-international.org/5.1/#sec-5.2
5+
// https://262.ecma-international.org/11.0/#eqn-modulo
66

77
module.exports = function modulo(x, y) {
8+
if (typeof x === 'bigint') {
9+
if (typeof y !== 'bigint') {
10+
throw new TypeError('`x` and `y` must both be BigInts or both be Numbers');
11+
}
12+
var r = x % y;
13+
if (r === BigInt(0)) { return r; }
14+
return (r < BigInt(0)) === (y < BigInt(0)) ? r : r + y;
15+
}
816
return mod(x, y);
917
};

2021/modulo.js

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

2022/modulo.js

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

2023/modulo.js

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

2024/modulo.js

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

2025/modulo.js

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/methods/modulo.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
var esV = require('../helpers/v');
4+
35
module.exports = function (t, year, modulo) {
46
t.ok(year >= 5, 'ES5+');
57

@@ -8,4 +10,30 @@ module.exports = function (t, year, modulo) {
810

911
t.equal(-3 % 2, -1, '-3 % 2 is -1');
1012
t.equal(modulo(-3, 2), 1, '-3 mod 2 is +1');
13+
14+
if (year >= 2020) {
15+
t.test('BigInts', { skip: !esV.hasBigInts }, function (st) {
16+
st.equal(modulo(BigInt(3), BigInt(2)), BigInt(1), '+3n mod +2n is +1n');
17+
st.equal(modulo(BigInt(-3), BigInt(2)), BigInt(1), '-3n mod +2n is +1n');
18+
st.equal(modulo(BigInt(3), BigInt(-2)), BigInt(-1), '+3n mod -2n is -1n');
19+
st.equal(modulo(BigInt(-3), BigInt(-2)), BigInt(-1), '-3n mod -2n is -1n');
20+
21+
st.equal(modulo(BigInt(6), BigInt(3)), BigInt(0), '+6n mod +3n is 0n');
22+
st.equal(modulo(BigInt(0), BigInt(3)), BigInt(0), '0n mod +3n is 0n');
23+
st.equal(modulo(BigInt(0), BigInt(-3)), BigInt(0), '0n mod -3n is 0n');
24+
25+
st['throws'](
26+
function () { modulo(BigInt(1), 2); },
27+
TypeError,
28+
'mixing bigint and number throws'
29+
);
30+
st['throws'](
31+
function () { modulo(1, BigInt(2)); },
32+
TypeError,
33+
'mixing number and bigint throws'
34+
);
35+
36+
st.end();
37+
});
38+
}
1139
};

0 commit comments

Comments
 (0)