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 | var isNaN = require('../helpers/isNaN');
|
---|
14 |
|
---|
15 | // https://262.ecma-international.org/11.0/#sec-tobigint
|
---|
16 |
|
---|
17 | module.exports = function ToBigInt(argument) {
|
---|
18 | if (!$BigInt) {
|
---|
19 | throw new $SyntaxError('BigInts are not supported in this environment');
|
---|
20 | }
|
---|
21 |
|
---|
22 | var prim = ToPrimitive(argument, $Number);
|
---|
23 |
|
---|
24 | if (prim == null) {
|
---|
25 | throw new $TypeError('Cannot convert null or undefined to a BigInt');
|
---|
26 | }
|
---|
27 |
|
---|
28 | if (typeof prim === 'boolean') {
|
---|
29 | return prim ? $BigInt(1) : $BigInt(0);
|
---|
30 | }
|
---|
31 |
|
---|
32 | if (typeof prim === 'number') {
|
---|
33 | throw new $TypeError('Cannot convert a Number value to a BigInt');
|
---|
34 | }
|
---|
35 |
|
---|
36 | if (typeof prim === 'string') {
|
---|
37 | var n = StringToBigInt(prim);
|
---|
38 | if (isNaN(n)) {
|
---|
39 | throw new $TypeError('Failed to parse String to BigInt');
|
---|
40 | }
|
---|
41 | return n;
|
---|
42 | }
|
---|
43 |
|
---|
44 | if (typeof prim === 'symbol') {
|
---|
45 | throw new $TypeError('Cannot convert a Symbol value to a BigInt');
|
---|
46 | }
|
---|
47 |
|
---|
48 | if (typeof prim !== 'bigint') {
|
---|
49 | throw new $SyntaxError('Assertion failed: unknown primitive type');
|
---|
50 | }
|
---|
51 |
|
---|
52 | return prim;
|
---|
53 | };
|
---|