Skip to content

Commit

Permalink
Release. Bump version number
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor-Losev committed Sep 6, 2023
1 parent 4c337c9 commit 0895826
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
115 changes: 111 additions & 4 deletions test/SpecRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -2889,7 +2889,7 @@ module.exports = (() => {
* provided.
*
* @public
* @param {Decimal|Number|String} exponent
* @param {Number} exponent
* @returns {Decimal}
*/
raise(exponent) {
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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}`;
}

/**
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -5072,7 +5102,7 @@ module.exports = (() => {
*
* @static
* @public
* @param {*} candidate {*}
* @param {*} candidate
* @returns {boolean}
*/
number(candidate) {
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit 0895826

Please sign in to comment.