Refactor re global object and crypto availability when bundling

This commit is contained in:
Michael Mclaughlin 2015-12-13 23:45:53 +00:00
parent 9a312f5b87
commit 4f93b20ea9
1 changed files with 23 additions and 22 deletions

View File

@ -1,10 +1,10 @@
/*! bignumber.js v2.1.2 https://github.com/MikeMcl/bignumber.js/LICENCE */
/*! bignumber.js v2.1.3 https://github.com/MikeMcl/bignumber.js/LICENCE */
;(function (global) {
;(function (globalObj) {
'use strict';
/*
bignumber.js v2.1.2
bignumber.js v2.1.3
A JavaScript library for arbitrary-precision arithmetic.
https://github.com/MikeMcl/bignumber.js
Copyright (c) 2015 Michael Mclaughlin <M8ch88l@gmail.com>
@ -12,8 +12,7 @@
*/
var BigNumber, parseNumeric,
crypto = global.crypto,
var cryptoObj, parseNumeric,
isNumeric = /^-?(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i,
mathceil = Math.ceil,
mathfloor = Math.floor,
@ -35,11 +34,13 @@
*/
MAX = 1E9; // 0 to MAX_INT32
if ( typeof crypto != 'undefined' ) cryptoObj = crypto;
/*
* Create and return a BigNumber constructor.
*/
function another(configObj) {
function constructorFactory(configObj) {
var div,
// id tracks the caller function, so its name can be included in error messages.
@ -314,7 +315,7 @@
// CONSTRUCTOR PROPERTIES
BigNumber.another = another;
BigNumber.another = constructorFactory;
BigNumber.ROUND_UP = 0;
BigNumber.ROUND_DOWN = 1;
@ -442,8 +443,8 @@
if ( has( p = 'CRYPTO' ) ) {
if ( v === !!v || v === 1 || v === 0 ) {
CRYPTO = !!( v && crypto && typeof crypto == 'object' );
if ( v && !CRYPTO && ERRORS ) raise( 2, 'crypto unavailable', crypto );
CRYPTO = !!( v && cryptoObj );
if ( v && !CRYPTO && ERRORS ) raise( 2, 'crypto unavailable', cryptoObj );
} else if (ERRORS) {
raise( 2, p + notBool, v );
}
@ -533,9 +534,9 @@
if (CRYPTO) {
// Browsers supporting crypto.getRandomValues.
if ( crypto && crypto.getRandomValues ) {
if ( cryptoObj && cryptoObj.getRandomValues ) {
a = crypto.getRandomValues( new Uint32Array( k *= 2 ) );
a = cryptoObj.getRandomValues( new Uint32Array( k *= 2 ) );
for ( ; i < k; ) {
@ -552,7 +553,7 @@
// Probability that v >= 9e15, is
// 7199254740992 / 9007199254740992 ~= 0.0008, i.e. 1 in 1251
if ( v >= 9e15 ) {
b = crypto.getRandomValues( new Uint32Array(2) );
b = cryptoObj.getRandomValues( new Uint32Array(2) );
a[i] = b[0];
a[i + 1] = b[1];
} else {
@ -566,10 +567,10 @@
i = k / 2;
// Node.js supporting crypto.randomBytes.
} else if ( crypto && crypto.randomBytes ) {
} else if ( cryptoObj && cryptoObj.randomBytes ) {
// buffer
a = crypto.randomBytes( k *= 7 );
a = cryptoObj.randomBytes( k *= 7 );
for ( ; i < k; ) {
@ -582,7 +583,7 @@
( a[i + 4] << 16 ) + ( a[i + 5] << 8 ) + a[i + 6];
if ( v >= 9e15 ) {
crypto.randomBytes(7).copy( a, i );
cryptoObj.randomBytes(7).copy( a, i );
} else {
// 0 <= (v % 1e14) <= 99999999999999
@ -592,7 +593,7 @@
}
i = k / 7;
} else if (ERRORS) {
raise( 14, 'crypto unavailable', crypto );
raise( 14, 'crypto unavailable', cryptoObj );
}
}
@ -2676,19 +2677,19 @@
// EXPORT
BigNumber = another();
// AMD.
if ( typeof define == 'function' && define.amd ) {
define( function () { return BigNumber; } );
define( function () { return constructorFactory(); } );
// Node and other environments that support module.exports.
} else if ( typeof module != 'undefined' && module.exports ) {
module.exports = BigNumber;
if ( !crypto ) try { crypto = require('cry' + 'pto'); } catch (e) {}
module.exports = constructorFactory();
if ( !cryptoObj ) try { cryptoOj = require('cry' + 'pto'); } catch (e) {}
// Browser.
} else if (globalObj) {
globalObj.BigNumber = constructorFactory();
} else {
global.BigNumber = BigNumber;
BigNumber = constructorFactory();
}
})(this);