@aman050807/cashflow-lens 0.0.0-stage → 0.1.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 +15 -0
- package/LICENSE +21 -0
- package/README.md +151 -2
- package/package.json +42 -4
- package/src/index.d.ts +28 -0
- package/src/index.js +98 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
Changes follow semantic versioning. Dates are actual release dates.
|
|
4
|
+
|
|
5
|
+
## 0.1.0 - 2026-11-16
|
|
6
|
+
|
|
7
|
+
Initial development release.
|
|
8
|
+
|
|
9
|
+
- Cash yield, cumulative total return and capital-only CAGR functions.
|
|
10
|
+
- Constant-income scenario projection with compound asset growth and exit fees.
|
|
11
|
+
- Stable, non-mutating scenario ranking with unique identifiers.
|
|
12
|
+
- Runtime validation, TypeScript declarations and CommonJS/ESM support.
|
|
13
|
+
- Unit tests and Node.js 20/22/24 CI workflow.
|
|
14
|
+
|
|
15
|
+
The model is educational. It does not calculate IRR, taxes, debt or investment risk.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 V M Aman Sharma
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,152 @@
|
|
|
1
|
-
#
|
|
1
|
+
# cashflow-lens
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Compare cashflow scenarios without mixing rental income with capital growth.
|
|
4
|
+
A dependency-free JavaScript/Node.js library with CommonJS, ESM named imports,
|
|
5
|
+
TypeScript declarations and strict runtime validation.
|
|
6
|
+
|
|
7
|
+
Useful for comparison tables, educational investment calculators and scenario
|
|
8
|
+
analysis. All examples are fictional, not investment advice or expected returns.
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```sh
|
|
13
|
+
npm install @aman050807/cashflow-lens
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Requires Node.js 20+. No runtime dependencies, network calls, build step or side effects.
|
|
17
|
+
|
|
18
|
+
## Quick start
|
|
19
|
+
|
|
20
|
+
```js
|
|
21
|
+
const { projectScenario } = require('@aman050807/cashflow-lens');
|
|
22
|
+
// ESM: import { projectScenario } from '@aman050807/cashflow-lens';
|
|
23
|
+
|
|
24
|
+
const result = projectScenario({
|
|
25
|
+
initialInvestment: 10000,
|
|
26
|
+
annualIncome: 800,
|
|
27
|
+
annualExpenses: 200,
|
|
28
|
+
annualGrowthRate: 0.05, // 5%, not 5
|
|
29
|
+
years: 2,
|
|
30
|
+
exitFeeRate: 0.02 // 2% of ending asset value
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
console.log(result.netProfit); // 2004.5
|
|
34
|
+
console.log(result.cashYieldRate); // 0.06 = 6%
|
|
35
|
+
console.log(result.totalReturnRate); // 0.20045 = 20.045% over two years
|
|
36
|
+
console.log(result.capitalCagrRate); // about 0.039447 = 3.9447% per year
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Compare scenarios
|
|
40
|
+
|
|
41
|
+
```js
|
|
42
|
+
const { compareScenarios } = require('@aman050807/cashflow-lens');
|
|
43
|
+
|
|
44
|
+
const ranked = compareScenarios([
|
|
45
|
+
{ id: 'income-first', initialInvestment: 10000, annualIncome: 800, years: 3 },
|
|
46
|
+
{ id: 'growth-first', initialInvestment: 10000, annualIncome: 400,
|
|
47
|
+
annualGrowthRate: 0.05, years: 3 }
|
|
48
|
+
]);
|
|
49
|
+
|
|
50
|
+
console.log(ranked.map(row => row.id)); // ['growth-first', 'income-first']
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Results are sorted by **total return rate**, highest first. Equal rates retain
|
|
54
|
+
input order. No input array or object is changed. Empty input returns `[]`.
|
|
55
|
+
Each scenario needs a unique non-empty string `id`; results contain that `id`
|
|
56
|
+
and the projection fields below, not arbitrary input metadata.
|
|
57
|
+
|
|
58
|
+
**Only compare like-for-like horizons and currency assumptions.** A higher total
|
|
59
|
+
return over ten years is not automatically better than a smaller return over one.
|
|
60
|
+
This function ranks one metric, not risk, liquidity, suitability or investment quality.
|
|
61
|
+
|
|
62
|
+
## API
|
|
63
|
+
|
|
64
|
+
All money arguments must use the same currency/unit. Rates are decimal fractions:
|
|
65
|
+
`0.08` means 8%. Results are unrounded JavaScript numbers. Round for display only.
|
|
66
|
+
|
|
67
|
+
### `cashYield(initialInvestment, annualIncome, annualExpenses = 0)`
|
|
68
|
+
|
|
69
|
+
Returns `(annualIncome - annualExpenses) / initialInvestment`.
|
|
70
|
+
Income and expenses must be non-negative; a net loss is allowed.
|
|
71
|
+
|
|
72
|
+
### `totalReturn(initialInvestment, terminalProceeds, accumulatedIncome = 0)`
|
|
73
|
+
|
|
74
|
+
Returns `(terminalProceeds + accumulatedIncome - initialInvestment) / initialInvestment`.
|
|
75
|
+
Terminal proceeds must be non-negative. Accumulated income may be negative to
|
|
76
|
+
represent operating losses. This is a **cumulative**, not annualized, return.
|
|
77
|
+
|
|
78
|
+
### `cagr(initialValue, finalValue, years)`
|
|
79
|
+
|
|
80
|
+
Returns `(finalValue / initialValue) ** (1 / years) - 1`.
|
|
81
|
+
Final value must be non-negative. A zero final value returns `-1` (100% loss).
|
|
82
|
+
This is a **capital-only** measure. It is not IRR/XIRR and does not account for
|
|
83
|
+
interim cashflow timing. Do not add this rate to cash yield to invent a total CAGR.
|
|
84
|
+
|
|
85
|
+
### `projectScenario(options)`
|
|
86
|
+
|
|
87
|
+
| Option | Required | Meaning |
|
|
88
|
+
| --- | --- | --- |
|
|
89
|
+
| `initialInvestment` | yes | Positive initial asset value / capital invested |
|
|
90
|
+
| `annualIncome` | yes | Non-negative gross income per year |
|
|
91
|
+
| `years` | yes | Positive holding period; fractional years allowed |
|
|
92
|
+
| `annualExpenses` | no, `0` | Non-negative fixed annual expenses |
|
|
93
|
+
| `annualGrowthRate` | no, `0` | Annual compound asset growth, at least `-1` |
|
|
94
|
+
| `exitFeeRate` | no, `0` | Fraction of ending asset value, between `0` and `1` |
|
|
95
|
+
|
|
96
|
+
Returned fields:
|
|
97
|
+
|
|
98
|
+
| Field | Formula |
|
|
99
|
+
| --- | --- |
|
|
100
|
+
| `netAnnualIncome` | annualIncome - annualExpenses |
|
|
101
|
+
| `accumulatedIncome` | netAnnualIncome * years |
|
|
102
|
+
| `endingAssetValue` | initialInvestment * (1 + annualGrowthRate) ** years |
|
|
103
|
+
| `exitFee` | endingAssetValue * exitFeeRate |
|
|
104
|
+
| `terminalProceeds` | endingAssetValue - exitFee |
|
|
105
|
+
| `netProfit` | terminalProceeds + accumulatedIncome - initialInvestment |
|
|
106
|
+
| `cashYieldRate` | netAnnualIncome / initialInvestment |
|
|
107
|
+
| `totalReturnRate` | netProfit / initialInvestment |
|
|
108
|
+
| `capitalCagrRate` | cagr(initialInvestment, terminalProceeds, years) |
|
|
109
|
+
|
|
110
|
+
### `compareScenarios(scenarios)`
|
|
111
|
+
|
|
112
|
+
Accepts an array of the options above plus a unique `id`. Returns ranked projection
|
|
113
|
+
objects. Duplicate ids throw `RangeError`; missing/blank/non-string ids throw `TypeError`.
|
|
114
|
+
|
|
115
|
+
## Model assumptions and limits
|
|
116
|
+
|
|
117
|
+
- Initial investment equals initial asset value. Acquisition costs are not modeled.
|
|
118
|
+
- Income and expenses stay constant and are **not reinvested**. Fractional years
|
|
119
|
+
prorate these flows linearly; asset growth compounds over the fractional exponent.
|
|
120
|
+
- No taxes, inflation, debt, currency conversion, vacancies, changing fees or risk model.
|
|
121
|
+
Model an expected vacancy allowance within annual income, not as a guaranteed outcome.
|
|
122
|
+
- Exit fee applies to ending asset value, not profit. Net cashflow may be negative.
|
|
123
|
+
- Annual growth cannot be below -100%; exit fees can reach 100%.
|
|
124
|
+
- Unknown option fields are ignored. Sparse comparison arrays are invalid.
|
|
125
|
+
- Uses IEEE-754 numbers, not decimal accounting arithmetic. Do not use for payment
|
|
126
|
+
settlement, ledger reconciliation or exact money storage. Very large inputs may overflow.
|
|
127
|
+
- Inputs must be finite numbers, not numeric strings. Initial capital and years must
|
|
128
|
+
be greater than zero. Invalid types throw `TypeError`, invalid ranges and non-finite
|
|
129
|
+
calculated results throw `RangeError`. Very tiny values can underflow to zero.
|
|
130
|
+
|
|
131
|
+
## Development and verification
|
|
132
|
+
|
|
133
|
+
```sh
|
|
134
|
+
npm ci
|
|
135
|
+
npm test
|
|
136
|
+
npm run test:coverage
|
|
137
|
+
npm pack --dry-run
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
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.
|
|
143
|
+
|
|
144
|
+
## Versioning
|
|
145
|
+
|
|
146
|
+
Semantic versioning. The first stable release is `1.0.0`. Patch versions fix bugs;
|
|
147
|
+
minor versions add backward-compatible features; major versions change existing
|
|
148
|
+
contracts. See [CHANGELOG.md](CHANGELOG.md).
|
|
149
|
+
|
|
150
|
+
## License
|
|
151
|
+
|
|
152
|
+
MIT. See [LICENSE](LICENSE).
|
package/package.json
CHANGED
|
@@ -1,6 +1,44 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aman050807/cashflow-lens",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Pure, dependency-free cashflow and investment scenario calculations with explicit assumptions.",
|
|
5
|
+
"main": "./src/index.js",
|
|
6
|
+
"types": "./src/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./src/index.d.ts",
|
|
10
|
+
"default": "./src/index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"src",
|
|
15
|
+
"README.md",
|
|
16
|
+
"LICENSE",
|
|
17
|
+
"CHANGELOG.md"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"test": "node --test",
|
|
21
|
+
"test:coverage": "node --test --experimental-test-coverage",
|
|
22
|
+
"prepublishOnly": "npm test"
|
|
23
|
+
},
|
|
24
|
+
"engines": {
|
|
25
|
+
"node": ">=20"
|
|
26
|
+
},
|
|
27
|
+
"keywords": [
|
|
28
|
+
"cashflow",
|
|
29
|
+
"roi",
|
|
30
|
+
"cagr",
|
|
31
|
+
"investment",
|
|
32
|
+
"comparison"
|
|
33
|
+
],
|
|
34
|
+
"author": "V M Aman Sharma",
|
|
35
|
+
"license": "MIT",
|
|
36
|
+
"repository": {
|
|
37
|
+
"type": "git",
|
|
38
|
+
"url": "git+https://github.com/aman050807/cashflow-lens.git"
|
|
39
|
+
},
|
|
40
|
+
"homepage": "https://github.com/aman050807/cashflow-lens#readme",
|
|
41
|
+
"bugs": {
|
|
42
|
+
"url": "https://github.com/aman050807/cashflow-lens/issues"
|
|
43
|
+
}
|
|
44
|
+
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/** Rates are decimals (0.05 = 5%); money uses one consistent currency. */
|
|
2
|
+
export interface Scenario {
|
|
3
|
+
initialInvestment: number;
|
|
4
|
+
annualIncome: number;
|
|
5
|
+
annualExpenses?: number;
|
|
6
|
+
annualGrowthRate?: number;
|
|
7
|
+
years: number;
|
|
8
|
+
exitFeeRate?: number;
|
|
9
|
+
}
|
|
10
|
+
export interface Projection {
|
|
11
|
+
netAnnualIncome: number;
|
|
12
|
+
accumulatedIncome: number;
|
|
13
|
+
endingAssetValue: number;
|
|
14
|
+
exitFee: number;
|
|
15
|
+
terminalProceeds: number;
|
|
16
|
+
netProfit: number;
|
|
17
|
+
cashYieldRate: number;
|
|
18
|
+
totalReturnRate: number;
|
|
19
|
+
/** Capital-only CAGR after exit fees. Excludes interim income. */
|
|
20
|
+
capitalCagrRate: number;
|
|
21
|
+
}
|
|
22
|
+
export interface IdentifiedScenario extends Scenario { id: string; }
|
|
23
|
+
export interface RankedProjection extends Projection { id: string; }
|
|
24
|
+
export declare function cashYield(initialInvestment: number, annualIncome: number, annualExpenses?: number): number;
|
|
25
|
+
export declare function totalReturn(initialInvestment: number, terminalProceeds: number, accumulatedIncome?: number): number;
|
|
26
|
+
export declare function cagr(initialValue: number, finalValue: number, years: number): number;
|
|
27
|
+
export declare function projectScenario(scenario: Scenario): Projection;
|
|
28
|
+
export declare function compareScenarios(scenarios: readonly IdentifiedScenario[]): RankedProjection[];
|
package/src/index.js
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
function finite(value, name) {
|
|
4
|
+
if (typeof value !== 'number' || !Number.isFinite(value)) {
|
|
5
|
+
throw new TypeError(`${name} must be a finite number`);
|
|
6
|
+
}
|
|
7
|
+
return value;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function positive(value, name) {
|
|
11
|
+
finite(value, name);
|
|
12
|
+
if (value <= 0) throw new RangeError(`${name} must be greater than zero`);
|
|
13
|
+
return value;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function nonnegative(value, name) {
|
|
17
|
+
finite(value, name);
|
|
18
|
+
if (value < 0) throw new RangeError(`${name} must not be negative`);
|
|
19
|
+
return value;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function result(value) {
|
|
23
|
+
if (!Number.isFinite(value)) throw new RangeError('calculation exceeds the finite number range');
|
|
24
|
+
return value;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Annual net income divided by initial investment. Returns a decimal rate. */
|
|
28
|
+
function cashYield(initialInvestment, annualIncome, annualExpenses = 0) {
|
|
29
|
+
positive(initialInvestment, 'initialInvestment');
|
|
30
|
+
nonnegative(annualIncome, 'annualIncome');
|
|
31
|
+
nonnegative(annualExpenses, 'annualExpenses');
|
|
32
|
+
return result((annualIncome - annualExpenses) / initialInvestment);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** (Terminal proceeds + accumulated income - initial investment) / initial investment. */
|
|
36
|
+
function totalReturn(initialInvestment, terminalProceeds, accumulatedIncome = 0) {
|
|
37
|
+
positive(initialInvestment, 'initialInvestment');
|
|
38
|
+
nonnegative(terminalProceeds, 'terminalProceeds');
|
|
39
|
+
finite(accumulatedIncome, 'accumulatedIncome');
|
|
40
|
+
return result((terminalProceeds + accumulatedIncome - initialInvestment) / initialInvestment);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Capital-only CAGR. Does not account for interim income or its timing. */
|
|
44
|
+
function cagr(initialValue, finalValue, years) {
|
|
45
|
+
positive(initialValue, 'initialValue');
|
|
46
|
+
nonnegative(finalValue, 'finalValue');
|
|
47
|
+
positive(years, 'years');
|
|
48
|
+
return result(Math.pow(finalValue / initialValue, 1 / years) - 1);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Fixed annual net income, no reinvestment, constant annual asset growth. */
|
|
52
|
+
function projectScenario(options) {
|
|
53
|
+
if (options === null || typeof options !== 'object' || Array.isArray(options)) {
|
|
54
|
+
throw new TypeError('scenario must be an object');
|
|
55
|
+
}
|
|
56
|
+
const { initialInvestment, annualIncome, annualExpenses = 0,
|
|
57
|
+
annualGrowthRate = 0, years, exitFeeRate = 0 } = options;
|
|
58
|
+
positive(initialInvestment, 'initialInvestment');
|
|
59
|
+
nonnegative(annualIncome, 'annualIncome');
|
|
60
|
+
nonnegative(annualExpenses, 'annualExpenses');
|
|
61
|
+
positive(years, 'years');
|
|
62
|
+
finite(annualGrowthRate, 'annualGrowthRate');
|
|
63
|
+
if (annualGrowthRate < -1) throw new RangeError('annualGrowthRate must be at least -1');
|
|
64
|
+
finite(exitFeeRate, 'exitFeeRate');
|
|
65
|
+
if (exitFeeRate < 0 || exitFeeRate > 1) throw new RangeError('exitFeeRate must be between 0 and 1');
|
|
66
|
+
const netAnnualIncome = result(annualIncome - annualExpenses);
|
|
67
|
+
const accumulatedIncome = result(netAnnualIncome * years);
|
|
68
|
+
const endingAssetValue = result(initialInvestment * Math.pow(1 + annualGrowthRate, years));
|
|
69
|
+
const exitFee = result(endingAssetValue * exitFeeRate);
|
|
70
|
+
const terminalProceeds = result(endingAssetValue - exitFee);
|
|
71
|
+
const netProfit = result(terminalProceeds + accumulatedIncome - initialInvestment);
|
|
72
|
+
return {
|
|
73
|
+
netAnnualIncome, accumulatedIncome, endingAssetValue, exitFee, terminalProceeds, netProfit,
|
|
74
|
+
cashYieldRate: cashYield(initialInvestment, annualIncome, annualExpenses),
|
|
75
|
+
totalReturnRate: totalReturn(initialInvestment, terminalProceeds, accumulatedIncome),
|
|
76
|
+
capitalCagrRate: cagr(initialInvestment, terminalProceeds, years)
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Rank scenarios by total return rate, descending. Input order breaks ties. */
|
|
81
|
+
function compareScenarios(scenarios) {
|
|
82
|
+
if (!Array.isArray(scenarios)) throw new TypeError('scenarios must be an array');
|
|
83
|
+
const ids = new Set();
|
|
84
|
+
return Array.from(scenarios, (scenario) => {
|
|
85
|
+
if (!scenario || typeof scenario.id !== 'string' || scenario.id.trim() === '') {
|
|
86
|
+
throw new TypeError('each scenario must have a non-empty string id');
|
|
87
|
+
}
|
|
88
|
+
if (ids.has(scenario.id)) throw new RangeError(`duplicate scenario id: ${scenario.id}`);
|
|
89
|
+
ids.add(scenario.id);
|
|
90
|
+
return { id: scenario.id, ...projectScenario(scenario) };
|
|
91
|
+
}).sort((a, b) => b.totalReturnRate - a.totalReturnRate);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
exports.cashYield = cashYield;
|
|
95
|
+
exports.totalReturn = totalReturn;
|
|
96
|
+
exports.cagr = cagr;
|
|
97
|
+
exports.projectScenario = projectScenario;
|
|
98
|
+
exports.compareScenarios = compareScenarios;
|