source: imaps-frontend/node_modules/es-abstract/2024/ToBigInt.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 1.2 KB
RevLine 
[d565449]1'use strict';
2
3var GetIntrinsic = require('get-intrinsic');
4
5var $BigInt = GetIntrinsic('%BigInt%', true);
6var $Number = GetIntrinsic('%Number%');
7var $TypeError = require('es-errors/type');
8var $SyntaxError = require('es-errors/syntax');
9
10var StringToBigInt = require('./StringToBigInt');
11var ToPrimitive = require('./ToPrimitive');
12
13// https://262.ecma-international.org/13.0/#sec-tobigint
14
15module.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};
Note: See TracBrowser for help on using the repository browser.