From 0895826b419eeb0df5d062355c75a3a345f92756 Mon Sep 17 00:00:00 2001 From: Igor Losev Date: Wed, 6 Sep 2023 12:24:59 +0200 Subject: [PATCH] Release. Bump version number --- package.json | 2 +- test/SpecRunner.js | 115 +++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 112 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 0223bf2..ff89611 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@barchart/common-js", - "version": "4.31.0", + "version": "4.32.0", "description": "Library of common JavaScript utilities", "author": { "name": "Bryan Ingle", diff --git a/test/SpecRunner.js b/test/SpecRunner.js index 6a66835..a2d0c52 100644 --- a/test/SpecRunner.js +++ b/test/SpecRunner.js @@ -2889,7 +2889,7 @@ module.exports = (() => { * provided. * * @public - * @param {Decimal|Number|String} exponent + * @param {Number} exponent * @returns {Decimal} */ raise(exponent) { @@ -3690,6 +3690,16 @@ module.exports = (() => { this._denominator = denominator; } + /** + * An array of constant exchange rates. + * + * @static + * @returns {Rate[]} + */ + static get CONSTANTS() { + return [Rate.fromPair(0.01, '^GBXGBP')]; + } + /** * The rate. * @@ -3764,7 +3774,7 @@ module.exports = (() => { */ formatPair(useCarat) { assert.argumentIsOptional(useCarat, 'useCarat', Boolean); - return `${useCarat ? '^' : ''}${this._numerator}${this._denominator}`; + return `${useCarat ? '^' : ''}${this._numerator.code}${this._denominator.code}`; } /** @@ -3804,7 +3814,27 @@ module.exports = (() => { } else { const numerator = desiredCurrency; const denominator = currency; - let rate = rates.find(r => r.numerator === numerator && r.denominator === denominator || r.numerator === denominator && r.denominator === numerator); + const staticRates = []; + Rate.CONSTANTS.forEach(staticRate => { + staticRates.push(staticRate); + const staticRateInverted = staticRate.invert(); + rates.forEach(rate => { + [staticRate, staticRateInverted].forEach(r => { + let indirect; + if (rate.numerator === r.denominator) { + indirect = new Rate(rate.decimal.multiply(r.decimal), r.numerator, rate.denominator); + } else if (rate.denominator === r.denominator) { + indirect = new Rate(rate.decimal.divide(r.decimal), rate.numerator, r.numerator); + } else { + indirect = null; + } + if (indirect !== null) { + staticRates.push(indirect); + } + }); + }); + }); + let rate = rates.concat(staticRates).find(r => r.numerator === numerator && r.denominator === denominator || r.numerator === denominator && r.denominator === numerator); if (rate) { if (rate.numerator === denominator) { rate = rate.invert(); @@ -5072,7 +5102,7 @@ module.exports = (() => { * * @static * @public - * @param {*} candidate {*} + * @param {*} candidate * @returns {boolean} */ number(candidate) { @@ -19923,6 +19953,83 @@ describe('When parsing an "^USDEUR" rate of 0.8333', () => { }); }); }); +describe('When parsing a "^GBPUSD" rate of 1.25882', () => { + 'use strict'; + + let rate; + beforeEach(() => { + rate = Rate.fromPair(1.25882, '^GBPUSD'); + }); + it('the quote currency should be USD', () => { + expect(rate.quote.code).toEqual('USD'); + }); + it('the base currency should be GBP', () => { + expect(rate.base.code).toEqual('GBP'); + }); + it('the numerator currency should be USD', () => { + expect(rate.numerator.code).toEqual('USD'); + }); + it('the denominator currency should be GBP', () => { + expect(rate.denominator.code).toEqual('GBP'); + }); + it('the value should be 1.25882', () => { + expect(rate.decimal.getIsEqual(1.25882)).toEqual(true); + }); + describe('When converting 10 GBP to USD', () => { + it('should be 12.59 USD', () => { + expect(Rate.convert(new Decimal(10), Currency.GBP, Currency.USD, rate).round(2).getIsEqual(12.59)).toEqual(true); + }); + }); + describe('When converting 1000 GBX to USD', () => { + it('should be 12.59 USD', () => { + expect(Rate.convert(new Decimal(1000), Currency.GBX, Currency.USD, rate).round(2).getIsEqual(12.59)).toEqual(true); + }); + }); + describe('When converting 1 USD to GBX', () => { + it('should be 79.44 USD', () => { + expect(Rate.convert(new Decimal(1), Currency.USD, Currency.GBX, rate).round(2).getIsEqual(79.44)).toEqual(true); + }); + }); + it('1 GBP should be 100 GBX', () => { + expect(Rate.convert(Decimal.ONE, Currency.GBP, Currency.GBX).round(2).getIsEqual(100)).toEqual(true); + }); + it('1 GBX should be 0.01 GBP', () => { + expect(Rate.convert(Decimal.ONE, Currency.GBX, Currency.GBP).round(2).getIsEqual(0.01)).toEqual(true); + }); +}); +describe('When parsing a "^EURGBX" rate of 85.503', () => { + 'use strict'; + + let rate; + beforeEach(() => { + rate = Rate.fromPair(85.503, '^EURGBX'); + }); + it('the quote currency should be GBX', () => { + expect(rate.quote.code).toEqual('GBX'); + }); + it('the base currency should be EUR', () => { + expect(rate.base.code).toEqual('EUR'); + }); + it('the numerator currency should be GBX', () => { + expect(rate.numerator.code).toEqual('GBX'); + }); + it('the denominator currency should be EUR', () => { + expect(rate.denominator.code).toEqual('EUR'); + }); + it('the value should be 85.503', () => { + expect(rate.decimal.getIsEqual(85.503)).toEqual(true); + }); + describe('When converting 100 GBX to EUR', () => { + it('should be 1.17 EUR', () => { + expect(Rate.convert(new Decimal(100), Currency.GBX, Currency.EUR, rate).round(2).getIsEqual(1.17)).toEqual(true); + }); + }); + describe('When converting 1 GBP to EUR', () => { + it('should be 1.17 EUR', () => { + expect(Rate.convert(new Decimal(1), Currency.GBP, Currency.EUR, rate).round(2).getIsEqual(1.17)).toEqual(true); + }); + }); +}); },{"./../../../lang/Currency":20,"./../../../lang/Decimal":22,"./../../../lang/Rate":26}],112:[function(require,module,exports){ const Timestamp = require('./../../../lang/Timestamp');