@aman050807/cashflow-lens 0.1.0 → 0.2.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.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,20 @@
2
2
 
3
3
  Changes follow semantic versioning. Dates are actual release dates.
4
4
 
5
+ ## 0.2.0 - 2026-11-19
6
+
7
+ Backward-compatible additions; all calculations are unchanged.
8
+
9
+ - `formatMoney` and `formatRate` display helpers built on `Intl.NumberFormat`,
10
+ including Indian locale grouping (`en-IN`). Display only: calculations stay
11
+ unrounded IEEE-754 numbers.
12
+ - Marked the package as side-effect free (`"sideEffects": false`) so bundlers
13
+ can drop unimported functions.
14
+ - Expanded editor hover documentation: full JSDoc with examples in the
15
+ TypeScript declarations.
16
+ - CI, npm version, bundle size and license badges in the README.
17
+ - Browser playground added to the companion demo repository.
18
+
5
19
  ## 0.1.0 - 2026-11-16
6
20
 
7
21
  Initial development release.
package/README.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # cashflow-lens
2
2
 
3
+ [![CI](https://github.com/aman050807/cashflow-lens/actions/workflows/test.yml/badge.svg)](https://github.com/aman050807/cashflow-lens/actions/workflows/test.yml)
4
+ [![npm version](https://img.shields.io/npm/v/@aman050807/cashflow-lens.svg)](https://www.npmjs.com/package/@aman050807/cashflow-lens)
5
+ [![minzipped size](https://img.shields.io/bundlephobia/minzip/@aman050807/cashflow-lens)](https://bundlephobia.com/package/@aman050807/cashflow-lens)
6
+ [![license](https://img.shields.io/npm/l/@aman050807/cashflow-lens)](https://github.com/aman050807/cashflow-lens/blob/main/LICENSE)
7
+
3
8
  Compare cashflow scenarios without mixing rental income with capital growth.
4
9
  A dependency-free JavaScript/Node.js library with CommonJS, ESM named imports,
5
10
  TypeScript declarations and strict runtime validation.
@@ -112,6 +117,36 @@ Returned fields:
112
117
  Accepts an array of the options above plus a unique `id`. Returns ranked projection
113
118
  objects. Duplicate ids throw `RangeError`; missing/blank/non-string ids throw `TypeError`.
114
119
 
120
+ ### `formatMoney(amount, options)`
121
+
122
+ Formats a money amount for display with `Intl.NumberFormat`. `options.currency`
123
+ is required (3-letter ISO 4217 code); `options.locale` defaults to `en-US`.
124
+ Use `en-IN` for Indian digit grouping.
125
+
126
+ ```js
127
+ formatMoney(100000, { locale: 'en-IN', currency: 'INR' }); // "₹1,00,000.00"
128
+ formatMoney(1000, { locale: 'en-US', currency: 'USD' }); // "$1,000.00"
129
+ ```
130
+
131
+ ### `formatRate(rate, options)`
132
+
133
+ Formats a decimal rate as a percentage. `options.maximumFractionDigits`
134
+ defaults to `2` (integer 0-20); `options.locale` defaults to `en-US`.
135
+
136
+ ```js
137
+ formatRate(0.06); // "6%"
138
+ formatRate(0.03945); // "3.95%"
139
+ ```
140
+
141
+ Both helpers are **display only**. They format output for humans; every
142
+ calculation stays an unrounded IEEE-754 number exactly as documented above.
143
+ They change nothing about financial accuracy and are never used internally.
144
+
145
+ ## Browser playground
146
+
147
+ A zero-build playground that ranks scenarios against the published package
148
+ lives in the demo repo: [cashflow-lens-demo/playground](https://github.com/aman050807/cashflow-lens-demo/tree/main/playground).
149
+
115
150
  ## Model assumptions and limits
116
151
 
117
152
  - Initial investment equals initial asset value. Acquisition costs are not modeled.
@@ -138,8 +173,8 @@ npm pack --dry-run
138
173
  ```
139
174
 
140
175
  Tests cover hand-calculated examples, losses, zero final values, fractional years,
141
- invalid input, overflow, stable sorting, scale invariance, non-mutation and both
142
- module loading styles. CI runs on Node.js 20, 22 and 24.
176
+ invalid input, overflow, stable sorting, scale invariance, non-mutation, display
177
+ formatting and both module loading styles. CI runs on Node.js 20, 22 and 24.
143
178
 
144
179
  ## Versioning
145
180
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aman050807/cashflow-lens",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Pure, dependency-free cashflow and investment scenario calculations with explicit assumptions.",
5
5
  "main": "./src/index.js",
6
6
  "types": "./src/index.d.ts",
@@ -10,6 +10,7 @@
10
10
  "default": "./src/index.js"
11
11
  }
12
12
  },
13
+ "sideEffects": false,
13
14
  "files": [
14
15
  "src",
15
16
  "README.md",
package/src/index.d.ts CHANGED
@@ -1,28 +1,213 @@
1
- /** Rates are decimals (0.05 = 5%); money uses one consistent currency. */
1
+ /**
2
+ * cashflow-lens: compare cashflow scenarios without mixing rental income
3
+ * with capital growth.
4
+ *
5
+ * All money arguments must use one consistent currency/unit. Rates are
6
+ * decimal fractions: `0.08` means 8%. Results are unrounded JavaScript
7
+ * numbers (IEEE-754, not decimal accounting); round for display only.
8
+ * All examples are fictional, not investment advice.
9
+ */
10
+
11
+ /**
12
+ * A scenario to project. Rates are decimals (`0.05` = 5%); money uses one
13
+ * consistent currency.
14
+ *
15
+ * @example
16
+ * const flat = {
17
+ * initialInvestment: 5000000, // ₹50 lakh
18
+ * annualIncome: 300000, // rent
19
+ * annualExpenses: 60000, // maintenance, tax
20
+ * annualGrowthRate: 0.04,
21
+ * years: 5,
22
+ * exitFeeRate: 0.02
23
+ * };
24
+ */
2
25
  export interface Scenario {
26
+ /** Initial asset value / capital invested. Must be greater than zero. */
3
27
  initialInvestment: number;
28
+ /** Gross income per year. Must be non-negative. */
4
29
  annualIncome: number;
30
+ /** Fixed annual expenses. Default `0`. Must be non-negative. */
5
31
  annualExpenses?: number;
32
+ /** Annual compound asset growth as a decimal. Default `0`. At least `-1`. */
6
33
  annualGrowthRate?: number;
34
+ /** Holding period in years; fractional years allowed. Must be greater than zero. */
7
35
  years: number;
36
+ /** Fraction of ending asset value charged at exit. Default `0`. Between `0` and `1`. */
8
37
  exitFeeRate?: number;
9
38
  }
39
+
40
+ /**
41
+ * The projection returned by {@link projectScenario}. All rates are decimal
42
+ * fractions; all money fields use the input's currency.
43
+ */
10
44
  export interface Projection {
45
+ /** `annualIncome - annualExpenses` */
11
46
  netAnnualIncome: number;
47
+ /** `netAnnualIncome * years` (income is not reinvested) */
12
48
  accumulatedIncome: number;
49
+ /** `initialInvestment * (1 + annualGrowthRate) ** years` */
13
50
  endingAssetValue: number;
51
+ /** `endingAssetValue * exitFeeRate` */
14
52
  exitFee: number;
53
+ /** `endingAssetValue - exitFee` */
15
54
  terminalProceeds: number;
55
+ /** `terminalProceeds + accumulatedIncome - initialInvestment` */
16
56
  netProfit: number;
57
+ /** `netAnnualIncome / initialInvestment` */
17
58
  cashYieldRate: number;
59
+ /** `netProfit / initialInvestment` (cumulative, not annualized) */
18
60
  totalReturnRate: number;
19
- /** Capital-only CAGR after exit fees. Excludes interim income. */
61
+ /** Capital-only CAGR after exit fees. Excludes interim income; do not add it to cash yield. */
20
62
  capitalCagrRate: number;
21
63
  }
64
+
65
+ /** A {@link Scenario} plus a unique non-empty string `id`. */
22
66
  export interface IdentifiedScenario extends Scenario { id: string; }
67
+
68
+ /** A {@link Projection} plus the `id` of the scenario it came from. */
23
69
  export interface RankedProjection extends Projection { id: string; }
70
+
71
+ /**
72
+ * Annual net income divided by initial investment. Returns a decimal rate.
73
+ *
74
+ * @param initialInvestment Positive initial capital.
75
+ * @param annualIncome Non-negative gross annual income.
76
+ * @param annualExpenses Non-negative annual expenses. Default `0`.
77
+ * @returns `(annualIncome - annualExpenses) / initialInvestment`
78
+ * @throws {TypeError} Non-finite or wrongly typed arguments.
79
+ * @throws {RangeError} Negative income/expenses or non-positive investment.
80
+ *
81
+ * @example
82
+ * cashYield(10000, 800, 200); // 0.06, i.e. 6%
83
+ */
24
84
  export declare function cashYield(initialInvestment: number, annualIncome: number, annualExpenses?: number): number;
85
+
86
+ /**
87
+ * Cumulative total return: income and terminal proceeds versus initial
88
+ * investment. Not annualized.
89
+ *
90
+ * @param initialInvestment Positive initial capital.
91
+ * @param terminalProceeds Non-negative proceeds at exit.
92
+ * @param accumulatedIncome Income over the holding period; may be negative. Default `0`.
93
+ * @returns `(terminalProceeds + accumulatedIncome - initialInvestment) / initialInvestment`
94
+ * @throws {TypeError} Non-finite or wrongly typed arguments.
95
+ * @throws {RangeError} Negative terminal proceeds or non-positive investment.
96
+ *
97
+ * @example
98
+ * totalReturn(10000, 10804.5, 1200); // 0.20045, i.e. 20.045% over the period
99
+ */
25
100
  export declare function totalReturn(initialInvestment: number, terminalProceeds: number, accumulatedIncome?: number): number;
101
+
102
+ /**
103
+ * Capital-only compound annual growth rate. Not IRR/XIRR: ignores interim
104
+ * income and its timing. A zero final value returns `-1` (100% loss).
105
+ *
106
+ * @param initialValue Positive starting value.
107
+ * @param finalValue Non-negative ending value.
108
+ * @param years Positive period; fractional years allowed.
109
+ * @returns `(finalValue / initialValue) ** (1 / years) - 1`
110
+ * @throws {TypeError} Non-finite or wrongly typed arguments.
111
+ * @throws {RangeError} Negative final value, or non-positive initial value or years.
112
+ *
113
+ * @example
114
+ * cagr(100, 121, 2); // 0.1, i.e. 10% per year
115
+ */
26
116
  export declare function cagr(initialValue: number, finalValue: number, years: number): number;
117
+
118
+ /**
119
+ * Project one scenario: fixed annual net income (not reinvested) plus
120
+ * constant compound asset growth, minus an exit fee on the ending value.
121
+ *
122
+ * Model limits: no taxes, inflation, debt, vacancies or reinvestment.
123
+ * Unknown option fields are ignored.
124
+ *
125
+ * @param scenario The scenario to project.
126
+ * @returns The {@link Projection}. Throws `RangeError` if any result overflows to a non-finite number.
127
+ * @throws {TypeError} Missing/invalid fields or a non-object argument.
128
+ * @throws {RangeError} Out-of-range fields (see {@link Scenario}).
129
+ *
130
+ * @example
131
+ * const p = projectScenario({
132
+ * initialInvestment: 10000, annualIncome: 800, annualExpenses: 200,
133
+ * annualGrowthRate: 0.05, years: 2, exitFeeRate: 0.02
134
+ * });
135
+ * p.netProfit; // 2004.5
136
+ * p.cashYieldRate; // 0.06
137
+ * p.totalReturnRate; // 0.20045
138
+ * p.capitalCagrRate; // about 0.0394
139
+ */
27
140
  export declare function projectScenario(scenario: Scenario): Projection;
141
+
142
+ /**
143
+ * Rank scenarios by total return rate, highest first. Equal rates retain
144
+ * input order. Inputs are never mutated; empty input returns `[]`.
145
+ *
146
+ * Only compare like-for-like horizons and currency assumptions: this ranks
147
+ * one metric, not risk, liquidity, suitability or investment quality.
148
+ *
149
+ * @param scenarios Scenarios with unique non-empty string `id`s.
150
+ * @returns Ranked projections carrying each scenario's `id`.
151
+ * @throws {TypeError} Non-array input, or a missing/blank/non-string `id`.
152
+ * @throws {RangeError} Duplicate `id`s.
153
+ *
154
+ * @example
155
+ * const ranked = compareScenarios([
156
+ * { id: 'income-first', initialInvestment: 10000, annualIncome: 800, years: 3 },
157
+ * { id: 'growth-first', initialInvestment: 10000, annualIncome: 400, annualGrowthRate: 0.05, years: 3 }
158
+ * ]);
159
+ * ranked.map(row => row.id); // ['growth-first', 'income-first']
160
+ */
28
161
  export declare function compareScenarios(scenarios: readonly IdentifiedScenario[]): RankedProjection[];
162
+
163
+ /** Options for {@link formatMoney}. */
164
+ export interface FormatMoneyOptions {
165
+ /**
166
+ * BCP 47 locale controlling digit grouping and symbol placement.
167
+ * Default `'en-US'`. Use `'en-IN'` for Indian grouping (`1,00,000`).
168
+ */
169
+ locale?: string;
170
+ /** 3-letter ISO 4217 currency code, e.g. `'INR'`, `'USD'`, `'EUR'`. Required. */
171
+ currency: string;
172
+ }
173
+
174
+ /** Options for {@link formatRate}. */
175
+ export interface FormatRateOptions {
176
+ /** BCP 47 locale. Default `'en-US'`. */
177
+ locale?: string;
178
+ /** Fraction digits shown after the decimal point, integer 0-20. Default `2`. */
179
+ maximumFractionDigits?: number;
180
+ }
181
+
182
+ /**
183
+ * Format a money amount for display, using the runtime's `Intl.NumberFormat`
184
+ * data. Display only: the returned string is for humans and never feeds the
185
+ * library's calculations, which stay unrounded IEEE-754 numbers.
186
+ *
187
+ * @param amount Finite number in the given currency.
188
+ * @param options See {@link FormatMoneyOptions}. `currency` is required.
189
+ * @returns A localized currency string.
190
+ * @throws {TypeError} Non-finite amount, missing/non-string currency, or non-object options.
191
+ * @throws {RangeError} Currency that is not a 3-letter ISO 4217 code.
192
+ *
193
+ * @example
194
+ * formatMoney(100000, { locale: 'en-IN', currency: 'INR' }); // "₹1,00,000.00"
195
+ * formatMoney(1000, { locale: 'en-US', currency: 'USD' }); // "$1,000.00"
196
+ */
197
+ export declare function formatMoney(amount: number, options: FormatMoneyOptions): string;
198
+
199
+ /**
200
+ * Format a decimal rate as a percentage for display. Display only: rounding
201
+ * applies to the string, not to stored values.
202
+ *
203
+ * @param rate Finite decimal rate (`0.06` = 6%).
204
+ * @param options See {@link FormatRateOptions}.
205
+ * @returns A localized percent string.
206
+ * @throws {TypeError} Non-finite rate, non-integer digits, or non-object options.
207
+ * @throws {RangeError} `maximumFractionDigits` outside 0-20.
208
+ *
209
+ * @example
210
+ * formatRate(0.06); // "6%"
211
+ * formatRate(0.0394, { maximumFractionDigits: 1 }); // "3.9%"
212
+ */
213
+ export declare function formatRate(rate: number, options?: FormatRateOptions): string;
package/src/index.js CHANGED
@@ -91,8 +91,54 @@ function compareScenarios(scenarios) {
91
91
  }).sort((a, b) => b.totalReturnRate - a.totalReturnRate);
92
92
  }
93
93
 
94
+ function localeOf(value) {
95
+ if (value === undefined) return 'en-US';
96
+ if (typeof value !== 'string' || value.trim() === '') {
97
+ throw new TypeError('locale must be a non-empty BCP 47 string, e.g. "en-IN"');
98
+ }
99
+ return value;
100
+ }
101
+
102
+ /**
103
+ * Format a money amount for display, e.g. "\u20B91,00,000.00" in en-IN/INR.
104
+ * Display only: the result is a string for humans and never feeds calculations.
105
+ */
106
+ function formatMoney(amount, options = {}) {
107
+ finite(amount, 'amount');
108
+ if (options === null || typeof options !== 'object' || Array.isArray(options)) {
109
+ throw new TypeError('options must be an object');
110
+ }
111
+ if (options.currency === undefined) throw new TypeError('options.currency is required');
112
+ if (typeof options.currency !== 'string') throw new TypeError('currency must be a string');
113
+ if (!/^[A-Za-z]{3}$/.test(options.currency)) {
114
+ throw new RangeError('currency must be a 3-letter ISO 4217 code, e.g. "INR"');
115
+ }
116
+ return new Intl.NumberFormat(localeOf(options.locale),
117
+ { style: 'currency', currency: options.currency.toUpperCase() }).format(amount);
118
+ }
119
+
120
+ /**
121
+ * Format a decimal rate as a percentage for display, e.g. 0.06 -> "6%".
122
+ * Display only: rounding applies to the string, not to stored values.
123
+ */
124
+ function formatRate(rate, options = {}) {
125
+ finite(rate, 'rate');
126
+ if (options === null || typeof options !== 'object' || Array.isArray(options)) {
127
+ throw new TypeError('options must be an object');
128
+ }
129
+ const digits = options.maximumFractionDigits === undefined ? 2 : options.maximumFractionDigits;
130
+ if (typeof digits !== 'number' || !Number.isInteger(digits)) {
131
+ throw new TypeError('maximumFractionDigits must be an integer');
132
+ }
133
+ if (digits < 0 || digits > 20) throw new RangeError('maximumFractionDigits must be between 0 and 20');
134
+ return new Intl.NumberFormat(localeOf(options.locale),
135
+ { style: 'percent', maximumFractionDigits: digits }).format(rate);
136
+ }
137
+
94
138
  exports.cashYield = cashYield;
95
139
  exports.totalReturn = totalReturn;
96
140
  exports.cagr = cagr;
97
141
  exports.projectScenario = projectScenario;
98
142
  exports.compareScenarios = compareScenarios;
143
+ exports.formatMoney = formatMoney;
144
+ exports.formatRate = formatRate;