Skip to content

Commit

Permalink
refactor: use branches over default transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahdayan committed Dec 3, 2022
1 parent b0ec1ec commit 2bf6376
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 28 deletions.
4 changes: 2 additions & 2 deletions packages/core/etc/core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ subtrahend: Dinero<TAmount>
];

// @public (undocumented)
export function toDecimal<TAmount, TOutput>(calculator: Calculator<TAmount>): (dineroObject: Dinero<TAmount>, transformer?: Transformer_2<TAmount, TOutput, string> | undefined) => TOutput;
export function toDecimal<TAmount, TOutput>(calculator: Calculator<TAmount>): (dineroObject: Dinero<TAmount>, transformer?: Transformer_2<TAmount, TOutput, string> | undefined) => string | TOutput;

// @public (undocumented)
export type ToDecimalParams<TAmount, TOutput> = readonly [
Expand All @@ -377,7 +377,7 @@ transformer?: Transformer_2<TAmount, TOutput, string>
export function toSnapshot<TAmount>(dineroObject: Dinero<TAmount>): DineroSnapshot<TAmount>;

// @public (undocumented)
export function toUnits<TAmount, TOutput>(calculator: Calculator<TAmount>): (dineroObject: Dinero<TAmount>, transformer?: Transformer_2<TAmount, TOutput, readonly TAmount[]> | undefined) => TOutput;
export function toUnits<TAmount, TOutput>(calculator: Calculator<TAmount>): (dineroObject: Dinero<TAmount>, transformer?: Transformer_2<TAmount, TOutput, readonly TAmount[]> | undefined) => TOutput | readonly TAmount[];

// @public (undocumented)
export type ToUnitsParams<TAmount, TOutput> = readonly [
Expand Down
9 changes: 5 additions & 4 deletions packages/core/src/api/toDecimal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ export function toDecimal<TAmount, TOutput>(calculator: Calculator<TAmount>) {
const equalFn = equal(calculator);

return function toDecimalFn(
...[
dineroObject,
transformer = ({ value }) => value as TOutput,
]: ToDecimalParams<TAmount, TOutput>
...[dineroObject, transformer]: ToDecimalParams<TAmount, TOutput>
) {
const { currency, scale } = dineroObject.toJSON();

Expand All @@ -39,6 +36,10 @@ export function toDecimal<TAmount, TOutput>(calculator: Calculator<TAmount>) {
const getDecimalFn = getDecimal(dineroObject.formatter);
const value = getDecimalFn(units, scale);

if (!transformer) {
return value;
}

return transformer({ value, currency });
};
}
Expand Down
39 changes: 17 additions & 22 deletions packages/core/src/api/toUnits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,29 @@ export function toUnits<TAmount, TOutput>(calculator: Calculator<TAmount>) {
const getDivisorsFn = getDivisors(calculator);

return function toUnitsFn(
...[
dineroObject,
transformer = ({ value }) => value as TOutput,
]: ToUnitsParams<TAmount, TOutput>
...[dineroObject, transformer]: ToUnitsParams<TAmount, TOutput>
) {
const { amount, currency, scale } = dineroObject.toJSON();
const { power, integerDivide, modulo } = calculator;

const bases = isArray(currency.base) ? currency.base : [currency.base];
const divisors = getDivisorsFn(bases.map((base) => power(base, scale)));
const value = divisors.reduce<readonly TAmount[]>(
(amounts, divisor, index) => {
const amountLeft = amounts[index];

return transformer({
value: divisors.reduce<readonly TAmount[]>(
(amounts, divisor, index) => {
const amountLeft = amounts[index];

const quotient = integerDivide(amountLeft, divisor);
const remainder = modulo(amountLeft, divisor);

return [
...amounts.filter((_, i) => i !== index),
quotient,
remainder,
];
},
[amount]
),
currency,
});
const quotient = integerDivide(amountLeft, divisor);
const remainder = modulo(amountLeft, divisor);

return [...amounts.filter((_, i) => i !== index), quotient, remainder];
},
[amount]
);

if (!transformer) {
return value;
}

return transformer({ value, currency });
};
}

0 comments on commit 2bf6376

Please sign in to comment.