1 | 'use strict';
|
---|
2 |
|
---|
3 | var GetIntrinsic = require('get-intrinsic');
|
---|
4 |
|
---|
5 | var $BigInt = GetIntrinsic('%BigInt%', true);
|
---|
6 | var $Number = GetIntrinsic('%Number%');
|
---|
7 | var $TypeError = require('es-errors/type');
|
---|
8 | var $SyntaxError = require('es-errors/syntax');
|
---|
9 |
|
---|
10 | var StringToBigInt = require('./StringToBigInt');
|
---|
11 | var ToPrimitive = require('./ToPrimitive');
|
---|
12 |
|
---|
13 | // https://262.ecma-international.org/13.0/#sec-tobigint
|
---|
14 |
|
---|
15 | module.exports = function ToBigInt(argument) {
|
---|
16 | if (!$BigInt) {
|
---|
17 | throw new $SyntaxError('BigInts are not supported in this environment');
|
---|
18 | }
|
---|
19 |
|
---|
20 | var prim = ToPrimitive(argument, $Number);
|
---|
21 |
|
---|
22 | if (prim == null) {
|
---|
23 | throw new $TypeError('Cannot convert null or undefined to a BigInt');
|
---|
24 | }
|
---|
25 |
|
---|
26 | if (typeof prim === 'boolean') {
|
---|
27 | return prim ? $BigInt(1) : $BigInt(0);
|
---|
28 | }
|
---|
29 |
|
---|
30 | if (typeof prim === 'number') {
|
---|
31 | throw new $TypeError('Cannot convert a Number value to a BigInt');
|
---|
32 | }
|
---|
33 |
|
---|
34 | if (typeof prim === 'string') {
|
---|
35 | var n = StringToBigInt(prim);
|
---|
36 | if (typeof n === 'undefined') {
|
---|
37 | throw new $TypeError('Failed to parse String to BigInt');
|
---|
38 | }
|
---|
39 | return n;
|
---|
40 | }
|
---|
41 |
|
---|
42 | if (typeof prim === 'symbol') {
|
---|
43 | throw new $TypeError('Cannot convert a Symbol value to a BigInt');
|
---|
44 | }
|
---|
45 |
|
---|
46 | if (typeof prim !== 'bigint') {
|
---|
47 | throw new $SyntaxError('Assertion failed: unknown primitive type');
|
---|
48 | }
|
---|
49 |
|
---|
50 | return prim;
|
---|
51 | };
|
---|