source: imaps-frontend/node_modules/unbox-primitive/index.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
Line 
1'use strict';
2
3var whichBoxedPrimitive = require('which-boxed-primitive');
4var callBound = require('call-bind/callBound');
5var hasSymbols = require('has-symbols')();
6var hasBigInts = require('has-bigints')();
7
8var stringToString = callBound('String.prototype.toString');
9var numberValueOf = callBound('Number.prototype.valueOf');
10var booleanValueOf = callBound('Boolean.prototype.valueOf');
11var symbolValueOf = hasSymbols && callBound('Symbol.prototype.valueOf');
12var bigIntValueOf = hasBigInts && callBound('BigInt.prototype.valueOf');
13
14module.exports = function unboxPrimitive(value) {
15 var which = whichBoxedPrimitive(value);
16 if (typeof which !== 'string') {
17 throw new TypeError(which === null ? 'value is an unboxed primitive' : 'value is a non-boxed-primitive object');
18 }
19
20 if (which === 'String') {
21 return stringToString(value);
22 }
23 if (which === 'Number') {
24 return numberValueOf(value);
25 }
26 if (which === 'Boolean') {
27 return booleanValueOf(value);
28 }
29 if (which === 'Symbol') {
30 if (!hasSymbols) {
31 throw new EvalError('somehow this environment does not have Symbols, but you have a boxed Symbol value. Please report this!');
32 }
33 return symbolValueOf(value);
34 }
35 if (which === 'BigInt') {
36 return bigIntValueOf(value);
37 }
38 throw new RangeError('unknown boxed primitive found: ' + which);
39};
Note: See TracBrowser for help on using the repository browser.