mirror of https://github.com/ghostfolio/ghostfolio
committed by
Sebastian Legarraga
5 changed files with 79 additions and 1 deletions
@ -0,0 +1,51 @@ |
|||
import { AssetSubClass } from '@ghostfolio/prisma/enums'; |
|||
|
|||
import { HoldingsTableItem } from './interfaces/interfaces'; |
|||
import { getHoldingQuantityPrecision } from './quantity-precision'; |
|||
|
|||
function createHolding( |
|||
quantity: number | undefined, |
|||
assetSubClass: AssetSubClass |
|||
): Pick<HoldingsTableItem, 'assetProfile' | 'quantity'> { |
|||
return { |
|||
assetProfile: { assetSubClass } as HoldingsTableItem['assetProfile'], |
|||
quantity |
|||
}; |
|||
} |
|||
|
|||
describe('getHoldingQuantityPrecision', () => { |
|||
it('preserves satoshi precision for fractional cryptocurrency quantities', () => { |
|||
expect( |
|||
getHoldingQuantityPrecision( |
|||
createHolding(0.01525217, AssetSubClass.CRYPTOCURRENCY) |
|||
) |
|||
).toBe(8); |
|||
expect( |
|||
getHoldingQuantityPrecision( |
|||
createHolding(10.12345678, AssetSubClass.CRYPTOCURRENCY) |
|||
) |
|||
).toBe(8); |
|||
}); |
|||
|
|||
it('does not add decimal places to integer cryptocurrency quantities', () => { |
|||
expect( |
|||
getHoldingQuantityPrecision( |
|||
createHolding(12, AssetSubClass.CRYPTOCURRENCY) |
|||
) |
|||
).toBe(0); |
|||
}); |
|||
|
|||
it('keeps the existing precision for non-cryptocurrency quantities', () => { |
|||
expect( |
|||
getHoldingQuantityPrecision(createHolding(12.3456, AssetSubClass.STOCK)) |
|||
).toBe(2); |
|||
}); |
|||
|
|||
it('does not add decimal places when the quantity is not available', () => { |
|||
expect( |
|||
getHoldingQuantityPrecision( |
|||
createHolding(undefined, AssetSubClass.CRYPTOCURRENCY) |
|||
) |
|||
).toBe(0); |
|||
}); |
|||
}); |
|||
@ -0,0 +1,18 @@ |
|||
import { AssetSubClass } from '@ghostfolio/prisma/enums'; |
|||
|
|||
import { HoldingsTableItem } from './interfaces/interfaces'; |
|||
|
|||
export function getHoldingQuantityPrecision({ |
|||
assetProfile, |
|||
quantity |
|||
}: Pick<HoldingsTableItem, 'assetProfile' | 'quantity'>): number { |
|||
if (quantity === undefined || Number.isInteger(quantity)) { |
|||
return 0; |
|||
} |
|||
|
|||
if (assetProfile.assetSubClass === AssetSubClass.CRYPTOCURRENCY) { |
|||
return 8; |
|||
} |
|||
|
|||
return 2; |
|||
} |
|||
Loading…
Reference in new issue