1
0
mirror of https://github.com/MikeMcl/decimal.js.git synced 2024-10-27 20:34:12 +00:00

Remove crypto require. Add set as config alias.

This commit is contained in:
Michael Mclaughlin 2016-11-09 17:01:36 +00:00
parent fb37ca6bde
commit 46b1aad320

View File

@ -1,14 +1,14 @@
/*! decimal.js v6.0.0 https://github.com/MikeMcl/decimal.js/LICENCE */ /*! decimal.js v7.0.0 https://github.com/MikeMcl/decimal.js/LICENCE */
;(function (globalScope) { ;(function (globalScope) {
'use strict'; 'use strict';
/* /*
* decimal.js v6.0.0 * decimal.js v7.0.0
* An arbitrary-precision Decimal type for JavaScript. * An arbitrary-precision Decimal type for JavaScript.
* https://github.com/MikeMcl/decimal.js * https://github.com/MikeMcl/decimal.js
* Copyright (c) 2016 Michael Mclaughlin <M8ch88l@gmail.com> * Copyright (c) 2016 Michael Mclaughlin <M8ch88l@gmail.com>
* MIT Expat Licence * MIT Licence
*/ */
@ -37,7 +37,7 @@
Decimal = { Decimal = {
// These values must be integers within the stated ranges (inclusive). // These values must be integers within the stated ranges (inclusive).
// Most of these values can be changed at run-time using `Decimal.config`. // Most of these values can be changed at run-time using the `Decimal.config` method.
// The maximum number of significant digits of the result of a calculation or base conversion. // The maximum number of significant digits of the result of a calculation or base conversion.
// E.g. `Decimal.config({ precision: 20 });` // E.g. `Decimal.config({ precision: 20 });`
@ -92,7 +92,7 @@
maxE: EXP_LIMIT, // 1 to EXP_LIMIT maxE: EXP_LIMIT, // 1 to EXP_LIMIT
// Whether to use cryptographically-secure random number generation, if available. // Whether to use cryptographically-secure random number generation, if available.
crypto: void 0 // true/false/undefined crypto: false // true/false
}, },
@ -100,12 +100,12 @@
inexact, noConflict, quadrant, inexact, noConflict, quadrant,
cryptoObject = typeof crypto != 'undefined' ? crypto : null,
external = true, external = true,
decimalError = '[DecimalError] ', decimalError = '[DecimalError] ',
invalidArgument = decimalError + 'Invalid argument: ', invalidArgument = decimalError + 'Invalid argument: ',
precisionLimitExceeded = decimalError + 'Precision limit exceeded', precisionLimitExceeded = decimalError + 'Precision limit exceeded',
cryptoUnavailable = decimalError + 'crypto unavailable',
mathfloor = Math.floor, mathfloor = Math.floor,
mathpow = Math.pow, mathpow = Math.pow,
@ -3936,6 +3936,7 @@
* pow * pow
* random * random
* round * round
* set
* sign * sign
* sin * sin
* sinh * sinh
@ -4146,7 +4147,7 @@
* maxE {number} * maxE {number}
* minE {number} * minE {number}
* modulo {number} * modulo {number}
* crypto {boolean|number|undefined} * crypto {boolean|number}
* *
* E.g. Decimal.config({ precision: 20, rounding: 4 }) * E.g. Decimal.config({ precision: 20, rounding: 4 })
* *
@ -4171,12 +4172,18 @@
} }
} }
if (obj.hasOwnProperty(p = 'crypto')) { if ((v = obj[p = 'crypto']) !== void 0) {
if ((v = obj[p]) === void 0) { if (v === true || v === false || v === 0 || v === 1) {
this[p] = v; if (v) {
} else if (v === true || v === false || v === 0 || v === 1) { if (typeof crypto != 'undefined' && crypto &&
this[p] = !!(v && cryptoObject && (crypto.getRandomValues || crypto.randomBytes)) {
(cryptoObject.getRandomValues || cryptoObject.randomBytes)); this[p] = true;
} else {
throw Error(cryptoUnavailable);
}
} else {
this[p] = false;
}
} else { } else {
throw Error(invalidArgument + p + ': ' + v); throw Error(invalidArgument + p + ': ' + v);
} }
@ -4306,7 +4313,7 @@
Decimal.ROUND_HALF_FLOOR = 8; Decimal.ROUND_HALF_FLOOR = 8;
Decimal.EUCLID = 9; Decimal.EUCLID = 9;
Decimal.config = config; Decimal.config = Decimal.set = config;
Decimal.clone = clone; Decimal.clone = clone;
Decimal.abs = abs; Decimal.abs = abs;
@ -4557,12 +4564,12 @@
k = Math.ceil(sd / LOG_BASE); k = Math.ceil(sd / LOG_BASE);
if (this.crypto === false) { if (!this.crypto) {
for (; i < k;) rd[i++] = Math.random() * 1e7 | 0; for (; i < k;) rd[i++] = Math.random() * 1e7 | 0;
// Browsers supporting crypto.getRandomValues. // Browsers supporting crypto.getRandomValues.
} else if (cryptoObject && cryptoObject.getRandomValues) { } else if (crypto.getRandomValues) {
d = cryptoObject.getRandomValues(new Uint32Array(k)); d = crypto.getRandomValues(new Uint32Array(k));
for (; i < k;) { for (; i < k;) {
n = d[i]; n = d[i];
@ -4570,7 +4577,7 @@
// 0 <= n < 4294967296 // 0 <= n < 4294967296
// Probability n >= 4.29e9, is 4967296 / 4294967296 = 0.00116 (1 in 865). // Probability n >= 4.29e9, is 4967296 / 4294967296 = 0.00116 (1 in 865).
if (n >= 4.29e9) { if (n >= 4.29e9) {
d[i] = cryptoObject.getRandomValues(new Uint32Array(1))[0]; d[i] = crypto.getRandomValues(new Uint32Array(1))[0];
} else { } else {
// 0 <= n <= 4289999999 // 0 <= n <= 4289999999
@ -4580,10 +4587,10 @@
} }
// Node.js supporting crypto.randomBytes. // Node.js supporting crypto.randomBytes.
} else if (cryptoObject && cryptoObject.randomBytes) { } else if (crypto.randomBytes) {
// buffer // buffer
d = cryptoObject.randomBytes(k *= 4); d = crypto.randomBytes(k *= 4);
for (; i < k;) { for (; i < k;) {
@ -4592,7 +4599,7 @@
// Probability n >= 2.14e9, is 7483648 / 2147483648 = 0.0035 (1 in 286). // Probability n >= 2.14e9, is 7483648 / 2147483648 = 0.0035 (1 in 286).
if (n >= 2.14e9) { if (n >= 2.14e9) {
cryptoObject.randomBytes(4).copy(d, i); crypto.randomBytes(4).copy(d, i);
} else { } else {
// 0 <= n <= 2139999999 // 0 <= n <= 2139999999
@ -4603,10 +4610,8 @@
} }
i = k / 4; i = k / 4;
} else if (this.crypto) {
throw Error(decimalError + 'crypto unavailable');
} else { } else {
for (; i < k;) rd[i++] = Math.random() * 1e7 | 0; throw Error(cryptoUnavailable);
} }
k = rd[--i]; k = rd[--i];
@ -4778,14 +4783,6 @@
} else if (typeof module != 'undefined' && module.exports) { } else if (typeof module != 'undefined' && module.exports) {
module.exports = Decimal; module.exports = Decimal;
if (!cryptoObject) {
try {
cryptoObject = require('cry' + 'pto');
} catch (e) {
// Ignore.
}
}
// Browser. // Browser.
} else { } else {
if (!globalScope) { if (!globalScope) {