@cranberry-money/shared-utils 1.0.1 → 3.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/portfolio/index.d.ts +16 -0
- package/dist/portfolio/index.d.ts.map +1 -0
- package/dist/portfolio/index.js +44 -0
- package/package.json +3 -2
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,cAAc,cAAc,CAAC;AAC7B,cAAc,WAAW,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,cAAc,cAAc,CAAC;AAC7B,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Portfolio utility functions
|
|
3
|
+
* Calculations and processing for portfolio data
|
|
4
|
+
*/
|
|
5
|
+
import type { PortfolioServices } from '@cranberry-money/shared-services';
|
|
6
|
+
/**
|
|
7
|
+
* Calculate the actual percentage allocations based on holdings
|
|
8
|
+
* Returns a map of instrument UUID to percentage
|
|
9
|
+
*/
|
|
10
|
+
export declare const calculateHoldingAllocations: (holdings: PortfolioServices.AssetHolding[]) => Record<string, number>;
|
|
11
|
+
/**
|
|
12
|
+
* Format quantity for display
|
|
13
|
+
* Handles fractional shares and whole numbers appropriately
|
|
14
|
+
*/
|
|
15
|
+
export declare const formatQuantity: (quantity?: number) => string;
|
|
16
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/portfolio/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,kCAAkC,CAAC;AAE1E;;;GAGG;AACH,eAAO,MAAM,2BAA2B,GAAI,UAAU,iBAAiB,CAAC,YAAY,EAAE,KAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAwB7G,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,cAAc,GAAI,WAAW,MAAM,KAAG,MAWlD,CAAC"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Portfolio utility functions
|
|
3
|
+
* Calculations and processing for portfolio data
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Calculate the actual percentage allocations based on holdings
|
|
7
|
+
* Returns a map of instrument UUID to percentage
|
|
8
|
+
*/
|
|
9
|
+
export const calculateHoldingAllocations = (holdings) => {
|
|
10
|
+
// Calculate total portfolio value
|
|
11
|
+
const totalValue = holdings.reduce((sum, holding) => {
|
|
12
|
+
const price = parseFloat(holding.instrument.currentPrice || '0');
|
|
13
|
+
const value = holding.quantity * price;
|
|
14
|
+
return sum + value;
|
|
15
|
+
}, 0);
|
|
16
|
+
// If no value, return empty allocations
|
|
17
|
+
if (totalValue === 0) {
|
|
18
|
+
return {};
|
|
19
|
+
}
|
|
20
|
+
// Calculate percentage for each holding
|
|
21
|
+
const allocations = {};
|
|
22
|
+
holdings.forEach((holding) => {
|
|
23
|
+
const price = parseFloat(holding.instrument.currentPrice || '0');
|
|
24
|
+
const value = holding.quantity * price;
|
|
25
|
+
const percentage = (value / totalValue) * 100;
|
|
26
|
+
allocations[holding.instrument.uuid] = percentage;
|
|
27
|
+
});
|
|
28
|
+
return allocations;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Format quantity for display
|
|
32
|
+
* Handles fractional shares and whole numbers appropriately
|
|
33
|
+
*/
|
|
34
|
+
export const formatQuantity = (quantity) => {
|
|
35
|
+
if (quantity === undefined || quantity === null)
|
|
36
|
+
return '0';
|
|
37
|
+
// For whole numbers, show without decimals
|
|
38
|
+
if (Number.isInteger(quantity)) {
|
|
39
|
+
return quantity.toString();
|
|
40
|
+
}
|
|
41
|
+
// For fractional shares, show up to 6 decimal places
|
|
42
|
+
// Remove trailing zeros
|
|
43
|
+
return quantity.toFixed(6).replace(/\.?0+$/, '');
|
|
44
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cranberry-money/shared-utils",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.1",
|
|
4
4
|
"description": "Shared utility functions for MyPortfolio platform",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -21,7 +21,8 @@
|
|
|
21
21
|
"dev": "tsc --watch"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@cranberry-money/shared-constants": "^
|
|
24
|
+
"@cranberry-money/shared-constants": "^3.0.0",
|
|
25
|
+
"@cranberry-money/shared-services": "^3.0.0"
|
|
25
26
|
},
|
|
26
27
|
"devDependencies": {
|
|
27
28
|
"typescript": "^5.0.0"
|