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