@wickermoney/plugin-sdk 0.2.0 → 0.3.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/README.md +53 -2
- package/dist/index.d.ts +4 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/manifest/CORE_TABLES.d.ts.map +1 -1
- package/dist/manifest/CORE_TABLES.js +3 -2
- package/dist/manifest/CORE_TABLES.js.map +1 -1
- package/dist/money/MONEY_SCALE.d.ts +3 -0
- package/dist/money/MONEY_SCALE.d.ts.map +1 -0
- package/dist/money/MONEY_SCALE.js +3 -0
- package/dist/money/MONEY_SCALE.js.map +1 -0
- package/dist/money/MONEY_UNIT.d.ts +8 -0
- package/dist/money/MONEY_UNIT.d.ts.map +1 -0
- package/dist/money/MONEY_UNIT.js +8 -0
- package/dist/money/MONEY_UNIT.js.map +1 -0
- package/dist/money/Money.d.ts +12 -0
- package/dist/money/Money.d.ts.map +1 -0
- package/dist/money/Money.js +2 -0
- package/dist/money/Money.js.map +1 -0
- package/dist/money/ZERO_MONEY.d.ts +8 -0
- package/dist/money/ZERO_MONEY.d.ts.map +1 -0
- package/dist/money/ZERO_MONEY.js +8 -0
- package/dist/money/ZERO_MONEY.js.map +1 -0
- package/dist/money/absMoney.d.ts +9 -0
- package/dist/money/absMoney.d.ts.map +1 -0
- package/dist/money/absMoney.js +14 -0
- package/dist/money/absMoney.js.map +1 -0
- package/dist/money/addMoney.d.ts +12 -0
- package/dist/money/addMoney.d.ts.map +1 -0
- package/dist/money/addMoney.js +16 -0
- package/dist/money/addMoney.js.map +1 -0
- package/dist/money/compareMoney.d.ts +13 -0
- package/dist/money/compareMoney.d.ts.map +1 -0
- package/dist/money/compareMoney.js +18 -0
- package/dist/money/compareMoney.js.map +1 -0
- package/dist/money/divideUnits.d.ts +21 -0
- package/dist/money/divideUnits.d.ts.map +1 -0
- package/dist/money/divideUnits.js +29 -0
- package/dist/money/divideUnits.js.map +1 -0
- package/dist/money/editableMoney.d.ts +16 -0
- package/dist/money/editableMoney.d.ts.map +1 -0
- package/dist/money/editableMoney.js +27 -0
- package/dist/money/editableMoney.js.map +1 -0
- package/dist/money/equalMoney.d.ts +13 -0
- package/dist/money/equalMoney.d.ts.map +1 -0
- package/dist/money/equalMoney.js +16 -0
- package/dist/money/equalMoney.js.map +1 -0
- package/dist/money/index.d.ts +44 -0
- package/dist/money/index.d.ts.map +1 -0
- package/dist/money/index.js +18 -0
- package/dist/money/index.js.map +1 -0
- package/dist/money/isNegativeMoney.d.ts +9 -0
- package/dist/money/isNegativeMoney.d.ts.map +1 -0
- package/dist/money/isNegativeMoney.js +12 -0
- package/dist/money/isNegativeMoney.js.map +1 -0
- package/dist/money/isZeroMoney.d.ts +9 -0
- package/dist/money/isZeroMoney.d.ts.map +1 -0
- package/dist/money/isZeroMoney.js +12 -0
- package/dist/money/isZeroMoney.js.map +1 -0
- package/dist/money/moneyToUnits.d.ts +23 -0
- package/dist/money/moneyToUnits.d.ts.map +1 -0
- package/dist/money/moneyToUnits.js +37 -0
- package/dist/money/moneyToUnits.js.map +1 -0
- package/dist/money/negateMoney.d.ts +9 -0
- package/dist/money/negateMoney.d.ts.map +1 -0
- package/dist/money/negateMoney.js +13 -0
- package/dist/money/negateMoney.js.map +1 -0
- package/dist/money/normalizeMoney.d.ts +12 -0
- package/dist/money/normalizeMoney.d.ts.map +1 -0
- package/dist/money/normalizeMoney.js +16 -0
- package/dist/money/normalizeMoney.js.map +1 -0
- package/dist/money/subtractMoney.d.ts +10 -0
- package/dist/money/subtractMoney.d.ts.map +1 -0
- package/dist/money/subtractMoney.js +14 -0
- package/dist/money/subtractMoney.js.map +1 -0
- package/dist/money/sumMoney.d.ts +9 -0
- package/dist/money/sumMoney.d.ts.map +1 -0
- package/dist/money/sumMoney.js +13 -0
- package/dist/money/sumMoney.js.map +1 -0
- package/dist/money/unitsToMoney.d.ts +14 -0
- package/dist/money/unitsToMoney.d.ts.map +1 -0
- package/dist/money/unitsToMoney.js +23 -0
- package/dist/money/unitsToMoney.js.map +1 -0
- package/dist/recurrence/index.d.ts +2 -1
- package/dist/recurrence/index.d.ts.map +1 -1
- package/dist/recurrence/index.js +1 -0
- package/dist/recurrence/index.js.map +1 -1
- package/dist/recurrence/projection.d.ts +10 -2
- package/dist/recurrence/projection.d.ts.map +1 -1
- package/dist/recurrence/projection.js +38 -30
- package/dist/recurrence/projection.js.map +1 -1
- package/dist/recurrence/scheduled.d.ts +29 -0
- package/dist/recurrence/scheduled.d.ts.map +1 -0
- package/dist/recurrence/scheduled.js +108 -0
- package/dist/recurrence/scheduled.js.map +1 -0
- package/dist/recurrence/types.d.ts +42 -0
- package/dist/recurrence/types.d.ts.map +1 -1
- package/package.json +5 -1
- package/dist/recurrence/money.d.ts +0 -39
- package/dist/recurrence/money.d.ts.map +0 -1
- package/dist/recurrence/money.js +0 -58
- package/dist/recurrence/money.js.map +0 -1
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tests whether an amount is exactly zero.
|
|
3
|
+
*
|
|
4
|
+
* @param value - A decimal string.
|
|
5
|
+
* @returns `true` for any spelling of zero (`'0'`, `'0.00'`, `'-0.0000'`).
|
|
6
|
+
* @throws {RangeError} If `value` is not a decimal with at most four places.
|
|
7
|
+
*/
|
|
8
|
+
export declare function isZeroMoney(value: string): boolean;
|
|
9
|
+
//# sourceMappingURL=isZeroMoney.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"isZeroMoney.d.ts","sourceRoot":"","sources":["../../src/money/isZeroMoney.ts"],"names":[],"mappings":"AAEA;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAElD"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { moneyToUnits } from './moneyToUnits.js';
|
|
2
|
+
/**
|
|
3
|
+
* Tests whether an amount is exactly zero.
|
|
4
|
+
*
|
|
5
|
+
* @param value - A decimal string.
|
|
6
|
+
* @returns `true` for any spelling of zero (`'0'`, `'0.00'`, `'-0.0000'`).
|
|
7
|
+
* @throws {RangeError} If `value` is not a decimal with at most four places.
|
|
8
|
+
*/
|
|
9
|
+
export function isZeroMoney(value) {
|
|
10
|
+
return moneyToUnits(value) === 0n;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=isZeroMoney.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"isZeroMoney.js","sourceRoot":"","sources":["../../src/money/isZeroMoney.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAEhD;;;;;;GAMG;AACH,MAAM,UAAU,WAAW,CAAC,KAAa;IACvC,OAAO,YAAY,CAAC,KAAK,CAAC,KAAK,EAAE,CAAA;AACnC,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parses a decimal string into integer units of 0.0001, exactly.
|
|
3
|
+
*
|
|
4
|
+
* The parse is strict. More than four decimal places is an error, not a
|
|
5
|
+
* rounding: the column holds four, so a fifth cannot have come from the
|
|
6
|
+
* database, and quietly rounding or truncating a value someone typed would
|
|
7
|
+
* store a number they never entered. The API refuses the same input, so a
|
|
8
|
+
* plugin that validates with this agrees with the server. Surrounding
|
|
9
|
+
* whitespace is ignored; thousands separators, currency symbols, exponents, a
|
|
10
|
+
* leading `+` and a bare `.5` or `5.` are not accepted.
|
|
11
|
+
*
|
|
12
|
+
* There is no size limit: `bigint` is exact at any length, so totals larger
|
|
13
|
+
* than a single column could hold still add up correctly.
|
|
14
|
+
*
|
|
15
|
+
* @param value - A plain decimal string such as `'-81.2'`, `'2850.0000'` or `'0'`.
|
|
16
|
+
* @param label - What the value is, used in the error message. Defaults to `'amount'`.
|
|
17
|
+
* @returns The amount scaled by 10,000. `'-0'` returns `0n` (a `bigint` has no negative zero).
|
|
18
|
+
* @throws {RangeError} If `value` is not a string, not a plain decimal, or has more than four decimal places.
|
|
19
|
+
* @example
|
|
20
|
+
* moneyToUnits('-81.2') // -812_000n
|
|
21
|
+
*/
|
|
22
|
+
export declare function moneyToUnits(value: string, label?: string): bigint;
|
|
23
|
+
//# sourceMappingURL=moneyToUnits.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"moneyToUnits.d.ts","sourceRoot":"","sources":["../../src/money/moneyToUnits.ts"],"names":[],"mappings":"AAMA;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,SAAW,GAAG,MAAM,CAWpE"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { MONEY_SCALE } from './MONEY_SCALE.js';
|
|
2
|
+
import { MONEY_UNIT } from './MONEY_UNIT.js';
|
|
3
|
+
/** Optional minus, digits, and at most {@link MONEY_SCALE} fraction digits. */
|
|
4
|
+
const DECIMAL = /^-?\d+(?:\.\d{1,4})?$/;
|
|
5
|
+
/**
|
|
6
|
+
* Parses a decimal string into integer units of 0.0001, exactly.
|
|
7
|
+
*
|
|
8
|
+
* The parse is strict. More than four decimal places is an error, not a
|
|
9
|
+
* rounding: the column holds four, so a fifth cannot have come from the
|
|
10
|
+
* database, and quietly rounding or truncating a value someone typed would
|
|
11
|
+
* store a number they never entered. The API refuses the same input, so a
|
|
12
|
+
* plugin that validates with this agrees with the server. Surrounding
|
|
13
|
+
* whitespace is ignored; thousands separators, currency symbols, exponents, a
|
|
14
|
+
* leading `+` and a bare `.5` or `5.` are not accepted.
|
|
15
|
+
*
|
|
16
|
+
* There is no size limit: `bigint` is exact at any length, so totals larger
|
|
17
|
+
* than a single column could hold still add up correctly.
|
|
18
|
+
*
|
|
19
|
+
* @param value - A plain decimal string such as `'-81.2'`, `'2850.0000'` or `'0'`.
|
|
20
|
+
* @param label - What the value is, used in the error message. Defaults to `'amount'`.
|
|
21
|
+
* @returns The amount scaled by 10,000. `'-0'` returns `0n` (a `bigint` has no negative zero).
|
|
22
|
+
* @throws {RangeError} If `value` is not a string, not a plain decimal, or has more than four decimal places.
|
|
23
|
+
* @example
|
|
24
|
+
* moneyToUnits('-81.2') // -812_000n
|
|
25
|
+
*/
|
|
26
|
+
export function moneyToUnits(value, label = 'amount') {
|
|
27
|
+
const text = typeof value === 'string' ? value.trim() : '';
|
|
28
|
+
if (!DECIMAL.test(text)) {
|
|
29
|
+
const shown = typeof value === 'string' ? JSON.stringify(value) : `${typeof value} ${String(value)}`;
|
|
30
|
+
throw new RangeError(`Invalid ${label}: ${shown}`);
|
|
31
|
+
}
|
|
32
|
+
const negative = text.startsWith('-');
|
|
33
|
+
const [whole = '0', fraction = ''] = (negative ? text.slice(1) : text).split('.');
|
|
34
|
+
const units = BigInt(whole) * MONEY_UNIT + BigInt(fraction.padEnd(MONEY_SCALE, '0'));
|
|
35
|
+
return negative ? -units : units;
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=moneyToUnits.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"moneyToUnits.js","sourceRoot":"","sources":["../../src/money/moneyToUnits.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAC9C,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAE5C,+EAA+E;AAC/E,MAAM,OAAO,GAAG,uBAAuB,CAAA;AAEvC;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,UAAU,YAAY,CAAC,KAAa,EAAE,KAAK,GAAG,QAAQ;IAC1D,MAAM,IAAI,GAAG,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAAA;IAC1D,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;QACxB,MAAM,KAAK,GAAG,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,GAAG,OAAO,KAAK,IAAI,MAAM,CAAC,KAAK,CAAC,EAAE,CAAA;QACpG,MAAM,IAAI,UAAU,CAAC,WAAW,KAAK,KAAK,KAAK,EAAE,CAAC,CAAA;IACpD,CAAC;IAED,MAAM,QAAQ,GAAG,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAA;IACrC,MAAM,CAAC,KAAK,GAAG,GAAG,EAAE,QAAQ,GAAG,EAAE,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;IACjF,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC,GAAG,UAAU,GAAG,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,WAAW,EAAE,GAAG,CAAC,CAAC,CAAA;IACpF,OAAO,QAAQ,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,CAAA;AAClC,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Flips the sign of an amount.
|
|
3
|
+
*
|
|
4
|
+
* @param value - A decimal string.
|
|
5
|
+
* @returns `-value` as a four-decimal string; zero stays `'0.0000'`.
|
|
6
|
+
* @throws {RangeError} If `value` is not a decimal with at most four places.
|
|
7
|
+
*/
|
|
8
|
+
export declare function negateMoney(value: string): string;
|
|
9
|
+
//# sourceMappingURL=negateMoney.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"negateMoney.d.ts","sourceRoot":"","sources":["../../src/money/negateMoney.ts"],"names":[],"mappings":"AAGA;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAEjD"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { moneyToUnits } from './moneyToUnits.js';
|
|
2
|
+
import { unitsToMoney } from './unitsToMoney.js';
|
|
3
|
+
/**
|
|
4
|
+
* Flips the sign of an amount.
|
|
5
|
+
*
|
|
6
|
+
* @param value - A decimal string.
|
|
7
|
+
* @returns `-value` as a four-decimal string; zero stays `'0.0000'`.
|
|
8
|
+
* @throws {RangeError} If `value` is not a decimal with at most four places.
|
|
9
|
+
*/
|
|
10
|
+
export function negateMoney(value) {
|
|
11
|
+
return unitsToMoney(-moneyToUnits(value));
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=negateMoney.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"negateMoney.js","sourceRoot":"","sources":["../../src/money/negateMoney.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAChD,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAEhD;;;;;;GAMG;AACH,MAAM,UAAU,WAAW,CAAC,KAAa;IACvC,OAAO,YAAY,CAAC,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC,CAAA;AAC3C,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rewrites an amount in canonical four-decimal form.
|
|
3
|
+
*
|
|
4
|
+
* Useful before using an amount as a map key or comparing it as text:
|
|
5
|
+
* `'-81.20'`, `' -81.2 '` and `'-81.2000'` all become `'-81.2000'`.
|
|
6
|
+
*
|
|
7
|
+
* @param value - A decimal string.
|
|
8
|
+
* @returns The same amount as a four-decimal string; any zero becomes `'0.0000'`.
|
|
9
|
+
* @throws {RangeError} If `value` is not a decimal with at most four places.
|
|
10
|
+
*/
|
|
11
|
+
export declare function normalizeMoney(value: string): string;
|
|
12
|
+
//# sourceMappingURL=normalizeMoney.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"normalizeMoney.d.ts","sourceRoot":"","sources":["../../src/money/normalizeMoney.ts"],"names":[],"mappings":"AAGA;;;;;;;;;GASG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAEpD"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { moneyToUnits } from './moneyToUnits.js';
|
|
2
|
+
import { unitsToMoney } from './unitsToMoney.js';
|
|
3
|
+
/**
|
|
4
|
+
* Rewrites an amount in canonical four-decimal form.
|
|
5
|
+
*
|
|
6
|
+
* Useful before using an amount as a map key or comparing it as text:
|
|
7
|
+
* `'-81.20'`, `' -81.2 '` and `'-81.2000'` all become `'-81.2000'`.
|
|
8
|
+
*
|
|
9
|
+
* @param value - A decimal string.
|
|
10
|
+
* @returns The same amount as a four-decimal string; any zero becomes `'0.0000'`.
|
|
11
|
+
* @throws {RangeError} If `value` is not a decimal with at most four places.
|
|
12
|
+
*/
|
|
13
|
+
export function normalizeMoney(value) {
|
|
14
|
+
return unitsToMoney(moneyToUnits(value));
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=normalizeMoney.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"normalizeMoney.js","sourceRoot":"","sources":["../../src/money/normalizeMoney.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAChD,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAEhD;;;;;;;;;GASG;AACH,MAAM,UAAU,cAAc,CAAC,KAAa;IAC1C,OAAO,YAAY,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC,CAAA;AAC1C,CAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Subtracts one amount from another exactly.
|
|
3
|
+
*
|
|
4
|
+
* @param a - Amount to subtract from, as a decimal string.
|
|
5
|
+
* @param b - Amount to subtract, as a decimal string.
|
|
6
|
+
* @returns `a - b` as a four-decimal string.
|
|
7
|
+
* @throws {RangeError} If either argument is not a decimal with at most four places.
|
|
8
|
+
*/
|
|
9
|
+
export declare function subtractMoney(a: string, b: string): string;
|
|
10
|
+
//# sourceMappingURL=subtractMoney.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"subtractMoney.d.ts","sourceRoot":"","sources":["../../src/money/subtractMoney.ts"],"names":[],"mappings":"AAGA;;;;;;;GAOG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAE1D"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { moneyToUnits } from './moneyToUnits.js';
|
|
2
|
+
import { unitsToMoney } from './unitsToMoney.js';
|
|
3
|
+
/**
|
|
4
|
+
* Subtracts one amount from another exactly.
|
|
5
|
+
*
|
|
6
|
+
* @param a - Amount to subtract from, as a decimal string.
|
|
7
|
+
* @param b - Amount to subtract, as a decimal string.
|
|
8
|
+
* @returns `a - b` as a four-decimal string.
|
|
9
|
+
* @throws {RangeError} If either argument is not a decimal with at most four places.
|
|
10
|
+
*/
|
|
11
|
+
export function subtractMoney(a, b) {
|
|
12
|
+
return unitsToMoney(moneyToUnits(a) - moneyToUnits(b));
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=subtractMoney.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"subtractMoney.js","sourceRoot":"","sources":["../../src/money/subtractMoney.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAChD,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAEhD;;;;;;;GAOG;AACH,MAAM,UAAU,aAAa,CAAC,CAAS,EAAE,CAAS;IAChD,OAAO,YAAY,CAAC,YAAY,CAAC,CAAC,CAAC,GAAG,YAAY,CAAC,CAAC,CAAC,CAAC,CAAA;AACxD,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Totals a list of amounts exactly.
|
|
3
|
+
*
|
|
4
|
+
* @param values - Decimal strings to add; may be empty.
|
|
5
|
+
* @returns The total as a four-decimal string, `'0.0000'` for an empty list.
|
|
6
|
+
* @throws {RangeError} If any element is not a decimal with at most four places.
|
|
7
|
+
*/
|
|
8
|
+
export declare function sumMoney(values: readonly string[]): string;
|
|
9
|
+
//# sourceMappingURL=sumMoney.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sumMoney.d.ts","sourceRoot":"","sources":["../../src/money/sumMoney.ts"],"names":[],"mappings":"AAGA;;;;;;GAMG;AACH,wBAAgB,QAAQ,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,CAE1D"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { moneyToUnits } from './moneyToUnits.js';
|
|
2
|
+
import { unitsToMoney } from './unitsToMoney.js';
|
|
3
|
+
/**
|
|
4
|
+
* Totals a list of amounts exactly.
|
|
5
|
+
*
|
|
6
|
+
* @param values - Decimal strings to add; may be empty.
|
|
7
|
+
* @returns The total as a four-decimal string, `'0.0000'` for an empty list.
|
|
8
|
+
* @throws {RangeError} If any element is not a decimal with at most four places.
|
|
9
|
+
*/
|
|
10
|
+
export function sumMoney(values) {
|
|
11
|
+
return unitsToMoney(values.reduce((total, v) => total + moneyToUnits(v), 0n));
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=sumMoney.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sumMoney.js","sourceRoot":"","sources":["../../src/money/sumMoney.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAChD,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAA;AAEhD;;;;;;GAMG;AACH,MAAM,UAAU,QAAQ,CAAC,MAAyB;IAChD,OAAO,YAAY,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,CAAC,EAAE,EAAE,CAAC,KAAK,GAAG,YAAY,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAA;AAC/E,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Formats integer units of 0.0001 as a canonical four-decimal string.
|
|
3
|
+
*
|
|
4
|
+
* This is the storage form, not a display form: use the host's
|
|
5
|
+
* `ctx.formatMoney` to show an amount to a person.
|
|
6
|
+
*
|
|
7
|
+
* @param units - An amount scaled by 10,000, as {@link moneyToUnits} returns it.
|
|
8
|
+
* @returns A string such as `'-81.2000'`. Zero is always `'0.0000'`, never `'-0.0000'`.
|
|
9
|
+
* @throws {TypeError} If `units` is not a `bigint`.
|
|
10
|
+
* @example
|
|
11
|
+
* unitsToMoney(-812_000n) // '-81.2000'
|
|
12
|
+
*/
|
|
13
|
+
export declare function unitsToMoney(units: bigint): string;
|
|
14
|
+
//# sourceMappingURL=unitsToMoney.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"unitsToMoney.d.ts","sourceRoot":"","sources":["../../src/money/unitsToMoney.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;GAWG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAMlD"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { MONEY_SCALE } from './MONEY_SCALE.js';
|
|
2
|
+
import { MONEY_UNIT } from './MONEY_UNIT.js';
|
|
3
|
+
/**
|
|
4
|
+
* Formats integer units of 0.0001 as a canonical four-decimal string.
|
|
5
|
+
*
|
|
6
|
+
* This is the storage form, not a display form: use the host's
|
|
7
|
+
* `ctx.formatMoney` to show an amount to a person.
|
|
8
|
+
*
|
|
9
|
+
* @param units - An amount scaled by 10,000, as {@link moneyToUnits} returns it.
|
|
10
|
+
* @returns A string such as `'-81.2000'`. Zero is always `'0.0000'`, never `'-0.0000'`.
|
|
11
|
+
* @throws {TypeError} If `units` is not a `bigint`.
|
|
12
|
+
* @example
|
|
13
|
+
* unitsToMoney(-812_000n) // '-81.2000'
|
|
14
|
+
*/
|
|
15
|
+
export function unitsToMoney(units) {
|
|
16
|
+
if (typeof units !== 'bigint')
|
|
17
|
+
throw new TypeError(`Expected bigint units, got ${typeof units}`);
|
|
18
|
+
const negative = units < 0n;
|
|
19
|
+
const absolute = negative ? -units : units;
|
|
20
|
+
const fraction = (absolute % MONEY_UNIT).toString().padStart(MONEY_SCALE, '0');
|
|
21
|
+
return `${negative ? '-' : ''}${absolute / MONEY_UNIT}.${fraction}`;
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=unitsToMoney.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"unitsToMoney.js","sourceRoot":"","sources":["../../src/money/unitsToMoney.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAA;AAC9C,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAE5C;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,YAAY,CAAC,KAAa;IACxC,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,MAAM,IAAI,SAAS,CAAC,8BAA8B,OAAO,KAAK,EAAE,CAAC,CAAA;IAChG,MAAM,QAAQ,GAAG,KAAK,GAAG,EAAE,CAAA;IAC3B,MAAM,QAAQ,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,CAAA;IAC1C,MAAM,QAAQ,GAAG,CAAC,QAAQ,GAAG,UAAU,CAAC,CAAC,QAAQ,EAAE,CAAC,QAAQ,CAAC,WAAW,EAAE,GAAG,CAAC,CAAA;IAC9E,OAAO,GAAG,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,GAAG,QAAQ,GAAG,UAAU,IAAI,QAAQ,EAAE,CAAA;AACrE,CAAC"}
|
|
@@ -11,7 +11,8 @@
|
|
|
11
11
|
* `DashboardRange`.
|
|
12
12
|
*/
|
|
13
13
|
export { DEFAULT_SEMIMONTHLY_DAYS, RECURRENCE_FREQUENCIES } from './types.js';
|
|
14
|
-
export type { DailyBalance, FlowTotals, RecurrenceFrequency, RecurrenceSchedule, RecurringItem, RecurringLeg, } from './types.js';
|
|
14
|
+
export type { DailyBalance, FlowTotals, OccurrenceOverride, RecurrenceFrequency, RecurrenceSchedule, RecurringItem, RecurringLeg, ScheduledOccurrence, } from './types.js';
|
|
15
15
|
export { nextOccurrence, occurrences } from './schedule.js';
|
|
16
|
+
export { nextScheduledOccurrence, scheduledOccurrences } from './scheduled.js';
|
|
16
17
|
export { dailyBalances, flowTotals, monthlyEquivalent, nextPayday, } from './projection.js';
|
|
17
18
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/recurrence/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,OAAO,EAAE,wBAAwB,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAA;AAC7E,YAAY,EACV,YAAY,EAAE,UAAU,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,aAAa,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/recurrence/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,OAAO,EAAE,wBAAwB,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAA;AAC7E,YAAY,EACV,YAAY,EAAE,UAAU,EAAE,kBAAkB,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,aAAa,EACpG,YAAY,EAAE,mBAAmB,GAClC,MAAM,YAAY,CAAA;AACnB,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAC3D,OAAO,EAAE,uBAAuB,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAA;AAC9E,OAAO,EACL,aAAa,EAAE,UAAU,EAAE,iBAAiB,EAAE,UAAU,GACzD,MAAM,iBAAiB,CAAA"}
|
package/dist/recurrence/index.js
CHANGED
|
@@ -12,5 +12,6 @@
|
|
|
12
12
|
*/
|
|
13
13
|
export { DEFAULT_SEMIMONTHLY_DAYS, RECURRENCE_FREQUENCIES } from './types.js';
|
|
14
14
|
export { nextOccurrence, occurrences } from './schedule.js';
|
|
15
|
+
export { nextScheduledOccurrence, scheduledOccurrences } from './scheduled.js';
|
|
15
16
|
export { dailyBalances, flowTotals, monthlyEquivalent, nextPayday, } from './projection.js';
|
|
16
17
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/recurrence/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,OAAO,EAAE,wBAAwB,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAA;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/recurrence/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,OAAO,EAAE,wBAAwB,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAA;AAK7E,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAC3D,OAAO,EAAE,uBAAuB,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAA;AAC9E,OAAO,EACL,aAAa,EAAE,UAAU,EAAE,iBAAiB,EAAE,UAAU,GACzD,MAAM,iBAAiB,CAAA"}
|
|
@@ -11,7 +11,8 @@ import type { DailyBalance, FlowTotals, RecurrenceFrequency, RecurringItem } fro
|
|
|
11
11
|
* @param frequency - How often it occurs.
|
|
12
12
|
* @returns The signed monthly amount as a four-decimal string, rounded half
|
|
13
13
|
* away from zero. `'0.0000'` for `once`, which has no monthly rate.
|
|
14
|
-
* @throws {RangeError} If `amount` is not a decimal
|
|
14
|
+
* @throws {RangeError} If `amount` is not a decimal with at most four places, or
|
|
15
|
+
* `frequency` is unknown.
|
|
15
16
|
*/
|
|
16
17
|
export declare function monthlyEquivalent(amount: string, frequency: RecurrenceFrequency): string;
|
|
17
18
|
/**
|
|
@@ -21,6 +22,10 @@ export declare function monthlyEquivalent(amount: string, frequency: RecurrenceF
|
|
|
21
22
|
* anything that landed today. Every income item counts, so two people paid
|
|
22
23
|
* biweekly on offset weeks give a payday every week.
|
|
23
24
|
*
|
|
25
|
+
* Overrides apply: a skipped paycheck is not a payday, one moved is a payday
|
|
26
|
+
* on the date it lands, and one whose override leaves no legs (it already
|
|
27
|
+
* arrived) is passed over for the next.
|
|
28
|
+
*
|
|
24
29
|
* @param items - All of the user's recurring items; non-income items are ignored.
|
|
25
30
|
* @param today - The user's today, `YYYY-MM-DD`, in their time zone.
|
|
26
31
|
* @returns The date, or `null` when no income occurs after today (the caller
|
|
@@ -41,6 +46,9 @@ export declare function nextPayday(items: readonly RecurringItem[], today: strin
|
|
|
41
46
|
* no activity, so a chart can plot the series directly. Each day also carries
|
|
42
47
|
* its `low`: the balance after that day's outflows but before its inflows.
|
|
43
48
|
*
|
|
49
|
+
* Per-occurrence overrides apply (see `scheduledOccurrences`): each
|
|
50
|
+
* occurrence moves its own legs on the date it lands.
|
|
51
|
+
*
|
|
44
52
|
* @param items - Recurring items to apply.
|
|
45
53
|
* @param startingBalances - Balance per account id at the end of the day
|
|
46
54
|
* before `from`, as decimal strings. Only these accounts are projected.
|
|
@@ -58,7 +66,7 @@ export declare function dailyBalances(items: readonly RecurringItem[], startingB
|
|
|
58
66
|
* listed accounts (checking to savings, with both listed) nets to zero and is
|
|
59
67
|
* never spending, while the same transfer measured on checking alone is an
|
|
60
68
|
* outflow. A payment from checking to a loan, with only cash accounts listed,
|
|
61
|
-
* is an outflow.
|
|
69
|
+
* is an outflow. Per-occurrence overrides apply.
|
|
62
70
|
*
|
|
63
71
|
* @param items - Recurring items to total.
|
|
64
72
|
* @param accountIds - The accounts that make up "the household" for this total.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"projection.d.ts","sourceRoot":"","sources":["../../src/recurrence/projection.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"projection.d.ts","sourceRoot":"","sources":["../../src/recurrence/projection.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EACV,YAAY,EAAE,UAAU,EAAE,mBAAmB,EAAE,aAAa,EAC7D,MAAM,YAAY,CAAA;AAoBnB;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,mBAAmB,GAAG,MAAM,CAMxF;AAcD;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,SAAS,aAAa,EAAE,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CASxF;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,aAAa,CAC3B,KAAK,EAAE,SAAS,aAAa,EAAE,EAC/B,gBAAgB,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,EAClD,IAAI,EAAE,MAAM,EACZ,EAAE,EAAE,MAAM,GACT,MAAM,CAAC,MAAM,EAAE,YAAY,EAAE,CAAC,CA4ChC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,UAAU,CACxB,KAAK,EAAE,SAAS,aAAa,EAAE,EAC/B,UAAU,EAAE,SAAS,MAAM,EAAE,EAC7B,IAAI,EAAE,MAAM,EACZ,EAAE,EAAE,MAAM,GACT,UAAU,CAgBZ"}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { fromDayNumber, toDayNumber } from './calendar.js';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { divideUnits } from '../money/divideUnits.js';
|
|
3
|
+
import { moneyToUnits } from '../money/moneyToUnits.js';
|
|
4
|
+
import { unitsToMoney } from '../money/unitsToMoney.js';
|
|
5
|
+
import { nextScheduledOccurrence, scheduledOccurrences } from './scheduled.js';
|
|
4
6
|
/**
|
|
5
7
|
* Occurrences per month for each frequency, as an exact fraction.
|
|
6
8
|
*
|
|
@@ -30,14 +32,15 @@ const PER_MONTH = {
|
|
|
30
32
|
* @param frequency - How often it occurs.
|
|
31
33
|
* @returns The signed monthly amount as a four-decimal string, rounded half
|
|
32
34
|
* away from zero. `'0.0000'` for `once`, which has no monthly rate.
|
|
33
|
-
* @throws {RangeError} If `amount` is not a decimal
|
|
35
|
+
* @throws {RangeError} If `amount` is not a decimal with at most four places, or
|
|
36
|
+
* `frequency` is unknown.
|
|
34
37
|
*/
|
|
35
38
|
export function monthlyEquivalent(amount, frequency) {
|
|
36
39
|
if (!Object.hasOwn(PER_MONTH, frequency)) {
|
|
37
40
|
throw new RangeError(`Unknown recurrence frequency: ${JSON.stringify(frequency)}`);
|
|
38
41
|
}
|
|
39
42
|
const [numerator, denominator] = PER_MONTH[frequency];
|
|
40
|
-
return
|
|
43
|
+
return unitsToMoney(divideUnits(moneyToUnits(amount) * numerator, denominator));
|
|
41
44
|
}
|
|
42
45
|
/**
|
|
43
46
|
* Whether an item brings money into the household: it has legs and every leg
|
|
@@ -48,7 +51,7 @@ export function monthlyEquivalent(amount, frequency) {
|
|
|
48
51
|
* @returns `true` for income.
|
|
49
52
|
*/
|
|
50
53
|
function isIncome(item) {
|
|
51
|
-
return item.legs.length > 0 && item.legs.every((leg) =>
|
|
54
|
+
return item.legs.length > 0 && item.legs.every((leg) => moneyToUnits(leg.amount, 'leg amount') > 0n);
|
|
52
55
|
}
|
|
53
56
|
/**
|
|
54
57
|
* The household's next payday: the earliest income, into any account, after today.
|
|
@@ -57,6 +60,10 @@ function isIncome(item) {
|
|
|
57
60
|
* anything that landed today. Every income item counts, so two people paid
|
|
58
61
|
* biweekly on offset weeks give a payday every week.
|
|
59
62
|
*
|
|
63
|
+
* Overrides apply: a skipped paycheck is not a payday, one moved is a payday
|
|
64
|
+
* on the date it lands, and one whose override leaves no legs (it already
|
|
65
|
+
* arrived) is passed over for the next.
|
|
66
|
+
*
|
|
60
67
|
* @param items - All of the user's recurring items; non-income items are ignored.
|
|
61
68
|
* @param today - The user's today, `YYYY-MM-DD`, in their time zone.
|
|
62
69
|
* @returns The date, or `null` when no income occurs after today (the caller
|
|
@@ -69,7 +76,7 @@ export function nextPayday(items, today) {
|
|
|
69
76
|
for (const item of items) {
|
|
70
77
|
if (!isIncome(item))
|
|
71
78
|
continue;
|
|
72
|
-
const next =
|
|
79
|
+
const next = nextScheduledOccurrence(item, tomorrow)?.date ?? null;
|
|
73
80
|
if (next !== null && (earliest === null || next < earliest))
|
|
74
81
|
earliest = next;
|
|
75
82
|
}
|
|
@@ -88,6 +95,9 @@ export function nextPayday(items, today) {
|
|
|
88
95
|
* no activity, so a chart can plot the series directly. Each day also carries
|
|
89
96
|
* its `low`: the balance after that day's outflows but before its inflows.
|
|
90
97
|
*
|
|
98
|
+
* Per-occurrence overrides apply (see `scheduledOccurrences`): each
|
|
99
|
+
* occurrence moves its own legs on the date it lands.
|
|
100
|
+
*
|
|
91
101
|
* @param items - Recurring items to apply.
|
|
92
102
|
* @param startingBalances - Balance per account id at the end of the day
|
|
93
103
|
* before `from`, as decimal strings. Only these accounts are projected.
|
|
@@ -103,7 +113,7 @@ export function dailyBalances(items, startingBalances, from, to) {
|
|
|
103
113
|
throw new RangeError(`Range end ${to} is before its start ${from}`);
|
|
104
114
|
const accounts = new Map();
|
|
105
115
|
for (const [accountId, balance] of Object.entries(startingBalances)) {
|
|
106
|
-
accounts.set(accountId,
|
|
116
|
+
accounts.set(accountId, moneyToUnits(balance, `starting balance for ${accountId}`));
|
|
107
117
|
}
|
|
108
118
|
// Per account, the money out and the money in on each day offset from
|
|
109
119
|
// `first`, kept apart so a day's low point can apply outflows first.
|
|
@@ -114,15 +124,13 @@ export function dailyBalances(items, startingBalances, from, to) {
|
|
|
114
124
|
ins.set(accountId, new Array(last - first).fill(0n));
|
|
115
125
|
}
|
|
116
126
|
for (const item of items) {
|
|
117
|
-
const
|
|
118
|
-
|
|
119
|
-
const
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
const offset = toDayNumber(date) - first;
|
|
125
|
-
series[offset] = (series[offset] ?? 0n) + amount;
|
|
127
|
+
for (const occurrence of scheduledOccurrences(item, from, to)) {
|
|
128
|
+
const offset = toDayNumber(occurrence.date) - first;
|
|
129
|
+
for (const leg of occurrence.legs) {
|
|
130
|
+
const amount = moneyToUnits(leg.amount, 'leg amount');
|
|
131
|
+
const series = (amount < 0n ? outs : ins).get(leg.accountId);
|
|
132
|
+
if (series)
|
|
133
|
+
series[offset] = (series[offset] ?? 0n) + amount;
|
|
126
134
|
}
|
|
127
135
|
}
|
|
128
136
|
}
|
|
@@ -135,7 +143,7 @@ export function dailyBalances(items, startingBalances, from, to) {
|
|
|
135
143
|
for (let offset = 0; offset < last - first; offset += 1) {
|
|
136
144
|
const low = running + (out[offset] ?? 0n);
|
|
137
145
|
running = low + (into[offset] ?? 0n);
|
|
138
|
-
days.push({ date: fromDayNumber(first + offset), balance:
|
|
146
|
+
days.push({ date: fromDayNumber(first + offset), balance: unitsToMoney(running), low: unitsToMoney(low) });
|
|
139
147
|
}
|
|
140
148
|
result.push([accountId, days]);
|
|
141
149
|
}
|
|
@@ -149,7 +157,7 @@ export function dailyBalances(items, startingBalances, from, to) {
|
|
|
149
157
|
* listed accounts (checking to savings, with both listed) nets to zero and is
|
|
150
158
|
* never spending, while the same transfer measured on checking alone is an
|
|
151
159
|
* outflow. A payment from checking to a loan, with only cash accounts listed,
|
|
152
|
-
* is an outflow.
|
|
160
|
+
* is an outflow. Per-occurrence overrides apply.
|
|
153
161
|
*
|
|
154
162
|
* @param items - Recurring items to total.
|
|
155
163
|
* @param accountIds - The accounts that make up "the household" for this total.
|
|
@@ -163,19 +171,19 @@ export function flowTotals(items, accountIds, from, to) {
|
|
|
163
171
|
let inflow = 0n;
|
|
164
172
|
let outflow = 0n;
|
|
165
173
|
for (const item of items) {
|
|
166
|
-
const
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
174
|
+
for (const occurrence of scheduledOccurrences(item, from, to)) {
|
|
175
|
+
let net = 0n;
|
|
176
|
+
for (const leg of occurrence.legs) {
|
|
177
|
+
const amount = moneyToUnits(leg.amount, 'leg amount');
|
|
178
|
+
if (inSet.has(leg.accountId))
|
|
179
|
+
net += amount;
|
|
180
|
+
}
|
|
181
|
+
if (net > 0n)
|
|
182
|
+
inflow += net;
|
|
183
|
+
else
|
|
184
|
+
outflow += net;
|
|
172
185
|
}
|
|
173
|
-
const total = perOccurrence * BigInt(dates.length);
|
|
174
|
-
if (total > 0n)
|
|
175
|
-
inflow += total;
|
|
176
|
-
else
|
|
177
|
-
outflow += total;
|
|
178
186
|
}
|
|
179
|
-
return { inflow:
|
|
187
|
+
return { inflow: unitsToMoney(inflow), outflow: unitsToMoney(outflow), net: unitsToMoney(inflow + outflow) };
|
|
180
188
|
}
|
|
181
189
|
//# sourceMappingURL=projection.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"projection.js","sourceRoot":"","sources":["../../src/recurrence/projection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAC1D,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"projection.js","sourceRoot":"","sources":["../../src/recurrence/projection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAC1D,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAA;AACrD,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAA;AACvD,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAA;AACvD,OAAO,EAAE,uBAAuB,EAAE,oB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K,MAAM,GAAG,IAAI,UAAU,CAAC,IAAI,EAAE,CAAC;gBAClC,MAAM,MAAM,GAAG,YAAY,CAAC,GAAG,CAAC,MAAM,EAAE,YAAY,CAAC,CAAA;gBACrD,IAAI,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,SAAS,CAAC;oBAAE,GAAG,IAAI,MAAM,CAAA;YAC7C,CAAC;YACD,IAAI,GAAG,GAAG,EAAE;gBAAE,MAAM,IAAI,GAAG,CAAA;;gBACtB,OAAO,IAAI,GAAG,CAAA;QACrB,CAAC;IACH,CAAC;IACD,OAAO,EAAE,MAAM,EAAE,YAAY,CAAC,MAAM,CAAC,EAAE,OAAO,EAAE,YAAY,CAAC,OAAO,CAAC,EAAE,GAAG,EAAE,YAAY,CAAC,MAAM,GAAG,OAAO,CAAC,EAAE,CAAA;AAC9G,CAAC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { RecurringItem, ScheduledOccurrence } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Lists an item's occurrences that land in a half-open range, after its
|
|
4
|
+
* per-occurrence overrides.
|
|
5
|
+
*
|
|
6
|
+
* An occurrence is placed by the date it lands on, so one moved into the
|
|
7
|
+
* range from outside it is included and one moved out is not. Skipped
|
|
8
|
+
* occurrences are left out. Overrides keyed by a date that is not one of the
|
|
9
|
+
* schedule's are ignored.
|
|
10
|
+
*
|
|
11
|
+
* @param item - The item, with any `overrides`.
|
|
12
|
+
* @param from - First day to include, `YYYY-MM-DD`.
|
|
13
|
+
* @param to - First day to exclude, `YYYY-MM-DD`.
|
|
14
|
+
* @returns Occurrences ordered by the date they land, then nominal date.
|
|
15
|
+
* @throws {RangeError} If the schedule, a date, an override date or `to` is invalid.
|
|
16
|
+
*/
|
|
17
|
+
export declare function scheduledOccurrences(item: RecurringItem, from: string, to: string): ScheduledOccurrence[];
|
|
18
|
+
/**
|
|
19
|
+
* The first occurrence that lands on or after a date and still moves money,
|
|
20
|
+
* after overrides. Skipped occurrences and ones whose override leaves no legs
|
|
21
|
+
* are passed over.
|
|
22
|
+
*
|
|
23
|
+
* @param item - The item, with any `overrides`.
|
|
24
|
+
* @param from - Earliest date to consider, inclusive, `YYYY-MM-DD`.
|
|
25
|
+
* @returns The occurrence, or `null` if none is left.
|
|
26
|
+
* @throws {RangeError} If the schedule, a date or an override date is invalid.
|
|
27
|
+
*/
|
|
28
|
+
export declare function nextScheduledOccurrence(item: RecurringItem, from: string): ScheduledOccurrence | null;
|
|
29
|
+
//# sourceMappingURL=scheduled.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scheduled.d.ts","sourceRoot":"","sources":["../../src/recurrence/scheduled.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAsB,aAAa,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAA;AAExF;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,aAAa,EAAE,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,mBAAmB,EAAE,CAqBzG;AAED;;;;;;;;;GASG;AACH,wBAAgB,uBAAuB,CAAC,IAAI,EAAE,aAAa,EAAE,IAAI,EAAE,MAAM,GAAG,mBAAmB,GAAG,IAAI,CA0BrG"}
|