From 46b1aad320482649f2ca368249f3576e867b69df Mon Sep 17 00:00:00 2001 From: Michael Mclaughlin Date: Wed, 9 Nov 2016 17:01:36 +0000 Subject: [PATCH] Remove crypto require. Add set as config alias. --- decimal.js | 61 ++++++++++++++++++++++++++---------------------------- 1 file changed, 29 insertions(+), 32 deletions(-) diff --git a/decimal.js b/decimal.js index e8cc702..99f5d84 100644 --- a/decimal.js +++ b/decimal.js @@ -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) { 'use strict'; /* - * decimal.js v6.0.0 + * decimal.js v7.0.0 * An arbitrary-precision Decimal type for JavaScript. * https://github.com/MikeMcl/decimal.js * Copyright (c) 2016 Michael Mclaughlin - * MIT Expat Licence + * MIT Licence */ @@ -37,7 +37,7 @@ Decimal = { // 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. // E.g. `Decimal.config({ precision: 20 });` @@ -92,7 +92,7 @@ maxE: EXP_LIMIT, // 1 to EXP_LIMIT // 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, - cryptoObject = typeof crypto != 'undefined' ? crypto : null, external = true, decimalError = '[DecimalError] ', invalidArgument = decimalError + 'Invalid argument: ', precisionLimitExceeded = decimalError + 'Precision limit exceeded', + cryptoUnavailable = decimalError + 'crypto unavailable', mathfloor = Math.floor, mathpow = Math.pow, @@ -3936,6 +3936,7 @@ * pow * random * round + * set * sign * sin * sinh @@ -4146,7 +4147,7 @@ * maxE {number} * minE {number} * modulo {number} - * crypto {boolean|number|undefined} + * crypto {boolean|number} * * E.g. Decimal.config({ precision: 20, rounding: 4 }) * @@ -4171,12 +4172,18 @@ } } - if (obj.hasOwnProperty(p = 'crypto')) { - if ((v = obj[p]) === void 0) { - this[p] = v; - } else if (v === true || v === false || v === 0 || v === 1) { - this[p] = !!(v && cryptoObject && - (cryptoObject.getRandomValues || cryptoObject.randomBytes)); + if ((v = obj[p = 'crypto']) !== void 0) { + if (v === true || v === false || v === 0 || v === 1) { + if (v) { + if (typeof crypto != 'undefined' && crypto && + (crypto.getRandomValues || crypto.randomBytes)) { + this[p] = true; + } else { + throw Error(cryptoUnavailable); + } + } else { + this[p] = false; + } } else { throw Error(invalidArgument + p + ': ' + v); } @@ -4306,7 +4313,7 @@ Decimal.ROUND_HALF_FLOOR = 8; Decimal.EUCLID = 9; - Decimal.config = config; + Decimal.config = Decimal.set = config; Decimal.clone = clone; Decimal.abs = abs; @@ -4557,12 +4564,12 @@ k = Math.ceil(sd / LOG_BASE); - if (this.crypto === false) { + if (!this.crypto) { for (; i < k;) rd[i++] = Math.random() * 1e7 | 0; // Browsers supporting crypto.getRandomValues. - } else if (cryptoObject && cryptoObject.getRandomValues) { - d = cryptoObject.getRandomValues(new Uint32Array(k)); + } else if (crypto.getRandomValues) { + d = crypto.getRandomValues(new Uint32Array(k)); for (; i < k;) { n = d[i]; @@ -4570,7 +4577,7 @@ // 0 <= n < 4294967296 // Probability n >= 4.29e9, is 4967296 / 4294967296 = 0.00116 (1 in 865). if (n >= 4.29e9) { - d[i] = cryptoObject.getRandomValues(new Uint32Array(1))[0]; + d[i] = crypto.getRandomValues(new Uint32Array(1))[0]; } else { // 0 <= n <= 4289999999 @@ -4580,10 +4587,10 @@ } // Node.js supporting crypto.randomBytes. - } else if (cryptoObject && cryptoObject.randomBytes) { + } else if (crypto.randomBytes) { // buffer - d = cryptoObject.randomBytes(k *= 4); + d = crypto.randomBytes(k *= 4); for (; i < k;) { @@ -4592,7 +4599,7 @@ // Probability n >= 2.14e9, is 7483648 / 2147483648 = 0.0035 (1 in 286). if (n >= 2.14e9) { - cryptoObject.randomBytes(4).copy(d, i); + crypto.randomBytes(4).copy(d, i); } else { // 0 <= n <= 2139999999 @@ -4603,10 +4610,8 @@ } i = k / 4; - } else if (this.crypto) { - throw Error(decimalError + 'crypto unavailable'); } else { - for (; i < k;) rd[i++] = Math.random() * 1e7 | 0; + throw Error(cryptoUnavailable); } k = rd[--i]; @@ -4778,14 +4783,6 @@ } else if (typeof module != 'undefined' && module.exports) { module.exports = Decimal; - if (!cryptoObject) { - try { - cryptoObject = require('cry' + 'pto'); - } catch (e) { - // Ignore. - } - } - // Browser. } else { if (!globalScope) {