From 9b03a9f492428923c4e5fb9d3fd7deb174be867b Mon Sep 17 00:00:00 2001 From: Bryan Ingle Date: Mon, 1 Apr 2024 11:56:56 -0600 Subject: [PATCH] Release. Bump version number --- docs/_coverpage.md | 2 +- example/browser/example.js | 514 ++++++++++++------- lib/meta.js | 2 +- package.json | 2 +- test/dist/barchart-marketdata-api-tests-6.js | 504 +++++++++++------- 5 files changed, 640 insertions(+), 384 deletions(-) diff --git a/docs/_coverpage.md b/docs/_coverpage.md index c5bdec8..0b654c0 100644 --- a/docs/_coverpage.md +++ b/docs/_coverpage.md @@ -1,4 +1,4 @@ -# Barchart Market Data SDK JavaScript 6.2.5 +# Barchart Market Data SDK JavaScript 6.2.6 > Inject real-time market data into your JavaScript applications diff --git a/example/browser/example.js b/example/browser/example.js index 42fc72a..7b133ed 100644 --- a/example/browser/example.js +++ b/example/browser/example.js @@ -421,7 +421,6 @@ module.exports = (() => { //return formatPrice(price, that.quote().profile.unitCode, '-', true); }; - that.formatInteger = function (value) { return formatDecimal(value, 0, ','); }; @@ -5310,7 +5309,7 @@ module.exports = (() => { 'use strict'; return { - version: '6.2.5' + version: '6.2.6' }; })(); @@ -8421,8 +8420,8 @@ module.exports = (() => { /** * Returns a {@link Day} instance using today's local date. * - * @static * @public + * @static * @returns {Day} */ static getToday() { @@ -8609,8 +8608,8 @@ module.exports = (() => { } /** - * Returns a new {@link Decimal} instance with a value that returns - * the remainder of dividing by the value supplied. + * Returns a new {@link Decimal} instance with of the remainder when + * dividing the current instance by the value supplied. * * @public * @param {Decimal|Number|String} other @@ -8824,6 +8823,17 @@ module.exports = (() => { return this._big.toFixed(); } + /** + * Returns a {@link Number} that is approximately equal to the value of + * this {@link Decimal} instance. + * + * @public + * @returns {String} + */ + toNumber() { + return this._big.toNumber(); + } + /** * Returns the JSON representation. * @@ -8852,6 +8862,7 @@ module.exports = (() => { * use with the value emitted by {@link Decimal#toJSON}. * * @public + * @static * @param {Decimal|Number|String} value * @returns {Decimal} */ @@ -8863,6 +8874,7 @@ module.exports = (() => { * Returns an instance with the value of zero. * * @public + * @static * @returns {Decimal} */ static get ZERO() { @@ -8873,6 +8885,7 @@ module.exports = (() => { * Returns an instance with the value of one. * * @public + * @static * @returns {Decimal} */ static get ONE() { @@ -8883,6 +8896,7 @@ module.exports = (() => { * Returns an instance with the value of one. * * @public + * @static * @returns {Decimal} */ static get NEGATIVE_ONE() { @@ -8893,6 +8907,7 @@ module.exports = (() => { * Return the {@link RoundingMode} enumeration. * * @public + * @static * @returns {RoundingMode} */ static get ROUNDING_MODE() { @@ -8903,6 +8918,7 @@ module.exports = (() => { * Runs {@link Decimal#getIsZero} and returns the result. * * @public + * @static * @param {Decimal} instance * @returns {Boolean} */ @@ -8915,6 +8931,7 @@ module.exports = (() => { * Runs {@link Decimal#getIsZero} and returns the inverse. * * @public + * @static * @param {Decimal} instance * @returns {Boolean} */ @@ -8927,6 +8944,7 @@ module.exports = (() => { * Runs {@link Decimal#getIsPositive} and returns the result. * * @public + * @static * @param {Decimal} instance * @returns {Boolean} */ @@ -8939,6 +8957,7 @@ module.exports = (() => { * Checks an instance to see if its negative or zero. * * @public + * @static * @param {Decimal} instance * @returns {Boolean} */ @@ -8951,6 +8970,7 @@ module.exports = (() => { * Runs {@link Decimal#getIsNegative} and returns the result. * * @public + * @static * @param {Decimal} instance * @returns {Boolean} */ @@ -8963,6 +8983,7 @@ module.exports = (() => { * Checks an instance to see if its positive or zero. * * @public + * @static * @param {Decimal} instance * @returns {Boolean} */ @@ -8975,6 +8996,7 @@ module.exports = (() => { * A comparator function for {@link Decimal} instances. * * @public + * @static * @param {Decimal} a * @param {Decimal} b * @returns {Number} @@ -9037,6 +9059,7 @@ module.exports = (() => { * Rounds away from zero. * * @public + * @static * @returns {RoundingMode} */ static get UP() { @@ -9047,6 +9070,7 @@ module.exports = (() => { * Rounds towards zero. * * @public + * @static * @returns {RoundingMode} */ static get DOWN() { @@ -9057,6 +9081,7 @@ module.exports = (() => { * Rounds towards nearest neighbor. If equidistant, rounds away from zero. * * @public + * @static * @returns {RoundingMode} */ static get NORMAL() { @@ -9258,6 +9283,7 @@ module.exports = (() => { * Given a code, returns the enumeration item. * * @public + * @static * @param {String} code * @returns {Timezones|null} */ @@ -12309,10 +12335,10 @@ module.exports={ },{}],86:[function(require,module,exports){ /* - * big.js v5.2.2 + * big.js v6.2.1 * A small, fast, easy-to-use library for arbitrary-precision decimal arithmetic. - * Copyright (c) 2018 Michael Mclaughlin - * https://github.com/MikeMcl/big.js/LICENCE + * Copyright (c) 2022 Michael Mclaughlin + * https://github.com/MikeMcl/big.js/LICENCE.md */ ;(function (GLOBAL) { 'use strict'; @@ -12328,7 +12354,7 @@ module.exports={ * The maximum number of decimal places (DP) of the results of operations involving division: * div and sqrt, and pow with negative exponents. */ - DP = 20, // 0 to MAX_DP + DP = 20, // 0 to MAX_DP /* * The rounding mode (RM) used when rounding to the above decimal places. @@ -12356,11 +12382,17 @@ module.exports={ /* * The positive exponent (PE) at and above which toString returns exponential notation. * (JavaScript numbers: 21) - * 1000000 is the maximum recommended exponent value of a Big. - * (This limit is not enforced or checked.) + * 1000000 is the maximum recommended exponent value of a Big, but this limit is not enforced. */ PE = 21, // 0 to 1000000 + /* + * When true, an error will be thrown if a primitive number is passed to the Big constructor, + * or if valueOf is called, or if toNumber is called on a Big which cannot be converted to a + * primitive number without a loss of precision. + */ + STRICT = false, // true or false + /**************************************************************************************************/ @@ -12380,7 +12412,6 @@ module.exports={ /* * Create and return a Big constructor. - * */ function _Big_() { @@ -12402,13 +12433,20 @@ module.exports={ x.e = n.e; x.c = n.c.slice(); } else { + if (typeof n !== 'string') { + if (Big.strict === true && typeof n !== 'bigint') { + throw TypeError(INVALID + 'value'); + } + + // Minus zero? + n = n === 0 && 1 / n < 0 ? '-0' : String(n); + } + parse(x, n); } - /* - * Retain a reference to this Big constructor, and shadow Big.prototype.constructor which - * points to Object. - */ + // Retain a reference to this Big constructor. + // Shadow Big.prototype.constructor which points to Object. x.constructor = Big; } @@ -12417,7 +12455,11 @@ module.exports={ Big.RM = RM; Big.NE = NE; Big.PE = PE; - Big.version = '5.2.2'; + Big.strict = STRICT; + Big.roundDown = 0; + Big.roundHalfUp = 1; + Big.roundHalfEven = 2; + Big.roundUp = 3; return Big; } @@ -12432,9 +12474,9 @@ module.exports={ function parse(x, n) { var e, i, nl; - // Minus zero? - if (n === 0 && 1 / n < 0) n = '-0'; - else if (!NUMERIC.test(n += '')) throw Error(INVALID + 'number'); + if (!NUMERIC.test(n)) { + throw Error(INVALID + 'number'); + } // Determine sign. x.s = n.charAt(0) == '-' ? (n = n.slice(1), -1) : 1; @@ -12480,69 +12522,68 @@ module.exports={ /* - * Round Big x to a maximum of dp decimal places using rounding mode rm. - * Called by stringify, P.div, P.round and P.sqrt. + * Round Big x to a maximum of sd significant digits using rounding mode rm. * * x {Big} The Big to round. - * dp {number} Integer, 0 to MAX_DP inclusive. - * rm {number} 0, 1, 2 or 3 (DOWN, HALF_UP, HALF_EVEN, UP) + * sd {number} Significant digits: integer, 0 to MAX_DP inclusive. + * rm {number} Rounding mode: 0 (down), 1 (half-up), 2 (half-even) or 3 (up). * [more] {boolean} Whether the result of division was truncated. */ - function round(x, dp, rm, more) { - var xc = x.c, - i = x.e + dp + 1; - - if (i < xc.length) { - if (rm === 1) { - - // xc[i] is the digit after the digit that may be rounded up. - more = xc[i] >= 5; - } else if (rm === 2) { - more = xc[i] > 5 || xc[i] == 5 && - (more || i < 0 || xc[i + 1] !== UNDEFINED || xc[i - 1] & 1); - } else if (rm === 3) { - more = more || !!xc[0]; - } else { - more = false; - if (rm !== 0) throw Error(INVALID_RM); - } - - if (i < 1) { - xc.length = 1; + function round(x, sd, rm, more) { + var xc = x.c; - if (more) { + if (rm === UNDEFINED) rm = x.constructor.RM; + if (rm !== 0 && rm !== 1 && rm !== 2 && rm !== 3) { + throw Error(INVALID_RM); + } - // 1, 0.1, 0.01, 0.001, 0.0001 etc. - x.e = -dp; - xc[0] = 1; - } else { + if (sd < 1) { + more = + rm === 3 && (more || !!xc[0]) || sd === 0 && ( + rm === 1 && xc[0] >= 5 || + rm === 2 && (xc[0] > 5 || xc[0] === 5 && (more || xc[1] !== UNDEFINED)) + ); - // Zero. - xc[0] = x.e = 0; - } - } else { + xc.length = 1; - // Remove any digits after the required decimal places. - xc.length = i--; + if (more) { - // Round up? - if (more) { + // 1, 0.1, 0.01, 0.001, 0.0001 etc. + x.e = x.e - sd + 1; + xc[0] = 1; + } else { - // Rounding up may mean the previous digit has to be rounded up. - for (; ++xc[i] > 9;) { - xc[i] = 0; - if (!i--) { - ++x.e; - xc.unshift(1); - } + // Zero. + xc[0] = x.e = 0; + } + } else if (sd < xc.length) { + + // xc[sd] is the digit after the digit that may be rounded up. + more = + rm === 1 && xc[sd] >= 5 || + rm === 2 && (xc[sd] > 5 || xc[sd] === 5 && + (more || xc[sd + 1] !== UNDEFINED || xc[sd - 1] & 1)) || + rm === 3 && (more || !!xc[0]); + + // Remove any digits after the required precision. + xc.length = sd; + + // Round up? + if (more) { + + // Rounding up may mean the previous digit has to be rounded up. + for (; ++xc[--sd] > 9;) { + xc[sd] = 0; + if (sd === 0) { + ++x.e; + xc.unshift(1); + break; } } - - // Remove trailing zeros. - for (i = xc.length; !xc[--i];) xc.pop(); } - } else if (rm < 0 || rm > 3 || rm !== ~~rm) { - throw Error(INVALID_RM); + + // Remove trailing zeros. + for (sd = xc.length; !xc[--sd];) xc.pop(); } return x; @@ -12552,47 +12593,14 @@ module.exports={ /* * Return a string representing the value of Big x in normal or exponential notation. * Handles P.toExponential, P.toFixed, P.toJSON, P.toPrecision, P.toString and P.valueOf. - * - * x {Big} - * id? {number} Caller id. - * 1 toExponential - * 2 toFixed - * 3 toPrecision - * 4 valueOf - * n? {number|undefined} Caller's argument. - * k? {number|undefined} */ - function stringify(x, id, n, k) { - var e, s, - Big = x.constructor, - z = !x.c[0]; - - if (n !== UNDEFINED) { - if (n !== ~~n || n < (id == 3) || n > MAX_DP) { - throw Error(id == 3 ? INVALID + 'precision' : INVALID_DP); - } - - x = new Big(x); - - // The index of the digit that may be rounded up. - n = k - x.e; - - // Round? - if (x.c.length > ++k) round(x, n, Big.RM); - - // toFixed: recalculate k as x.e may have changed if value rounded up. - if (id == 2) k = x.e + n + 1; - - // Append zeros? - for (; x.c.length < k;) x.c.push(0); - } - - e = x.e; - s = x.c.join(''); - n = s.length; + function stringify(x, doExponential, isNonzero) { + var e = x.e, + s = x.c.join(''), + n = s.length; // Exponential notation? - if (id != 2 && (id == 1 || id == 3 && k <= e || e <= Big.NE || e >= Big.PE)) { + if (doExponential) { s = s.charAt(0) + (n > 1 ? '.' + s.slice(1) : '') + (e < 0 ? 'e' : 'e+') + e; // Normal notation. @@ -12600,13 +12608,16 @@ module.exports={ for (; ++e;) s = '0' + s; s = '0.' + s; } else if (e > 0) { - if (++e > n) for (e -= n; e--;) s += '0'; - else if (e < n) s = s.slice(0, e) + '.' + s.slice(e); + if (++e > n) { + for (e -= n; e--;) s += '0'; + } else if (e < n) { + s = s.slice(0, e) + '.' + s.slice(e); + } } else if (n > 1) { s = s.charAt(0) + '.' + s.slice(1); } - return x.s < 0 && (!z || id == 4) ? '-' + s : s; + return x.s < 0 && isNonzero ? '-' + s : s; } @@ -12627,7 +12638,7 @@ module.exports={ * Return 1 if the value of this Big is greater than the value of Big y, * -1 if the value of this Big is less than the value of Big y, or * 0 if they have the same value. - */ + */ P.cmp = function (y) { var isneg, x = this, @@ -12673,13 +12684,21 @@ module.exports={ k = x.s == y.s ? 1 : -1, dp = Big.DP; - if (dp !== ~~dp || dp < 0 || dp > MAX_DP) throw Error(INVALID_DP); + if (dp !== ~~dp || dp < 0 || dp > MAX_DP) { + throw Error(INVALID_DP); + } // Divisor is zero? - if (!b[0]) throw Error(DIV_BY_ZERO); + if (!b[0]) { + throw Error(DIV_BY_ZERO); + } // Dividend is 0? Return +-0. - if (!a[0]) return new Big(k * 0); + if (!a[0]) { + y.s = k; + y.c = [y.e = 0]; + return y; + } var bl, bt, n, cmp, ri, bz = b.slice(), @@ -12690,10 +12709,10 @@ module.exports={ q = y, // quotient qc = q.c = [], qi = 0, - d = dp + (q.e = x.e - y.e) + 1; // number of digits of the result + p = dp + (q.e = x.e - y.e) + 1; // precision of the result q.s = k; - k = d < 0 ? 0 : d; + k = p < 0 ? 0 : p; // Create version of divisor with leading zero. bz.unshift(0); @@ -12754,10 +12773,11 @@ module.exports={ // There can't be more than one zero. qc.shift(); q.e--; + p--; } // Round? - if (qi > d) round(q, dp, Big.RM, r[0] !== UNDEFINED); + if (qi > p) round(q, p, Big.RM, r[0] !== UNDEFINED); return q; }; @@ -12767,7 +12787,7 @@ module.exports={ * Return true if the value of this Big is equal to the value of Big y, otherwise return false. */ P.eq = function (y) { - return !this.cmp(y); + return this.cmp(y) === 0; }; @@ -12829,9 +12849,14 @@ module.exports={ // Either zero? if (!xc[0] || !yc[0]) { - - // y is non-zero? x is non-zero? Or both are zero. - return yc[0] ? (y.s = -b, y) : new Big(xc[0] ? x : 0); + if (yc[0]) { + y.s = -b; + } else if (xc[0]) { + y = new Big(x); + } else { + y.s = 1; + } + return y; } // Determine which is the bigger number. Prepend zeros to equalise exponents. @@ -12921,7 +12946,9 @@ module.exports={ a = x.s, b = (y = new Big(y)).s; - if (!y.c[0]) throw Error(DIV_BY_ZERO); + if (!y.c[0]) { + throw Error(DIV_BY_ZERO); + } x.s = y.s = 1; ygtx = y.cmp(x) == 1; @@ -12939,21 +12966,31 @@ module.exports={ return this.minus(x.times(y)); }; + + + /* + * Return a new Big whose value is the value of this Big negated. + */ + P.neg = function () { + var x = new this.constructor(this); + x.s = -x.s; + return x; + }; /* * Return a new Big whose value is the value of this Big plus the value of Big y. */ P.plus = P.add = function (y) { - var t, + var e, k, t, x = this, - Big = x.constructor, - a = x.s, - b = (y = new Big(y)).s; + Big = x.constructor; + + y = new Big(y); // Signs differ? - if (a != b) { - y.s = -b; + if (x.s != y.s) { + y.s = -y.s; return x.minus(y); } @@ -12962,24 +12999,33 @@ module.exports={ ye = y.e, yc = y.c; - // Either zero? y is non-zero? x is non-zero? Or both are zero. - if (!xc[0] || !yc[0]) return yc[0] ? y : new Big(xc[0] ? x : a * 0); + // Either zero? + if (!xc[0] || !yc[0]) { + if (!yc[0]) { + if (xc[0]) { + y = new Big(x); + } else { + y.s = x.s; + } + } + return y; + } xc = xc.slice(); // Prepend zeros to equalise exponents. // Note: reverse faster than unshifts. - if (a = xe - ye) { - if (a > 0) { + if (e = xe - ye) { + if (e > 0) { ye = xe; t = yc; } else { - a = -a; + e = -e; t = xc; } t.reverse(); - for (; a--;) t.push(0); + for (; e--;) t.push(0); t.reverse(); } @@ -12990,20 +13036,20 @@ module.exports={ xc = t; } - a = yc.length; + e = yc.length; // Only start adding at yc.length - 1 as the further digits of xc can be left as they are. - for (b = 0; a; xc[a] %= 10) b = (xc[--a] = xc[a] + yc[a] + b) / 10 | 0; + for (k = 0; e; xc[e] %= 10) k = (xc[--e] = xc[e] + yc[e] + k) / 10 | 0; // No need to check for zero, as +x + +y != 0 && -x + -y != 0 - if (b) { - xc.unshift(b); + if (k) { + xc.unshift(k); ++ye; } // Remove trailing zeros. - for (a = xc.length; xc[--a] === 0;) xc.pop(); + for (e = xc.length; xc[--e] === 0;) xc.pop(); y.c = xc; y.e = ye; @@ -13021,11 +13067,14 @@ module.exports={ */ P.pow = function (n) { var x = this, - one = new x.constructor(1), + one = new x.constructor('1'), y = one, isneg = n < 0; - if (n !== ~~n || n < -MAX_POWER || n > MAX_POWER) throw Error(INVALID + 'exponent'); + if (n !== ~~n || n < -MAX_POWER || n > MAX_POWER) { + throw Error(INVALID + 'exponent'); + } + if (isneg) n = -n; for (;;) { @@ -13040,20 +13089,35 @@ module.exports={ /* - * Return a new Big whose value is the value of this Big rounded using rounding mode rm - * to a maximum of dp decimal places, or, if dp is negative, to an integer which is a - * multiple of 10**-dp. + * Return a new Big whose value is the value of this Big rounded to a maximum precision of sd + * significant digits using rounding mode rm, or Big.RM if rm is not specified. + * + * sd {number} Significant digits: integer, 1 to MAX_DP inclusive. + * rm? {number} Rounding mode: 0 (down), 1 (half-up), 2 (half-even) or 3 (up). + */ + P.prec = function (sd, rm) { + if (sd !== ~~sd || sd < 1 || sd > MAX_DP) { + throw Error(INVALID + 'precision'); + } + return round(new this.constructor(this), sd, rm); + }; + + + /* + * Return a new Big whose value is the value of this Big rounded to a maximum of dp decimal places + * using rounding mode rm, or Big.RM if rm is not specified. + * If dp is negative, round to an integer which is a multiple of 10**-dp. * If dp is not specified, round to 0 decimal places. - * If rm is not specified, use Big.RM. * * dp? {number} Integer, -MAX_DP to MAX_DP inclusive. - * rm? 0, 1, 2 or 3 (ROUND_DOWN, ROUND_HALF_UP, ROUND_HALF_EVEN, ROUND_UP) + * rm? {number} Rounding mode: 0 (down), 1 (half-up), 2 (half-even) or 3 (up). */ P.round = function (dp, rm) { - var Big = this.constructor; if (dp === UNDEFINED) dp = 0; - else if (dp !== ~~dp || dp < -MAX_DP || dp > MAX_DP) throw Error(INVALID_DP); - return round(new Big(this), dp, rm === UNDEFINED ? Big.RM : rm); + else if (dp !== ~~dp || dp < -MAX_DP || dp > MAX_DP) { + throw Error(INVALID_DP); + } + return round(new this.constructor(this), dp + this.e + 1, rm); }; @@ -13067,13 +13131,15 @@ module.exports={ Big = x.constructor, s = x.s, e = x.e, - half = new Big(0.5); + half = new Big('0.5'); // Zero? if (!x.c[0]) return new Big(x); // Negative? - if (s < 0) throw Error(NAME + 'No square root'); + if (s < 0) { + throw Error(NAME + 'No square root'); + } // Estimate. s = Math.sqrt(x + ''); @@ -13085,9 +13151,9 @@ module.exports={ if (!(c.length + e & 1)) c += '0'; s = Math.sqrt(c); e = ((e + 1) / 2 | 0) - (e < 0 || e & 1); - r = new Big((s == 1 / 0 ? '1e' : (s = s.toExponential()).slice(0, s.indexOf('e') + 1)) + e); + r = new Big((s == 1 / 0 ? '5e' : (s = s.toExponential()).slice(0, s.indexOf('e') + 1)) + e); } else { - r = new Big(s); + r = new Big(s + ''); } e = r.e + (Big.DP += 4); @@ -13098,7 +13164,7 @@ module.exports={ r = half.times(t.plus(x.div(t))); } while (t.c.slice(0, e).join('') !== r.c.slice(0, e).join('')); - return round(r, Big.DP -= 4, Big.RM); + return round(r, (Big.DP -= 4) + r.e + 1, Big.RM); }; @@ -13120,7 +13186,10 @@ module.exports={ y.s = x.s == y.s ? 1 : -1; // Return signed 0 if either 0. - if (!xc[0] || !yc[0]) return new Big(y.s * 0); + if (!xc[0] || !yc[0]) { + y.c = [y.e = 0]; + return y; + } // Initialise exponent of result as x.e + y.e. y.e = i + j; @@ -13155,7 +13224,7 @@ module.exports={ b = b / 10 | 0; } - c[j] = (c[j] + b) % 10; + c[j] = b; } // Increment result exponent if there is a final carry, otherwise remove leading zero. @@ -13171,39 +13240,53 @@ module.exports={ /* - * Return a string representing the value of this Big in exponential notation to dp fixed decimal - * places and rounded using Big.RM. + * Return a string representing the value of this Big in exponential notation rounded to dp fixed + * decimal places using rounding mode rm, or Big.RM if rm is not specified. * - * dp? {number} Integer, 0 to MAX_DP inclusive. + * dp? {number} Decimal places: integer, 0 to MAX_DP inclusive. + * rm? {number} Rounding mode: 0 (down), 1 (half-up), 2 (half-even) or 3 (up). */ - P.toExponential = function (dp) { - return stringify(this, 1, dp, dp); + P.toExponential = function (dp, rm) { + var x = this, + n = x.c[0]; + + if (dp !== UNDEFINED) { + if (dp !== ~~dp || dp < 0 || dp > MAX_DP) { + throw Error(INVALID_DP); + } + x = round(new x.constructor(x), ++dp, rm); + for (; x.c.length < dp;) x.c.push(0); + } + + return stringify(x, true, !!n); }; /* - * Return a string representing the value of this Big in normal notation to dp fixed decimal - * places and rounded using Big.RM. + * Return a string representing the value of this Big in normal notation rounded to dp fixed + * decimal places using rounding mode rm, or Big.RM if rm is not specified. * - * dp? {number} Integer, 0 to MAX_DP inclusive. + * dp? {number} Decimal places: integer, 0 to MAX_DP inclusive. + * rm? {number} Rounding mode: 0 (down), 1 (half-up), 2 (half-even) or 3 (up). * * (-0).toFixed(0) is '0', but (-0.1).toFixed(0) is '-0'. * (-0).toFixed(1) is '0.0', but (-0.01).toFixed(1) is '-0.0'. */ - P.toFixed = function (dp) { - return stringify(this, 2, dp, this.e + dp); - }; + P.toFixed = function (dp, rm) { + var x = this, + n = x.c[0]; + if (dp !== UNDEFINED) { + if (dp !== ~~dp || dp < 0 || dp > MAX_DP) { + throw Error(INVALID_DP); + } + x = round(new x.constructor(x), dp + x.e + 1, rm); - /* - * Return a string representing the value of this Big rounded to sd significant digits using - * Big.RM. Use exponential notation if sd is less than the number of digits necessary to represent - * the integer part of the value in normal notation. - * - * sd {number} Integer, 1 to MAX_DP inclusive. - */ - P.toPrecision = function (sd) { - return stringify(this, 3, sd, sd - 1); + // x.e may have changed if the value is rounded up. + for (dp = dp + x.e + 1; x.c.length < dp;) x.c.push(0); + } + + return stringify(x, false, !!n); }; @@ -13213,8 +13296,48 @@ module.exports={ * Big.PE, or a negative exponent equal to or less than Big.NE. * Omit the sign for negative zero. */ - P.toString = function () { - return stringify(this); + P.toJSON = P.toString = function () { + var x = this, + Big = x.constructor; + return stringify(x, x.e <= Big.NE || x.e >= Big.PE, !!x.c[0]); + }; + + + /* + * Return the value of this Big as a primitve number. + */ + P.toNumber = function () { + var n = Number(stringify(this, true, true)); + if (this.constructor.strict === true && !this.eq(n.toString())) { + throw Error(NAME + 'Imprecise conversion'); + } + return n; + }; + + + /* + * Return a string representing the value of this Big rounded to sd significant digits using + * rounding mode rm, or Big.RM if rm is not specified. + * Use exponential notation if sd is less than the number of digits necessary to represent + * the integer part of the value in normal notation. + * + * sd {number} Significant digits: integer, 1 to MAX_DP inclusive. + * rm? {number} Rounding mode: 0 (down), 1 (half-up), 2 (half-even) or 3 (up). + */ + P.toPrecision = function (sd, rm) { + var x = this, + Big = x.constructor, + n = x.c[0]; + + if (sd !== UNDEFINED) { + if (sd !== ~~sd || sd < 1 || sd > MAX_DP) { + throw Error(INVALID + 'precision'); + } + x = round(new Big(x), sd, rm); + for (; x.c.length < sd;) x.c.push(0); + } + + return stringify(x, sd <= x.e || x.e <= Big.NE || x.e >= Big.PE, !!n); }; @@ -13224,8 +13347,13 @@ module.exports={ * Big.PE, or a negative exponent equal to or less than Big.NE. * Include the sign for negative zero. */ - P.valueOf = P.toJSON = function () { - return stringify(this, 4); + P.valueOf = function () { + var x = this, + Big = x.constructor; + if (Big.strict === true) { + throw Error(NAME + 'valueOf disallowed'); + } + return stringify(x, x.e <= Big.NE || x.e >= Big.PE, true); }; @@ -13293,7 +13421,7 @@ var patterns = { timezone: /([Z+-].*)$/, timezoneZ: /^(Z)$/, timezoneHH: /^([+-]\d{2})$/, - timezoneHHMM: /^([+-]\d{2}):?(\d{2})$/ + timezoneHHMM: /^([+-])(\d{2}):?(\d{2})$/ }; // Parse various time zone offset formats to an offset in milliseconds function tzParseTimezone(timezoneString, date, isUtcDate) { @@ -13329,15 +13457,15 @@ function tzParseTimezone(timezoneString, date, isUtcDate) { token = patterns.timezoneHHMM.exec(timezoneString); if (token) { - hours = parseInt(token[1], 10); - var minutes = parseInt(token[2], 10); + hours = parseInt(token[2], 10); + var minutes = parseInt(token[3], 10); if (!validateTimezone(hours, minutes)) { return NaN; } absoluteOffset = Math.abs(hours) * MILLISECONDS_IN_HOUR + minutes * MILLISECONDS_IN_MINUTE; - return hours > 0 ? -absoluteOffset : absoluteOffset; + return token[1] === '+' ? -absoluteOffset : absoluteOffset; } // IANA time zone @@ -13419,9 +13547,9 @@ Object.defineProperty(exports, "__esModule", { }); exports.default = tzTokenizeDate; -/** - * Returns the [year, month, day, hour, minute, seconds] tokens of the provided - * `date` as it will be rendered in the `timeZone`. +/** + * Returns the [year, month, day, hour, minute, seconds] tokens of the provided + * `date` as it will be rendered in the `timeZone`. */ function tzTokenizeDate(date, timeZone) { var dtf = getDateTimeFormat(timeZone); @@ -13461,7 +13589,7 @@ function partsOffset(dtf, date) { } function hackyOffset(dtf, date) { - var formatted = dtf.format(date).replace(/\u200E/g, ''); + var formatted = dtf.format(date); var parsed = /(\d+)\/(\d+)\/(\d+),? (\d+):(\d+):(\d+)/.exec(formatted); // var [, fMonth, fDay, fYear, fHour, fMinute, fSecond] = parsed // return [fYear, fMonth, fDay, fHour, fMinute, fSecond] @@ -13477,10 +13605,10 @@ function getDateTimeFormat(timeZone) { if (!dtfCache[timeZone]) { // New browsers use `hourCycle`, IE and Chrome <73 does not support it and uses `hour12` var testDateFormatted = new Intl.DateTimeFormat('en-US', { - hour12: false, + hourCycle: 'h23', timeZone: 'America/New_York', year: 'numeric', - month: 'numeric', + month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', @@ -13488,7 +13616,7 @@ function getDateTimeFormat(timeZone) { }).format(new Date('2014-06-25T04:00:00.123Z')); var hourCycleSupported = testDateFormatted === '06/25/2014, 00:00:00' || testDateFormatted === '‎06‎/‎25‎/‎2014‎ ‎00‎:‎00‎:‎00'; dtfCache[timeZone] = hourCycleSupported ? new Intl.DateTimeFormat('en-US', { - hour12: false, + hourCycle: 'h23', timeZone: timeZone, year: 'numeric', month: 'numeric', @@ -13497,7 +13625,7 @@ function getDateTimeFormat(timeZone) { minute: '2-digit', second: '2-digit' }) : new Intl.DateTimeFormat('en-US', { - hourCycle: 'h23', + hour12: false, timeZone: timeZone, year: 'numeric', month: 'numeric', diff --git a/lib/meta.js b/lib/meta.js index 37fc162..b15f80d 100644 --- a/lib/meta.js +++ b/lib/meta.js @@ -2,6 +2,6 @@ module.exports = (() => { 'use strict'; return { - version: '6.2.5' + version: '6.2.6' }; })(); \ No newline at end of file diff --git a/package.json b/package.json index c298bc4..09472ff 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@barchart/marketdata-api-js", - "version": "6.2.5", + "version": "6.2.6", "description": "SDK for streaming market data from Barchart.com", "author": { "name": "Eero Pikat", diff --git a/test/dist/barchart-marketdata-api-tests-6.js b/test/dist/barchart-marketdata-api-tests-6.js index c90b72b..46c7244 100644 --- a/test/dist/barchart-marketdata-api-tests-6.js +++ b/test/dist/barchart-marketdata-api-tests-6.js @@ -3576,8 +3576,8 @@ module.exports = (() => { } /** - * Returns a new {@link Decimal} instance with a value that returns - * the remainder of dividing by the value supplied. + * Returns a new {@link Decimal} instance with of the remainder when + * dividing the current instance by the value supplied. * * @public * @param {Decimal|Number|String} other @@ -3791,6 +3791,17 @@ module.exports = (() => { return this._big.toFixed(); } + /** + * Returns a {@link Number} that is approximately equal to the value of + * this {@link Decimal} instance. + * + * @public + * @returns {String} + */ + toNumber() { + return this._big.toNumber(); + } + /** * Returns the JSON representation. * @@ -3819,6 +3830,7 @@ module.exports = (() => { * use with the value emitted by {@link Decimal#toJSON}. * * @public + * @static * @param {Decimal|Number|String} value * @returns {Decimal} */ @@ -3830,6 +3842,7 @@ module.exports = (() => { * Returns an instance with the value of zero. * * @public + * @static * @returns {Decimal} */ static get ZERO() { @@ -3840,6 +3853,7 @@ module.exports = (() => { * Returns an instance with the value of one. * * @public + * @static * @returns {Decimal} */ static get ONE() { @@ -3850,6 +3864,7 @@ module.exports = (() => { * Returns an instance with the value of one. * * @public + * @static * @returns {Decimal} */ static get NEGATIVE_ONE() { @@ -3860,6 +3875,7 @@ module.exports = (() => { * Return the {@link RoundingMode} enumeration. * * @public + * @static * @returns {RoundingMode} */ static get ROUNDING_MODE() { @@ -3870,6 +3886,7 @@ module.exports = (() => { * Runs {@link Decimal#getIsZero} and returns the result. * * @public + * @static * @param {Decimal} instance * @returns {Boolean} */ @@ -3882,6 +3899,7 @@ module.exports = (() => { * Runs {@link Decimal#getIsZero} and returns the inverse. * * @public + * @static * @param {Decimal} instance * @returns {Boolean} */ @@ -3894,6 +3912,7 @@ module.exports = (() => { * Runs {@link Decimal#getIsPositive} and returns the result. * * @public + * @static * @param {Decimal} instance * @returns {Boolean} */ @@ -3906,6 +3925,7 @@ module.exports = (() => { * Checks an instance to see if its negative or zero. * * @public + * @static * @param {Decimal} instance * @returns {Boolean} */ @@ -3918,6 +3938,7 @@ module.exports = (() => { * Runs {@link Decimal#getIsNegative} and returns the result. * * @public + * @static * @param {Decimal} instance * @returns {Boolean} */ @@ -3930,6 +3951,7 @@ module.exports = (() => { * Checks an instance to see if its positive or zero. * * @public + * @static * @param {Decimal} instance * @returns {Boolean} */ @@ -3942,6 +3964,7 @@ module.exports = (() => { * A comparator function for {@link Decimal} instances. * * @public + * @static * @param {Decimal} a * @param {Decimal} b * @returns {Number} @@ -4004,6 +4027,7 @@ module.exports = (() => { * Rounds away from zero. * * @public + * @static * @returns {RoundingMode} */ static get UP() { @@ -4014,6 +4038,7 @@ module.exports = (() => { * Rounds towards zero. * * @public + * @static * @returns {RoundingMode} */ static get DOWN() { @@ -4024,6 +4049,7 @@ module.exports = (() => { * Rounds towards nearest neighbor. If equidistant, rounds away from zero. * * @public + * @static * @returns {RoundingMode} */ static get NORMAL() { @@ -4225,6 +4251,7 @@ module.exports = (() => { * Given a code, returns the enumeration item. * * @public + * @static * @param {String} code * @returns {Timezones|null} */ @@ -8521,10 +8548,10 @@ exports.ParseError = ParseError; },{"./conventions":43}],49:[function(require,module,exports){ /* - * big.js v5.2.2 + * big.js v6.2.1 * A small, fast, easy-to-use library for arbitrary-precision decimal arithmetic. - * Copyright (c) 2018 Michael Mclaughlin - * https://github.com/MikeMcl/big.js/LICENCE + * Copyright (c) 2022 Michael Mclaughlin + * https://github.com/MikeMcl/big.js/LICENCE.md */ ;(function (GLOBAL) { 'use strict'; @@ -8540,7 +8567,7 @@ exports.ParseError = ParseError; * The maximum number of decimal places (DP) of the results of operations involving division: * div and sqrt, and pow with negative exponents. */ - DP = 20, // 0 to MAX_DP + DP = 20, // 0 to MAX_DP /* * The rounding mode (RM) used when rounding to the above decimal places. @@ -8568,11 +8595,17 @@ exports.ParseError = ParseError; /* * The positive exponent (PE) at and above which toString returns exponential notation. * (JavaScript numbers: 21) - * 1000000 is the maximum recommended exponent value of a Big. - * (This limit is not enforced or checked.) + * 1000000 is the maximum recommended exponent value of a Big, but this limit is not enforced. */ PE = 21, // 0 to 1000000 + /* + * When true, an error will be thrown if a primitive number is passed to the Big constructor, + * or if valueOf is called, or if toNumber is called on a Big which cannot be converted to a + * primitive number without a loss of precision. + */ + STRICT = false, // true or false + /**************************************************************************************************/ @@ -8592,7 +8625,6 @@ exports.ParseError = ParseError; /* * Create and return a Big constructor. - * */ function _Big_() { @@ -8614,13 +8646,20 @@ exports.ParseError = ParseError; x.e = n.e; x.c = n.c.slice(); } else { + if (typeof n !== 'string') { + if (Big.strict === true && typeof n !== 'bigint') { + throw TypeError(INVALID + 'value'); + } + + // Minus zero? + n = n === 0 && 1 / n < 0 ? '-0' : String(n); + } + parse(x, n); } - /* - * Retain a reference to this Big constructor, and shadow Big.prototype.constructor which - * points to Object. - */ + // Retain a reference to this Big constructor. + // Shadow Big.prototype.constructor which points to Object. x.constructor = Big; } @@ -8629,7 +8668,11 @@ exports.ParseError = ParseError; Big.RM = RM; Big.NE = NE; Big.PE = PE; - Big.version = '5.2.2'; + Big.strict = STRICT; + Big.roundDown = 0; + Big.roundHalfUp = 1; + Big.roundHalfEven = 2; + Big.roundUp = 3; return Big; } @@ -8644,9 +8687,9 @@ exports.ParseError = ParseError; function parse(x, n) { var e, i, nl; - // Minus zero? - if (n === 0 && 1 / n < 0) n = '-0'; - else if (!NUMERIC.test(n += '')) throw Error(INVALID + 'number'); + if (!NUMERIC.test(n)) { + throw Error(INVALID + 'number'); + } // Determine sign. x.s = n.charAt(0) == '-' ? (n = n.slice(1), -1) : 1; @@ -8692,69 +8735,68 @@ exports.ParseError = ParseError; /* - * Round Big x to a maximum of dp decimal places using rounding mode rm. - * Called by stringify, P.div, P.round and P.sqrt. + * Round Big x to a maximum of sd significant digits using rounding mode rm. * * x {Big} The Big to round. - * dp {number} Integer, 0 to MAX_DP inclusive. - * rm {number} 0, 1, 2 or 3 (DOWN, HALF_UP, HALF_EVEN, UP) + * sd {number} Significant digits: integer, 0 to MAX_DP inclusive. + * rm {number} Rounding mode: 0 (down), 1 (half-up), 2 (half-even) or 3 (up). * [more] {boolean} Whether the result of division was truncated. */ - function round(x, dp, rm, more) { - var xc = x.c, - i = x.e + dp + 1; - - if (i < xc.length) { - if (rm === 1) { + function round(x, sd, rm, more) { + var xc = x.c; - // xc[i] is the digit after the digit that may be rounded up. - more = xc[i] >= 5; - } else if (rm === 2) { - more = xc[i] > 5 || xc[i] == 5 && - (more || i < 0 || xc[i + 1] !== UNDEFINED || xc[i - 1] & 1); - } else if (rm === 3) { - more = more || !!xc[0]; - } else { - more = false; - if (rm !== 0) throw Error(INVALID_RM); - } + if (rm === UNDEFINED) rm = x.constructor.RM; + if (rm !== 0 && rm !== 1 && rm !== 2 && rm !== 3) { + throw Error(INVALID_RM); + } - if (i < 1) { - xc.length = 1; + if (sd < 1) { + more = + rm === 3 && (more || !!xc[0]) || sd === 0 && ( + rm === 1 && xc[0] >= 5 || + rm === 2 && (xc[0] > 5 || xc[0] === 5 && (more || xc[1] !== UNDEFINED)) + ); - if (more) { + xc.length = 1; - // 1, 0.1, 0.01, 0.001, 0.0001 etc. - x.e = -dp; - xc[0] = 1; - } else { + if (more) { - // Zero. - xc[0] = x.e = 0; - } + // 1, 0.1, 0.01, 0.001, 0.0001 etc. + x.e = x.e - sd + 1; + xc[0] = 1; } else { - // Remove any digits after the required decimal places. - xc.length = i--; + // Zero. + xc[0] = x.e = 0; + } + } else if (sd < xc.length) { - // Round up? - if (more) { + // xc[sd] is the digit after the digit that may be rounded up. + more = + rm === 1 && xc[sd] >= 5 || + rm === 2 && (xc[sd] > 5 || xc[sd] === 5 && + (more || xc[sd + 1] !== UNDEFINED || xc[sd - 1] & 1)) || + rm === 3 && (more || !!xc[0]); - // Rounding up may mean the previous digit has to be rounded up. - for (; ++xc[i] > 9;) { - xc[i] = 0; - if (!i--) { - ++x.e; - xc.unshift(1); - } + // Remove any digits after the required precision. + xc.length = sd; + + // Round up? + if (more) { + + // Rounding up may mean the previous digit has to be rounded up. + for (; ++xc[--sd] > 9;) { + xc[sd] = 0; + if (sd === 0) { + ++x.e; + xc.unshift(1); + break; } } - - // Remove trailing zeros. - for (i = xc.length; !xc[--i];) xc.pop(); } - } else if (rm < 0 || rm > 3 || rm !== ~~rm) { - throw Error(INVALID_RM); + + // Remove trailing zeros. + for (sd = xc.length; !xc[--sd];) xc.pop(); } return x; @@ -8764,47 +8806,14 @@ exports.ParseError = ParseError; /* * Return a string representing the value of Big x in normal or exponential notation. * Handles P.toExponential, P.toFixed, P.toJSON, P.toPrecision, P.toString and P.valueOf. - * - * x {Big} - * id? {number} Caller id. - * 1 toExponential - * 2 toFixed - * 3 toPrecision - * 4 valueOf - * n? {number|undefined} Caller's argument. - * k? {number|undefined} */ - function stringify(x, id, n, k) { - var e, s, - Big = x.constructor, - z = !x.c[0]; - - if (n !== UNDEFINED) { - if (n !== ~~n || n < (id == 3) || n > MAX_DP) { - throw Error(id == 3 ? INVALID + 'precision' : INVALID_DP); - } - - x = new Big(x); - - // The index of the digit that may be rounded up. - n = k - x.e; - - // Round? - if (x.c.length > ++k) round(x, n, Big.RM); - - // toFixed: recalculate k as x.e may have changed if value rounded up. - if (id == 2) k = x.e + n + 1; - - // Append zeros? - for (; x.c.length < k;) x.c.push(0); - } - - e = x.e; - s = x.c.join(''); - n = s.length; + function stringify(x, doExponential, isNonzero) { + var e = x.e, + s = x.c.join(''), + n = s.length; // Exponential notation? - if (id != 2 && (id == 1 || id == 3 && k <= e || e <= Big.NE || e >= Big.PE)) { + if (doExponential) { s = s.charAt(0) + (n > 1 ? '.' + s.slice(1) : '') + (e < 0 ? 'e' : 'e+') + e; // Normal notation. @@ -8812,13 +8821,16 @@ exports.ParseError = ParseError; for (; ++e;) s = '0' + s; s = '0.' + s; } else if (e > 0) { - if (++e > n) for (e -= n; e--;) s += '0'; - else if (e < n) s = s.slice(0, e) + '.' + s.slice(e); + if (++e > n) { + for (e -= n; e--;) s += '0'; + } else if (e < n) { + s = s.slice(0, e) + '.' + s.slice(e); + } } else if (n > 1) { s = s.charAt(0) + '.' + s.slice(1); } - return x.s < 0 && (!z || id == 4) ? '-' + s : s; + return x.s < 0 && isNonzero ? '-' + s : s; } @@ -8839,7 +8851,7 @@ exports.ParseError = ParseError; * Return 1 if the value of this Big is greater than the value of Big y, * -1 if the value of this Big is less than the value of Big y, or * 0 if they have the same value. - */ + */ P.cmp = function (y) { var isneg, x = this, @@ -8885,13 +8897,21 @@ exports.ParseError = ParseError; k = x.s == y.s ? 1 : -1, dp = Big.DP; - if (dp !== ~~dp || dp < 0 || dp > MAX_DP) throw Error(INVALID_DP); + if (dp !== ~~dp || dp < 0 || dp > MAX_DP) { + throw Error(INVALID_DP); + } // Divisor is zero? - if (!b[0]) throw Error(DIV_BY_ZERO); + if (!b[0]) { + throw Error(DIV_BY_ZERO); + } // Dividend is 0? Return +-0. - if (!a[0]) return new Big(k * 0); + if (!a[0]) { + y.s = k; + y.c = [y.e = 0]; + return y; + } var bl, bt, n, cmp, ri, bz = b.slice(), @@ -8902,10 +8922,10 @@ exports.ParseError = ParseError; q = y, // quotient qc = q.c = [], qi = 0, - d = dp + (q.e = x.e - y.e) + 1; // number of digits of the result + p = dp + (q.e = x.e - y.e) + 1; // precision of the result q.s = k; - k = d < 0 ? 0 : d; + k = p < 0 ? 0 : p; // Create version of divisor with leading zero. bz.unshift(0); @@ -8966,10 +8986,11 @@ exports.ParseError = ParseError; // There can't be more than one zero. qc.shift(); q.e--; + p--; } // Round? - if (qi > d) round(q, dp, Big.RM, r[0] !== UNDEFINED); + if (qi > p) round(q, p, Big.RM, r[0] !== UNDEFINED); return q; }; @@ -8979,7 +9000,7 @@ exports.ParseError = ParseError; * Return true if the value of this Big is equal to the value of Big y, otherwise return false. */ P.eq = function (y) { - return !this.cmp(y); + return this.cmp(y) === 0; }; @@ -9041,9 +9062,14 @@ exports.ParseError = ParseError; // Either zero? if (!xc[0] || !yc[0]) { - - // y is non-zero? x is non-zero? Or both are zero. - return yc[0] ? (y.s = -b, y) : new Big(xc[0] ? x : 0); + if (yc[0]) { + y.s = -b; + } else if (xc[0]) { + y = new Big(x); + } else { + y.s = 1; + } + return y; } // Determine which is the bigger number. Prepend zeros to equalise exponents. @@ -9133,7 +9159,9 @@ exports.ParseError = ParseError; a = x.s, b = (y = new Big(y)).s; - if (!y.c[0]) throw Error(DIV_BY_ZERO); + if (!y.c[0]) { + throw Error(DIV_BY_ZERO); + } x.s = y.s = 1; ygtx = y.cmp(x) == 1; @@ -9151,21 +9179,31 @@ exports.ParseError = ParseError; return this.minus(x.times(y)); }; + + + /* + * Return a new Big whose value is the value of this Big negated. + */ + P.neg = function () { + var x = new this.constructor(this); + x.s = -x.s; + return x; + }; /* * Return a new Big whose value is the value of this Big plus the value of Big y. */ P.plus = P.add = function (y) { - var t, + var e, k, t, x = this, - Big = x.constructor, - a = x.s, - b = (y = new Big(y)).s; + Big = x.constructor; + + y = new Big(y); // Signs differ? - if (a != b) { - y.s = -b; + if (x.s != y.s) { + y.s = -y.s; return x.minus(y); } @@ -9174,24 +9212,33 @@ exports.ParseError = ParseError; ye = y.e, yc = y.c; - // Either zero? y is non-zero? x is non-zero? Or both are zero. - if (!xc[0] || !yc[0]) return yc[0] ? y : new Big(xc[0] ? x : a * 0); + // Either zero? + if (!xc[0] || !yc[0]) { + if (!yc[0]) { + if (xc[0]) { + y = new Big(x); + } else { + y.s = x.s; + } + } + return y; + } xc = xc.slice(); // Prepend zeros to equalise exponents. // Note: reverse faster than unshifts. - if (a = xe - ye) { - if (a > 0) { + if (e = xe - ye) { + if (e > 0) { ye = xe; t = yc; } else { - a = -a; + e = -e; t = xc; } t.reverse(); - for (; a--;) t.push(0); + for (; e--;) t.push(0); t.reverse(); } @@ -9202,20 +9249,20 @@ exports.ParseError = ParseError; xc = t; } - a = yc.length; + e = yc.length; // Only start adding at yc.length - 1 as the further digits of xc can be left as they are. - for (b = 0; a; xc[a] %= 10) b = (xc[--a] = xc[a] + yc[a] + b) / 10 | 0; + for (k = 0; e; xc[e] %= 10) k = (xc[--e] = xc[e] + yc[e] + k) / 10 | 0; // No need to check for zero, as +x + +y != 0 && -x + -y != 0 - if (b) { - xc.unshift(b); + if (k) { + xc.unshift(k); ++ye; } // Remove trailing zeros. - for (a = xc.length; xc[--a] === 0;) xc.pop(); + for (e = xc.length; xc[--e] === 0;) xc.pop(); y.c = xc; y.e = ye; @@ -9233,11 +9280,14 @@ exports.ParseError = ParseError; */ P.pow = function (n) { var x = this, - one = new x.constructor(1), + one = new x.constructor('1'), y = one, isneg = n < 0; - if (n !== ~~n || n < -MAX_POWER || n > MAX_POWER) throw Error(INVALID + 'exponent'); + if (n !== ~~n || n < -MAX_POWER || n > MAX_POWER) { + throw Error(INVALID + 'exponent'); + } + if (isneg) n = -n; for (;;) { @@ -9252,20 +9302,35 @@ exports.ParseError = ParseError; /* - * Return a new Big whose value is the value of this Big rounded using rounding mode rm - * to a maximum of dp decimal places, or, if dp is negative, to an integer which is a - * multiple of 10**-dp. + * Return a new Big whose value is the value of this Big rounded to a maximum precision of sd + * significant digits using rounding mode rm, or Big.RM if rm is not specified. + * + * sd {number} Significant digits: integer, 1 to MAX_DP inclusive. + * rm? {number} Rounding mode: 0 (down), 1 (half-up), 2 (half-even) or 3 (up). + */ + P.prec = function (sd, rm) { + if (sd !== ~~sd || sd < 1 || sd > MAX_DP) { + throw Error(INVALID + 'precision'); + } + return round(new this.constructor(this), sd, rm); + }; + + + /* + * Return a new Big whose value is the value of this Big rounded to a maximum of dp decimal places + * using rounding mode rm, or Big.RM if rm is not specified. + * If dp is negative, round to an integer which is a multiple of 10**-dp. * If dp is not specified, round to 0 decimal places. - * If rm is not specified, use Big.RM. * * dp? {number} Integer, -MAX_DP to MAX_DP inclusive. - * rm? 0, 1, 2 or 3 (ROUND_DOWN, ROUND_HALF_UP, ROUND_HALF_EVEN, ROUND_UP) + * rm? {number} Rounding mode: 0 (down), 1 (half-up), 2 (half-even) or 3 (up). */ P.round = function (dp, rm) { - var Big = this.constructor; if (dp === UNDEFINED) dp = 0; - else if (dp !== ~~dp || dp < -MAX_DP || dp > MAX_DP) throw Error(INVALID_DP); - return round(new Big(this), dp, rm === UNDEFINED ? Big.RM : rm); + else if (dp !== ~~dp || dp < -MAX_DP || dp > MAX_DP) { + throw Error(INVALID_DP); + } + return round(new this.constructor(this), dp + this.e + 1, rm); }; @@ -9279,13 +9344,15 @@ exports.ParseError = ParseError; Big = x.constructor, s = x.s, e = x.e, - half = new Big(0.5); + half = new Big('0.5'); // Zero? if (!x.c[0]) return new Big(x); // Negative? - if (s < 0) throw Error(NAME + 'No square root'); + if (s < 0) { + throw Error(NAME + 'No square root'); + } // Estimate. s = Math.sqrt(x + ''); @@ -9297,9 +9364,9 @@ exports.ParseError = ParseError; if (!(c.length + e & 1)) c += '0'; s = Math.sqrt(c); e = ((e + 1) / 2 | 0) - (e < 0 || e & 1); - r = new Big((s == 1 / 0 ? '1e' : (s = s.toExponential()).slice(0, s.indexOf('e') + 1)) + e); + r = new Big((s == 1 / 0 ? '5e' : (s = s.toExponential()).slice(0, s.indexOf('e') + 1)) + e); } else { - r = new Big(s); + r = new Big(s + ''); } e = r.e + (Big.DP += 4); @@ -9310,7 +9377,7 @@ exports.ParseError = ParseError; r = half.times(t.plus(x.div(t))); } while (t.c.slice(0, e).join('') !== r.c.slice(0, e).join('')); - return round(r, Big.DP -= 4, Big.RM); + return round(r, (Big.DP -= 4) + r.e + 1, Big.RM); }; @@ -9332,7 +9399,10 @@ exports.ParseError = ParseError; y.s = x.s == y.s ? 1 : -1; // Return signed 0 if either 0. - if (!xc[0] || !yc[0]) return new Big(y.s * 0); + if (!xc[0] || !yc[0]) { + y.c = [y.e = 0]; + return y; + } // Initialise exponent of result as x.e + y.e. y.e = i + j; @@ -9367,7 +9437,7 @@ exports.ParseError = ParseError; b = b / 10 | 0; } - c[j] = (c[j] + b) % 10; + c[j] = b; } // Increment result exponent if there is a final carry, otherwise remove leading zero. @@ -9383,39 +9453,53 @@ exports.ParseError = ParseError; /* - * Return a string representing the value of this Big in exponential notation to dp fixed decimal - * places and rounded using Big.RM. + * Return a string representing the value of this Big in exponential notation rounded to dp fixed + * decimal places using rounding mode rm, or Big.RM if rm is not specified. * - * dp? {number} Integer, 0 to MAX_DP inclusive. + * dp? {number} Decimal places: integer, 0 to MAX_DP inclusive. + * rm? {number} Rounding mode: 0 (down), 1 (half-up), 2 (half-even) or 3 (up). */ - P.toExponential = function (dp) { - return stringify(this, 1, dp, dp); + P.toExponential = function (dp, rm) { + var x = this, + n = x.c[0]; + + if (dp !== UNDEFINED) { + if (dp !== ~~dp || dp < 0 || dp > MAX_DP) { + throw Error(INVALID_DP); + } + x = round(new x.constructor(x), ++dp, rm); + for (; x.c.length < dp;) x.c.push(0); + } + + return stringify(x, true, !!n); }; /* - * Return a string representing the value of this Big in normal notation to dp fixed decimal - * places and rounded using Big.RM. + * Return a string representing the value of this Big in normal notation rounded to dp fixed + * decimal places using rounding mode rm, or Big.RM if rm is not specified. * - * dp? {number} Integer, 0 to MAX_DP inclusive. + * dp? {number} Decimal places: integer, 0 to MAX_DP inclusive. + * rm? {number} Rounding mode: 0 (down), 1 (half-up), 2 (half-even) or 3 (up). * * (-0).toFixed(0) is '0', but (-0.1).toFixed(0) is '-0'. * (-0).toFixed(1) is '0.0', but (-0.01).toFixed(1) is '-0.0'. */ - P.toFixed = function (dp) { - return stringify(this, 2, dp, this.e + dp); - }; + P.toFixed = function (dp, rm) { + var x = this, + n = x.c[0]; + if (dp !== UNDEFINED) { + if (dp !== ~~dp || dp < 0 || dp > MAX_DP) { + throw Error(INVALID_DP); + } + x = round(new x.constructor(x), dp + x.e + 1, rm); - /* - * Return a string representing the value of this Big rounded to sd significant digits using - * Big.RM. Use exponential notation if sd is less than the number of digits necessary to represent - * the integer part of the value in normal notation. - * - * sd {number} Integer, 1 to MAX_DP inclusive. - */ - P.toPrecision = function (sd) { - return stringify(this, 3, sd, sd - 1); + // x.e may have changed if the value is rounded up. + for (dp = dp + x.e + 1; x.c.length < dp;) x.c.push(0); + } + + return stringify(x, false, !!n); }; @@ -9425,8 +9509,48 @@ exports.ParseError = ParseError; * Big.PE, or a negative exponent equal to or less than Big.NE. * Omit the sign for negative zero. */ - P.toString = function () { - return stringify(this); + P.toJSON = P.toString = function () { + var x = this, + Big = x.constructor; + return stringify(x, x.e <= Big.NE || x.e >= Big.PE, !!x.c[0]); + }; + + + /* + * Return the value of this Big as a primitve number. + */ + P.toNumber = function () { + var n = Number(stringify(this, true, true)); + if (this.constructor.strict === true && !this.eq(n.toString())) { + throw Error(NAME + 'Imprecise conversion'); + } + return n; + }; + + + /* + * Return a string representing the value of this Big rounded to sd significant digits using + * rounding mode rm, or Big.RM if rm is not specified. + * Use exponential notation if sd is less than the number of digits necessary to represent + * the integer part of the value in normal notation. + * + * sd {number} Significant digits: integer, 1 to MAX_DP inclusive. + * rm? {number} Rounding mode: 0 (down), 1 (half-up), 2 (half-even) or 3 (up). + */ + P.toPrecision = function (sd, rm) { + var x = this, + Big = x.constructor, + n = x.c[0]; + + if (sd !== UNDEFINED) { + if (sd !== ~~sd || sd < 1 || sd > MAX_DP) { + throw Error(INVALID + 'precision'); + } + x = round(new Big(x), sd, rm); + for (; x.c.length < sd;) x.c.push(0); + } + + return stringify(x, sd <= x.e || x.e <= Big.NE || x.e >= Big.PE, !!n); }; @@ -9436,8 +9560,13 @@ exports.ParseError = ParseError; * Big.PE, or a negative exponent equal to or less than Big.NE. * Include the sign for negative zero. */ - P.valueOf = P.toJSON = function () { - return stringify(this, 4); + P.valueOf = function () { + var x = this, + Big = x.constructor; + if (Big.strict === true) { + throw Error(NAME + 'valueOf disallowed'); + } + return stringify(x, x.e <= Big.NE || x.e >= Big.PE, true); }; @@ -9505,7 +9634,7 @@ var patterns = { timezone: /([Z+-].*)$/, timezoneZ: /^(Z)$/, timezoneHH: /^([+-]\d{2})$/, - timezoneHHMM: /^([+-]\d{2}):?(\d{2})$/ + timezoneHHMM: /^([+-])(\d{2}):?(\d{2})$/ }; // Parse various time zone offset formats to an offset in milliseconds function tzParseTimezone(timezoneString, date, isUtcDate) { @@ -9541,15 +9670,15 @@ function tzParseTimezone(timezoneString, date, isUtcDate) { token = patterns.timezoneHHMM.exec(timezoneString); if (token) { - hours = parseInt(token[1], 10); - var minutes = parseInt(token[2], 10); + hours = parseInt(token[2], 10); + var minutes = parseInt(token[3], 10); if (!validateTimezone(hours, minutes)) { return NaN; } absoluteOffset = Math.abs(hours) * MILLISECONDS_IN_HOUR + minutes * MILLISECONDS_IN_MINUTE; - return hours > 0 ? -absoluteOffset : absoluteOffset; + return token[1] === '+' ? -absoluteOffset : absoluteOffset; } // IANA time zone @@ -9631,9 +9760,9 @@ Object.defineProperty(exports, "__esModule", { }); exports.default = tzTokenizeDate; -/** - * Returns the [year, month, day, hour, minute, seconds] tokens of the provided - * `date` as it will be rendered in the `timeZone`. +/** + * Returns the [year, month, day, hour, minute, seconds] tokens of the provided + * `date` as it will be rendered in the `timeZone`. */ function tzTokenizeDate(date, timeZone) { var dtf = getDateTimeFormat(timeZone); @@ -9673,7 +9802,7 @@ function partsOffset(dtf, date) { } function hackyOffset(dtf, date) { - var formatted = dtf.format(date).replace(/\u200E/g, ''); + var formatted = dtf.format(date); var parsed = /(\d+)\/(\d+)\/(\d+),? (\d+):(\d+):(\d+)/.exec(formatted); // var [, fMonth, fDay, fYear, fHour, fMinute, fSecond] = parsed // return [fYear, fMonth, fDay, fHour, fMinute, fSecond] @@ -9689,10 +9818,10 @@ function getDateTimeFormat(timeZone) { if (!dtfCache[timeZone]) { // New browsers use `hourCycle`, IE and Chrome <73 does not support it and uses `hour12` var testDateFormatted = new Intl.DateTimeFormat('en-US', { - hour12: false, + hourCycle: 'h23', timeZone: 'America/New_York', year: 'numeric', - month: 'numeric', + month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', @@ -9700,7 +9829,7 @@ function getDateTimeFormat(timeZone) { }).format(new Date('2014-06-25T04:00:00.123Z')); var hourCycleSupported = testDateFormatted === '06/25/2014, 00:00:00' || testDateFormatted === '‎06‎/‎25‎/‎2014‎ ‎00‎:‎00‎:‎00'; dtfCache[timeZone] = hourCycleSupported ? new Intl.DateTimeFormat('en-US', { - hour12: false, + hourCycle: 'h23', timeZone: timeZone, year: 'numeric', month: 'numeric', @@ -9709,7 +9838,7 @@ function getDateTimeFormat(timeZone) { minute: '2-digit', second: '2-digit' }) : new Intl.DateTimeFormat('en-US', { - hourCycle: 'h23', + hour12: false, timeZone: timeZone, year: 'numeric', month: 'numeric', @@ -15689,7 +15818,6 @@ describe('When checking to see if a symbol is crypto', () => { it('the symbol "^EURUSD" should return true', () => { expect(SymbolParser.getIsCrypto('^EURUSD')).toEqual(true); // should return false ... }); - it('the symbol "^BTCUSDT" should return true', () => { expect(SymbolParser.getIsCrypto('^BTCUSDT')).toEqual(true); });