source: imaps-frontend/node_modules/es-abstract/2021/ToBigInt.js@ d565449

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

Update repo after prototype presentation

  • Property mode set to 100644
File size: 1.3 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
13var isNaN = require('../helpers/isNaN');
14
15// https://262.ecma-international.org/11.0/#sec-tobigint
16
17module.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};
Note: See TracBrowser for help on using the repository browser.