@wpgenius/financial-calculators 0.0.0-stage → 1.0.0

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.
@@ -0,0 +1,208 @@
1
+ // Types for financial-calculators. Keep in step with src/ (test/types.ts checks them).
2
+
3
+ /** A numeric input: a number, or a string holding one. */
4
+ export type NumericInput = number | string;
5
+
6
+ /** Number grouping: Indian (12,34,567) or international (1,234,567). */
7
+ export type NumberSystem = 'indian' | 'international';
8
+
9
+ /** Every series point has the year (fractions for a last partial year) and the month. */
10
+ export interface SeriesPoint {
11
+ year: number;
12
+ month: number;
13
+ }
14
+
15
+ export interface SipInputs {
16
+ /** Amount invested at the start of every month. */
17
+ monthlyAmount: NumericInput;
18
+ /** Expected return, % a year (12 for 12%). */
19
+ annualRate: NumericInput;
20
+ /** Duration in years; fractions are rounded to whole months. */
21
+ years: NumericInput;
22
+ }
23
+
24
+ export interface GrowthPoint extends SeriesPoint {
25
+ invested: number;
26
+ total: number;
27
+ }
28
+
29
+ export interface SipResult {
30
+ invested: number;
31
+ returns: number;
32
+ total: number;
33
+ months: number;
34
+ series: GrowthPoint[];
35
+ }
36
+
37
+ export interface LumpsumInputs {
38
+ /** Amount invested once, at the start. */
39
+ amount: NumericInput;
40
+ annualRate: NumericInput;
41
+ years: NumericInput;
42
+ /** 1 yearly (default), 4 quarterly, 12 monthly. */
43
+ compoundingPerYear?: NumericInput;
44
+ }
45
+
46
+ export type LumpsumResult = SipResult;
47
+
48
+ export interface SwpInputs {
49
+ /** Amount invested at the start. */
50
+ investment: NumericInput;
51
+ /** Amount withdrawn at the start of every month. */
52
+ monthlyWithdrawal: NumericInput;
53
+ annualRate: NumericInput;
54
+ years: NumericInput;
55
+ }
56
+
57
+ export interface SwpPoint extends SeriesPoint {
58
+ balance: number;
59
+ /** Total withdrawn so far. */
60
+ withdrawn: number;
61
+ }
62
+
63
+ export interface SwpResult {
64
+ investment: number;
65
+ totalWithdrawn: number;
66
+ /** Never below 0. */
67
+ finalBalance: number;
68
+ /** finalBalance + totalWithdrawn − investment. */
69
+ returns: number;
70
+ /** The month (1-based) the money ran out, or null. */
71
+ depletedMonth: number | null;
72
+ months: number;
73
+ series: SwpPoint[];
74
+ }
75
+
76
+ export interface GoalInputs {
77
+ /** What the goal costs today. */
78
+ presentCost: NumericInput;
79
+ /** Savings already put aside for it (default 0). */
80
+ currentSavings?: NumericInput;
81
+ /** Inflation, % a year. */
82
+ inflationRate: NumericInput;
83
+ /** Expected return, % a year. */
84
+ annualRate: NumericInput;
85
+ /** Years until the goal. */
86
+ years: NumericInput;
87
+ }
88
+
89
+ export interface GoalPoint extends SeriesPoint {
90
+ goalCost: number;
91
+ savings: number;
92
+ investments: number;
93
+ total: number;
94
+ }
95
+
96
+ export interface GoalResult {
97
+ presentCost: number;
98
+ futureCost: number;
99
+ savingsFutureValue: number;
100
+ shortfall: number;
101
+ /** 0 when the savings already cover the goal. */
102
+ monthlyInvestment: number;
103
+ totalInvested: number;
104
+ months: number;
105
+ series: GoalPoint[];
106
+ }
107
+
108
+ export interface EmiInputs {
109
+ /** Loan amount. */
110
+ principal: NumericInput;
111
+ /** Interest, % a year. */
112
+ annualRate: NumericInput;
113
+ /** Tenure in years. */
114
+ years: NumericInput;
115
+ }
116
+
117
+ export interface EmiScheduleRow {
118
+ /** Loan year, from 1. */
119
+ year: number;
120
+ principalPaid: number;
121
+ interestPaid: number;
122
+ /** Balance left at the end of that year. */
123
+ balance: number;
124
+ }
125
+
126
+ export interface EmiPoint extends SeriesPoint {
127
+ balance: number;
128
+ /** Cumulative. */
129
+ principalPaid: number;
130
+ /** Cumulative. */
131
+ interestPaid: number;
132
+ }
133
+
134
+ export interface EmiResult {
135
+ emi: number;
136
+ principal: number;
137
+ totalInterest: number;
138
+ totalPayment: number;
139
+ months: number;
140
+ schedule: EmiScheduleRow[];
141
+ series: EmiPoint[];
142
+ }
143
+
144
+ export interface CalculatorMap {
145
+ sip: { inputs: SipInputs; result: SipResult };
146
+ lumpsum: { inputs: LumpsumInputs; result: LumpsumResult };
147
+ swp: { inputs: SwpInputs; result: SwpResult };
148
+ goal: { inputs: GoalInputs; result: GoalResult };
149
+ emi: { inputs: EmiInputs; result: EmiResult };
150
+ }
151
+
152
+ export type CalculatorType = keyof CalculatorMap;
153
+
154
+ export function sip( inputs: SipInputs ): SipResult;
155
+ export function lumpsum( inputs: LumpsumInputs ): LumpsumResult;
156
+ export function swp( inputs: SwpInputs ): SwpResult;
157
+ export function goal( inputs: GoalInputs ): GoalResult;
158
+ export function emi( inputs: EmiInputs ): EmiResult;
159
+
160
+ /** Runs a calculator by type. Throws on an unknown type. */
161
+ export function calculate<T extends CalculatorType>( type: T, inputs: CalculatorMap[ T ][ 'inputs' ] ): CalculatorMap[ T ][ 'result' ];
162
+ export function calculate( type: string, inputs: Record<string, unknown> ): unknown;
163
+
164
+ /** The calculator types available. */
165
+ export function types(): string[];
166
+
167
+ /** Adds or replaces a calculator. */
168
+ export function register( type: string, fn: ( inputs: any ) => unknown ): void;
169
+
170
+ export interface NumberOptions {
171
+ system?: NumberSystem;
172
+ /** Digits after the decimal point, 0 to 20. Default 0. */
173
+ decimals?: number;
174
+ }
175
+
176
+ export interface CurrencyOptions extends NumberOptions {
177
+ /** Default '₹'. */
178
+ symbol?: string;
179
+ /** 'before' (₹100, default) or 'after' (100 €). */
180
+ position?: 'before' | 'after';
181
+ }
182
+
183
+ export interface CompactOptions {
184
+ system?: NumberSystem;
185
+ /** Most digits after the decimal point. Default 1. */
186
+ decimals?: number;
187
+ }
188
+
189
+ /** formatNumber( 5808477 ) → "58,08,477". */
190
+ export function formatNumber( value: number, options?: NumberOptions ): string;
191
+ /** formatCurrency( -1500 ) → "-₹1,500". */
192
+ export function formatCurrency( value: number, options?: CurrencyOptions ): string;
193
+ /** formatCompact( 1500000 ) → "15L"; international → "1.5M". */
194
+ export function formatCompact( value: number, options?: CompactOptions ): string;
195
+ /** groupDigits( '1234567' ) → "12,34,567". */
196
+ export function groupDigits( digits: string, system?: NumberSystem ): string;
197
+ /** amountInWords( 2808477 ) → "Twenty Eight Lakh Eight Thousand Four Hundred and Seventy Seven". */
198
+ export function amountInWords( value: number | bigint | string, options?: { system?: NumberSystem } ): string;
199
+
200
+ /** Library version, e.g. "1.0.0". */
201
+ export const VERSION: string;
202
+
203
+ /** The browser builds define this global. */
204
+ declare global {
205
+ interface Window {
206
+ FinancialCalculators: typeof import( './financial-calculators.js' );
207
+ }
208
+ }