Skip to content

Commit 9f641ed

Browse files
committed
Add support for native bigints to type declarations
1 parent 825bdbd commit 9f641ed

File tree

5 files changed

+44
-28
lines changed

5 files changed

+44
-28
lines changed

.travis.yml

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
language: node_js
22
node_js:
3-
- "9"
4-
- "8"
5-
- "7"
6-
- "6"
7-
- "5"
8-
- "4"
3+
- "12"
4+
- "11"
5+
- "10"
96
script:
107
- npm test
118
- cat ./coverage/lcov.info | ./node_modules/.bin/coveralls

BigInteger.d.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export as namespace bigInt;
88
declare var bigInt: bigInt.BigIntegerStatic;
99

1010
declare namespace bigInt {
11-
type BigNumber = number | string | BigInteger;
11+
type BigNumber = number | bigint | string | BigInteger;
1212

1313
interface BigIntegerStatic {
1414
/**
@@ -21,6 +21,11 @@ declare namespace bigInt {
2121
*/
2222
(number: number): BigInteger;
2323

24+
/**
25+
* Parse a Javascript native bigint into a bigInt.
26+
*/
27+
(number: bigint): BigInteger;
28+
2429
/**
2530
* Parse a string into a bigInt.
2631
* Default base is 10.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"karma-jasmine": "^0.3.6",
3838
"karma-phantomjs-launcher": "^1.0.4",
3939
"lodash": "^4.17.11",
40-
"typescript": "^2.9.2",
40+
"typescript": "^3.6.3",
4141
"uglifyjs": "^2.4.10"
4242
},
4343
"license": "Unlicense",

spec/tsDefinitions.ts

+32-19
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ if (removedInstanceFns.length) {
106106
// constructor tests
107107
const noArgument = bigInt();
108108
const numberArgument = bigInt(93);
109+
const nativeBigintArgument = bigInt(93n);
109110
const stringArgument = bigInt("75643564363473453456342378564387956906736546456235345");
110111
const baseArgumentInt = bigInt("101010", 2);
111112
const baseArgumentStr = bigInt("101010", "2");
@@ -131,62 +132,73 @@ isBigInteger = bigInt[0];
131132
isBigInteger = bigInt[999];
132133

133134
isBigInteger = bigInt.fromArray([1, 2, 3]);
135+
isBigInteger = bigInt.fromArray([1n, 2n, 3n]);
134136
isBigInteger = bigInt.fromArray(['1', '2', '3']);
135137
isBigInteger = bigInt.fromArray([bigInt.one, bigInt.zero, bigInt(9)], 10, true);
136138

137139
isBigInteger = bigInt.gcd(0, 1);
138140
isBoolean = bigInt.isInstance(x);
139141
isBigInteger = bigInt.lcm(0, 1);
140142
isBigInteger = bigInt.max(0, 1);
141-
isBigInteger = bigInt.gcd(0, 1);
143+
isBigInteger = bigInt.min(0, 1);
142144
isBigInteger = bigInt.randBetween(0, 1);
143145

144146
// Instance methods
145147
isBigInteger = x.abs();
146-
isBigInteger = x.add(0).add(x).add("100");
147-
isBigInteger = x.and(0).and(x).and("100");
148+
isBigInteger = x.add(0).add(x).add("100").add(100n);
149+
isBigInteger = x.and(0).and(x).and("100").and(100n);
148150

149151
isNumber = x.compare(0);
150152
isNumber = x.compare(x);
151153
isNumber = x.compare("100");
154+
isNumber = x.compare(100n);
152155

153156
isNumber = x.compareAbs(0);
154157
isNumber = x.compareAbs(x);
155158
isNumber = x.compareAbs("100");
159+
isNumber = x.compareAbs(100n);
156160

157161
isNumber = x.compareTo(0);
158162
isNumber = x.compareTo(x);
159163
isNumber = x.compareTo("100");
164+
isNumber = x.compareTo(100n);
160165

161-
isBigInteger = x.divide(10).divide(x).divide('10');
166+
isBigInteger = x.divide(10).divide(x).divide('10').divide(10n);
162167

163168
isDivmod = x.divmod(10);
164169
isDivmod = x.divmod(x);
165170
isDivmod = x.divmod("100");
171+
isDivmod = x.divmod(100n);
166172

167173
isBoolean = x.eq(0);
168174
isBoolean = x.eq(x);
169175
isBoolean = x.eq("100");
176+
isBoolean = x.eq(100n);
170177

171178
isBoolean = x.equals(0);
172179
isBoolean = x.equals(x);
173180
isBoolean = x.equals("100");
181+
isBoolean = x.equals(100n);
174182

175183
isBoolean = x.geq(0);
176184
isBoolean = x.geq(x);
177185
isBoolean = x.geq("100");
186+
isBoolean = x.geq(100n);
178187

179188
isBoolean = x.greater(0);
180189
isBoolean = x.greater(x);
181190
isBoolean = x.greater("100");
191+
isBoolean = x.greater(100n);
182192

183193
isBoolean = x.greaterOrEquals(0);
184194
isBoolean = x.greaterOrEquals(x);
185195
isBoolean = x.greaterOrEquals("100");
196+
isBoolean = x.greaterOrEquals(100n);
186197

187198
isBoolean = x.gt(0);
188199
isBoolean = x.gt(x);
189200
isBoolean = x.gt("100");
201+
isBoolean = x.gt(100n);
190202

191203
isBoolean = x.isDivisibleBy(x);
192204
isBoolean = x.isEven();
@@ -204,11 +216,11 @@ isBoolean = x.leq(x);
204216
isBoolean = x.lesser(0);
205217
isBoolean = x.lesserOrEquals(0);
206218
isBoolean = x.lt(0);
207-
isBigInteger = x.minus(0).minus(x).minus('0');
208-
isBigInteger = x.mod(10).mod(x).mod('10');
209-
isBigInteger = bigInt(3).modInv(11);
210-
isBigInteger = x.modPow(10, 2).modPow(x, x).modPow('10', '2');
211-
isBigInteger = x.multiply(0).multiply(x).multiply('0');
219+
isBigInteger = x.minus(0).minus(x).minus('0').minus(0n);
220+
isBigInteger = x.mod(10).mod(x).mod('10').mod(10n);
221+
isBigInteger = bigInt(3).modInv(11).modInv(11n);
222+
isBigInteger = x.modPow(10, 2).modPow(x, x).modPow('10', '2').modPow(10n, 2n);
223+
isBigInteger = x.multiply(0).multiply(x).multiply('0').multiply(0n);
212224
isBigInteger = x.negate();
213225
isBoolean = x.neq(x);
214226
isBigInteger = x.next();
@@ -217,18 +229,19 @@ isBigInteger = x.not();
217229
isBoolean = x.notEquals(0);
218230
isBoolean = x.notEquals(x);
219231
isBoolean = x.notEquals("100");
232+
isBoolean = x.notEquals(100n);
220233

221-
isBigInteger = x.or(10).or(x).or('10');
222-
isBigInteger = x.over(10).over(x).over('10');
223-
isBigInteger = x.plus(0).plus(x).plus('0');
224-
isBigInteger = x.pow(0).pow(x).pow('0');
234+
isBigInteger = x.or(10).or(x).or('10').or(10n);
235+
isBigInteger = x.over(10).over(x).over('10').over(10n);
236+
isBigInteger = x.plus(0).plus(x).plus('0').plus(0n);
237+
isBigInteger = x.pow(0).pow(x).pow('0').pow(0n);
225238
isBigInteger = x.prev();
226-
isBigInteger = x.remainder(10).remainder(x).remainder('10');
227-
isBigInteger = x.shiftLeft(0).shiftLeft('0');
228-
isBigInteger = x.shiftRight(0).shiftRight('0');
239+
isBigInteger = x.remainder(10).remainder(x).remainder('10').remainder(10n);
240+
isBigInteger = x.shiftLeft(0).shiftLeft('0').shiftLeft(0n);
241+
isBigInteger = x.shiftRight(0).shiftRight('0').shiftRight(0n);
229242
isBigInteger = x.square();
230-
isBigInteger = x.subtract(0).subtract(x).subtract('0');
231-
isBigInteger = x.times(0).times(x).times('0');
243+
isBigInteger = x.subtract(0).subtract(x).subtract('0').subtract(0n);
244+
isBigInteger = x.times(0).times(x).times('0').times(0n);
232245
isNumber = x.toJSNumber();
233246

234247
isBaseArray = x.toArray(10);
@@ -239,4 +252,4 @@ isString = x.toString(36);
239252
isString = x.toJSON();
240253

241254
isNumber = x.valueOf();
242-
isBigInteger = x.xor(0).xor(x).xor('0');
255+
isBigInteger = x.xor(0).xor(x).xor('0').xor(0n);

tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"compilerOptions": {
3+
"target": "esnext",
34
"module": "commonjs",
45
"lib": [
56
"es6"
@@ -22,4 +23,4 @@
2223
"BigInteger.d.ts",
2324
"spec/tsDefinitions.ts"
2425
]
25-
}
26+
}

0 commit comments

Comments
 (0)