asset-depreciation-calculator
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -23,9 +23,10 @@ "use strict"; | ||
exports.assertDepreciationYears = assertDepreciationYears; | ||
const calculate = (purchaseAmount, totalDepreciationYears, previousEndAmount, monthsLeft = MONTHS_IN_YEAR) => { | ||
const calculate = (purchaseAmount, totalDepreciationYears, previousEndAmount, monthsLeft, isLastPart) => { | ||
const depreciationAmount = Math.round((purchaseAmount / totalDepreciationYears / MONTHS_IN_YEAR) * monthsLeft); | ||
const newEndAmount = previousEndAmount - depreciationAmount; | ||
// Because of the rounding, even if the calculation is correct, sometimes there is €0.01 left over. | ||
// Because of the rounding, even if the calculation is correct, sometimes there is €0.01 left over in the last part. | ||
// For example, if total is 31, divided by 3 it would be 10 / 10 / 10. | ||
// Also, there's a case, such as 29, where it returns negative result. | ||
if (newEndAmount <= 1) { | ||
// Therefore, we explicitly depreciate the previous year’s end amount in the last part. | ||
if (isLastPart) { | ||
return { | ||
@@ -53,3 +54,2 @@ depreciationAmount: previousEndAmount, | ||
let endAmount = purchaseAmount; | ||
let monthsLeftInLastYear = 0; | ||
if (totalDepreciationYears === 0) { | ||
@@ -65,22 +65,13 @@ return [{ | ||
} | ||
for (let index = 0; index < totalDepreciationYears; index++) { | ||
// Current year | ||
if (index === 0) { | ||
const monthsLeftInFirstYear = MONTHS_IN_YEAR - purchaseMonth + 1; | ||
const result = calculate(purchaseAmount, totalDepreciationYears, endAmount, monthsLeftInFirstYear); | ||
results.push(Object.assign({ year: purchaseYear, depreciationMonths: monthsLeftInFirstYear }, result)); | ||
endAmount = result.endAmount; | ||
// If item is purchased in Jan, monthsLeftInLastYear is 0 | ||
// If item is purchased in Dec, monthsLeftInLastYear is 11 | ||
monthsLeftInLastYear = MONTHS_IN_YEAR - monthsLeftInFirstYear; | ||
} | ||
else { // Future years | ||
const result = calculate(purchaseAmount, totalDepreciationYears, endAmount); | ||
results.push(Object.assign({ year: purchaseYear + index, depreciationMonths: MONTHS_IN_YEAR }, result)); | ||
endAmount = result.endAmount; | ||
} | ||
const parts = purchaseMonth > 1 ? totalDepreciationYears + 1 : totalDepreciationYears; | ||
const monthsInEachYear = Array(parts).fill(MONTHS_IN_YEAR); | ||
// Months in fisrt year | ||
monthsInEachYear[0] = MONTHS_IN_YEAR - purchaseMonth + 1; | ||
// Months in last year | ||
monthsInEachYear[monthsInEachYear.length - 1] = purchaseMonth > 1 ? purchaseMonth - 1 : MONTHS_IN_YEAR; | ||
for (let index = 0; index < parts; index++) { | ||
const result = calculate(purchaseAmount, totalDepreciationYears, endAmount, monthsInEachYear[index], index === parts - 1); | ||
results.push(Object.assign({ year: purchaseYear + index, depreciationMonths: monthsInEachYear[index] }, result)); | ||
endAmount = result.endAmount; | ||
} | ||
if (monthsLeftInLastYear > 0) { | ||
results.push(Object.assign({ year: purchaseYear + totalDepreciationYears, depreciationMonths: monthsLeftInLastYear }, calculate(purchaseAmount, totalDepreciationYears, endAmount, monthsLeftInLastYear))); | ||
} | ||
return results; | ||
@@ -87,0 +78,0 @@ }; |
{ | ||
"name": "asset-depreciation-calculator", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Asset Depreciation Calculator", | ||
@@ -27,3 +27,3 @@ "keywords": [ | ||
"@types/mocha": "^8.2.2", | ||
"@typescript-eslint/eslint-plugin": "^4.22.1", | ||
"@typescript-eslint/eslint-plugin": "^4.33.0", | ||
"@typescript-eslint/parser": "^4.22.1", | ||
@@ -30,0 +30,0 @@ "expect": "^26.6.2", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10358
6
94
2