Skip to content

Commit

Permalink
fix: use equal function to compare currency base and exponent
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahdayan committed Aug 7, 2021
1 parent 5c8784c commit ac4724f
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions packages/core/src/api/haveSameCurrency.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
import type { Dinero } from '../types';
import type { Currency } from '@dinero.js/currencies';
import { equal } from '../utils';

function currencyEqual<TAmount>(
subject: Currency<TAmount>,
comparator: Currency<TAmount>
) {
return (
subject.code === comparator.code &&
subject.base === comparator.base &&
subject.exponent === comparator.exponent
);
}
import type { Dinero } from '../types';

export function haveSameCurrency<TAmount>(
dineroObjects: ReadonlyArray<Dinero<TAmount>>
) {
const [firstDinero, ...otherDineros] = dineroObjects;
const { currency: comparator } = firstDinero.toJSON();
const equalFn = equal(firstDinero.calculator);

return otherDineros.every((d) => {
const { currency: subject } = d.toJSON();

return currencyEqual(subject, comparator);
return (
subject.code === comparator.code &&
equalFn(subject.base, comparator.base) &&
equalFn(subject.exponent, comparator.exponent)
);
});
}

0 comments on commit ac4724f

Please sign in to comment.