refactor: replace ignored numeric literals with constants
This commit is contained in:
@@ -2,10 +2,16 @@ import type { Money } from '../../../../common/money.types';
|
||||
|
||||
import {
|
||||
add,
|
||||
DECIMAL_RADIX,
|
||||
decimalFromString,
|
||||
formatDecimal,
|
||||
} from '../../../../common/decimal';
|
||||
|
||||
const ZERO_AMOUNT = 0n;
|
||||
const INITIAL_UNVALUED_COUNT = 0;
|
||||
const POSITION_INCREMENT = 1;
|
||||
const ROUNDING_DIVISOR = 2n;
|
||||
|
||||
const SHARE_PERCENT_DECIMAL_PLACES = 6;
|
||||
const PERCENT_SCALED_FACTOR = 100_000_000n;
|
||||
|
||||
@@ -33,7 +39,7 @@ export function portfolioStructure(
|
||||
const value = decimalFromString(money.value);
|
||||
// eslint-disable-next-line security/detect-object-injection -- The numeric index belongs to the fixed portfolio category array.
|
||||
group.values[index] = add(group.values[index], value);
|
||||
group.hasNegativeValues ||= value.value < 0n;
|
||||
group.hasNegativeValues ||= value.value < ZERO_AMOUNT;
|
||||
currencies.set(money.currency, group);
|
||||
};
|
||||
for (const position of positions) {
|
||||
@@ -41,7 +47,7 @@ export function portfolioStructure(
|
||||
position.type :
|
||||
'OTHER';
|
||||
if (position.currentValue) include(type, position.currentValue);
|
||||
else unvalued.set(type, (unvalued.get(type) ?? 0) + 1);
|
||||
else unvalued.set(type, (unvalued.get(type) ?? INITIAL_UNVALUED_COUNT) + POSITION_INCREMENT);
|
||||
}
|
||||
for (const amount of cash) include('CASH', amount);
|
||||
|
||||
@@ -52,7 +58,7 @@ export function portfolioStructure(
|
||||
.map(([currency, group]) => {
|
||||
const total = group.values.reduce(add, decimalFromString('0'));
|
||||
const canCalculateShares =
|
||||
!unvalued.size && !group.hasNegativeValues && total.value > 0n;
|
||||
!unvalued.size && !group.hasNegativeValues && total.value > ZERO_AMOUNT;
|
||||
const categories = group.values.map((value, index) => ({
|
||||
// eslint-disable-next-line security/detect-object-injection -- The numeric index belongs to the fixed portfolio category array.
|
||||
type: categoryTypes[index],
|
||||
@@ -63,9 +69,9 @@ export function portfolioStructure(
|
||||
formatDecimal({
|
||||
value:
|
||||
(value.value *
|
||||
10n ** BigInt(total.scale - value.scale) *
|
||||
DECIMAL_RADIX ** BigInt(total.scale - value.scale) *
|
||||
PERCENT_SCALED_FACTOR +
|
||||
total.value / 2n) /
|
||||
total.value / ROUNDING_DIVISOR) /
|
||||
total.value,
|
||||
scale: SHARE_PERCENT_DECIMAL_PLACES,
|
||||
}) :
|
||||
|
||||
Reference in New Issue
Block a user