diff --git a/.eslintrc.json b/.eslintrc.json index a3ccd87..a3d3ea1 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -45,6 +45,7 @@ "ts-expect-error": "allow-with-description" } ], - "@typescript-eslint/no-explicit-any" : 0 + "@typescript-eslint/no-explicit-any" : 0, + "@typescript-eslint/no-this-alias": 0 } -} \ No newline at end of file +} diff --git a/CHANGELOG.md b/CHANGELOG.md index a644503..80fa6ae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,12 @@ Changelog ========= +1.2.0 (2020-08-01) +------------------ + +* Added `pow()` operation. + + 1.1.1 (2020-02-02) ------------------ diff --git a/readme.md b/readme.md index b78e0c2..1439d7c 100644 --- a/readme.md +++ b/readme.md @@ -118,6 +118,9 @@ const result = new Money(10).divide(3); // Multiplication const result = new Money('2000').multiply('1.25'); + +// Powers +const result = new Money(2).pow(8); ``` ### Comparing objects diff --git a/src/money.ts b/src/money.ts index 44d36cd..c22ce11 100644 --- a/src/money.ts +++ b/src/money.ts @@ -110,6 +110,37 @@ export class Money { } + /** + * Pow returns the current value to it's exponent. + * + * pow currently only supports whole numbers. + */ + pow(exponent: number | bigint): Money { + + if (typeof exponent === 'number' && !Number.isInteger(exponent)) { + throw new Error('You can currently only use pow() with whole numbers'); + } + + if (exponent > 1) { + const resultBig = this.value ** BigInt(exponent); + return Money.fromSource( + divide(resultBig, PRECISION_M ** (BigInt(exponent)-1n), this.round), + this.currency, + this.round + ); + } else { + // This handles the 0, 1 and negative exponent cases. + // This uses an iterative approach and is therefore not going to super + // fast. + let base:Money = this; + for(let i = 1; i > exponent; i--) { + base = base.divide(this); + } + return base; + } + + } + /** * Returns the absolute value. */ diff --git a/test/money.ts b/test/money.ts index 797a696..1577e65 100644 --- a/test/money.ts +++ b/test/money.ts @@ -287,6 +287,31 @@ describe('Money class', () => { }); + describe('pow', () => { + + const cases: [string|number, number, string][] = [ + ['1', 5,'1.00'], + ['2', 8, '256.00'], + [5, 3, '125.00'], + [-10, 5, '-100000.00'], + [-10, 1, '-10.00'], + [-10, 0, '1.00'], + [2, -2, '0.25'], + ]; + + for (const cas of cases) { + + it(`${cas[0]} ** ${cas[1]} = ${cas[2]}`, () => { + + const x = new Money(cas[0], 'CAD'); + expect(x.pow(cas[1]).toFixed(2)).to.equal(cas[2]); + + }); + + } + + }); + describe('abs', () => { const cases = [