lease-term-calculator 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.
- package/LICENSE +21 -0
- package/README.md +55 -0
- package/dist/DateCalculator.d.ts +10 -0
- package/dist/DateCalculator.js +74 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +8 -0
- package/package.json +61 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Sher Ghan
|
|
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
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# lease-term-calculator
|
|
2
|
+
|
|
3
|
+
Calculate lease term duration in years and days.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install lease-term-calculator
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import calculator from 'lease-term-calculator';
|
|
15
|
+
// or
|
|
16
|
+
import { DateCalculator } from 'lease-term-calculator';
|
|
17
|
+
|
|
18
|
+
const calc = new DateCalculator();
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
### parseUIDate
|
|
22
|
+
|
|
23
|
+
Parses and validates date components.
|
|
24
|
+
|
|
25
|
+
```typescript
|
|
26
|
+
const date = calculator.parseUIDate(2024, 6, 15);
|
|
27
|
+
// Returns: Date object, '' (empty input), or 'bad date'
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### calculateTermOfLease
|
|
31
|
+
|
|
32
|
+
Calculates the lease term between dates.
|
|
33
|
+
|
|
34
|
+
```typescript
|
|
35
|
+
const effective = new Date(2024, 0, 1);
|
|
36
|
+
const start = new Date(2024, 0, 1);
|
|
37
|
+
const end = new Date(2026, 5, 30);
|
|
38
|
+
|
|
39
|
+
const result = calculator.calculateTermOfLease(effective, start, end);
|
|
40
|
+
// Returns: { years: 2, days: 181, daysInPartialYear: 365 }
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
**Parameters:**
|
|
44
|
+
- `effectiveDate` - The effective date (lease won't start before this)
|
|
45
|
+
- `startDate` - The lease start date
|
|
46
|
+
- `endDate` - The lease end date
|
|
47
|
+
|
|
48
|
+
**Returns:**
|
|
49
|
+
- `years` - Number of complete years
|
|
50
|
+
- `days` - Number of additional days beyond complete years
|
|
51
|
+
- `daysInPartialYear` - Total days in the partial year (for calculating fractions)
|
|
52
|
+
|
|
53
|
+
## License
|
|
54
|
+
|
|
55
|
+
MIT
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export interface LeaseTermResult {
|
|
2
|
+
years: number;
|
|
3
|
+
days: number;
|
|
4
|
+
daysInPartialYear: number;
|
|
5
|
+
}
|
|
6
|
+
export type ParseDateResult = Date | '' | 'bad date';
|
|
7
|
+
export declare class DateCalculator {
|
|
8
|
+
parseUIDate(year: string | number | null | undefined, month: string | number | null | undefined, day: string | number | null | undefined): ParseDateResult;
|
|
9
|
+
calculateTermOfLease(effectiveDate: Date, startDate: Date, endDate: Date): LeaseTermResult;
|
|
10
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DateCalculator = void 0;
|
|
4
|
+
class DateCalculator {
|
|
5
|
+
parseUIDate(year, month, day) {
|
|
6
|
+
const yearStr = String(year || 'empty');
|
|
7
|
+
const monthStr = String(month || 'empty');
|
|
8
|
+
const dayStr = String(day || 'empty');
|
|
9
|
+
if (yearStr + monthStr + dayStr === 'emptyemptyempty') {
|
|
10
|
+
return '';
|
|
11
|
+
}
|
|
12
|
+
const date = new Date(Number(year), Number(month) - 1, Number(day));
|
|
13
|
+
if (isNaN(date.getTime())) {
|
|
14
|
+
return 'bad date';
|
|
15
|
+
}
|
|
16
|
+
if (!/^\d{4}$/.test(String(year))) {
|
|
17
|
+
return 'bad date';
|
|
18
|
+
}
|
|
19
|
+
if (date.getFullYear() !== Number(year) ||
|
|
20
|
+
date.getMonth() !== Number(month) - 1 ||
|
|
21
|
+
date.getDate() !== Number(day)) {
|
|
22
|
+
return 'bad date';
|
|
23
|
+
}
|
|
24
|
+
return date;
|
|
25
|
+
}
|
|
26
|
+
calculateTermOfLease(effectiveDate, startDate, endDate) {
|
|
27
|
+
let start = startDate;
|
|
28
|
+
const end = endDate;
|
|
29
|
+
let years = 0;
|
|
30
|
+
let days = 0;
|
|
31
|
+
let daysInPartialYear = 0;
|
|
32
|
+
if (startDate < effectiveDate) {
|
|
33
|
+
start = effectiveDate;
|
|
34
|
+
}
|
|
35
|
+
const addYearAndGetEndDate = (date, yearsToAdd) => {
|
|
36
|
+
const newDate = new Date(date.getFullYear() + yearsToAdd, date.getMonth(), date.getDate());
|
|
37
|
+
newDate.setDate(newDate.getDate() - 1);
|
|
38
|
+
return newDate;
|
|
39
|
+
};
|
|
40
|
+
let testDate = addYearAndGetEndDate(start, (years = 1));
|
|
41
|
+
while (testDate <= end) {
|
|
42
|
+
testDate = addYearAndGetEndDate(start, ++years);
|
|
43
|
+
}
|
|
44
|
+
years--;
|
|
45
|
+
testDate = addYearAndGetEndDate(start, years);
|
|
46
|
+
days = 1;
|
|
47
|
+
if (start.getDate() === 29 && start.getMonth() === 1 && years % 4 !== 0) {
|
|
48
|
+
testDate.setDate(testDate.getDate() - 1);
|
|
49
|
+
}
|
|
50
|
+
testDate.setDate(testDate.getDate() + 1);
|
|
51
|
+
while (testDate <= end) {
|
|
52
|
+
days++;
|
|
53
|
+
testDate.setDate(testDate.getDate() + 1);
|
|
54
|
+
}
|
|
55
|
+
days--;
|
|
56
|
+
if (days > 0) {
|
|
57
|
+
const yearEndDate = addYearAndGetEndDate(start, years + 1);
|
|
58
|
+
daysInPartialYear = 1;
|
|
59
|
+
testDate = addYearAndGetEndDate(start, years);
|
|
60
|
+
testDate.setDate(testDate.getDate() + 1);
|
|
61
|
+
while (testDate <= yearEndDate) {
|
|
62
|
+
daysInPartialYear++;
|
|
63
|
+
testDate.setDate(testDate.getDate() + 1);
|
|
64
|
+
}
|
|
65
|
+
daysInPartialYear--;
|
|
66
|
+
}
|
|
67
|
+
return {
|
|
68
|
+
years,
|
|
69
|
+
days,
|
|
70
|
+
daysInPartialYear,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
exports.DateCalculator = DateCalculator;
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DateCalculator = void 0;
|
|
4
|
+
const DateCalculator_1 = require("./DateCalculator");
|
|
5
|
+
var DateCalculator_2 = require("./DateCalculator");
|
|
6
|
+
Object.defineProperty(exports, "DateCalculator", { enumerable: true, get: function () { return DateCalculator_2.DateCalculator; } });
|
|
7
|
+
const calculator = new DateCalculator_1.DateCalculator();
|
|
8
|
+
exports.default = calculator;
|
package/package.json
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "lease-term-calculator",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Calculate lease term duration in years and days, with support for partial year calculations",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"require": "./dist/index.js",
|
|
12
|
+
"import": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"LICENSE",
|
|
18
|
+
"README.md"
|
|
19
|
+
],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "tsc",
|
|
22
|
+
"clean": "rimraf dist",
|
|
23
|
+
"prepublishOnly": "npm run build"
|
|
24
|
+
},
|
|
25
|
+
"keywords": [
|
|
26
|
+
"lease",
|
|
27
|
+
"term",
|
|
28
|
+
"calculator",
|
|
29
|
+
"date",
|
|
30
|
+
"duration",
|
|
31
|
+
"rental",
|
|
32
|
+
"property",
|
|
33
|
+
"real-estate",
|
|
34
|
+
"accounting",
|
|
35
|
+
"finance",
|
|
36
|
+
"sdlt",
|
|
37
|
+
"hmrc"
|
|
38
|
+
],
|
|
39
|
+
"author": {
|
|
40
|
+
"name": "Sher Ghan",
|
|
41
|
+
"url": "https://github.com/sherghan7",
|
|
42
|
+
"email": "sherghan7@gmail.com",
|
|
43
|
+
"github": "https://github.com/sherghan7",
|
|
44
|
+
"linkedin": "https://www.linkedin.com/in/bu-fahad/"
|
|
45
|
+
},
|
|
46
|
+
"license": "MIT",
|
|
47
|
+
"repository": {
|
|
48
|
+
"type": "git",
|
|
49
|
+
"url": "git+https://github.com/sherghan7/lease-term-calculator.git"
|
|
50
|
+
},
|
|
51
|
+
"bugs": {
|
|
52
|
+
"url": "https://github.com/sherghan7/lease-term-calculator/issues"
|
|
53
|
+
},
|
|
54
|
+
"homepage": "https://github.com/sherghan7/lease-term-calculator#readme",
|
|
55
|
+
"engines": {
|
|
56
|
+
"node": ">=14.0.0"
|
|
57
|
+
},
|
|
58
|
+
"devDependencies": {
|
|
59
|
+
"typescript": "^5.3.0"
|
|
60
|
+
}
|
|
61
|
+
}
|