abschreibung
v0.1.0
Published
Deferred cost allocation utilities for inclusive date ranges.
Maintainers
Readme
abschreibung
Deferred cost allocation utilities for inclusive date ranges.
Install
npm install abschreibungDate Behavior
All date ranges are inclusive. A range from 2020-01-01 to 2020-01-01 has 1 day; a range from 2020-01-01 to 2020-01-02 has 2 days.
The package works with JavaScript Date values and normalizes calculations to calendar days.
Types
type DeferredCostAllocationMode = "monthly" | "yearly";
interface DeferredCostInput {
startDate: Date;
endDate: Date;
totalCost: number;
}
interface DeferredCostPeriod {
timeStart: string;
timeEnd: string;
days: number;
costs: number;
}DeferredCostPeriod.timeStart and DeferredCostPeriod.timeEnd are formatted as YYYY-MM-DD.
calculateDeferredCostAllocation
Calculates deferred cost periods by month or year. The default mode is yearly.
import { calculateDeferredCostAllocation } from "abschreibung";
const periods = calculateDeferredCostAllocation(
{
startDate: new Date("2020-01-01"),
endDate: new Date("2020-02-01"),
totalCost: 1000,
},
"monthly",
);Returns:
[
{ timeStart: "2020-01-01", timeEnd: "2020-01-31", days: 31, costs: 968.75 },
{ timeStart: "2020-02-01", timeEnd: "2020-02-01", days: 1, costs: 31.25 },
];calculateMonthlyDeferredCostAllocation
Calculates deferred cost periods split by calendar month.
import { calculateMonthlyDeferredCostAllocation } from "abschreibung";
const periods = calculateMonthlyDeferredCostAllocation({
startDate: new Date("2020-01-31"),
endDate: new Date("2020-02-02"),
totalCost: 1000,
});Returns:
[
{ timeStart: "2020-01-31", timeEnd: "2020-01-31", days: 1, costs: 333.3333333333333 },
{ timeStart: "2020-02-01", timeEnd: "2020-02-02", days: 2, costs: 666.6666666666666 },
];If the full range is inside one calendar month, the function returns one period with the full totalCost.
calculateYearlyDeferredCostAllocation
Calculates deferred cost periods split by calendar year.
import { calculateYearlyDeferredCostAllocation } from "abschreibung";
const periods = calculateYearlyDeferredCostAllocation({
startDate: new Date("2020-12-31"),
endDate: new Date("2021-01-01"),
totalCost: 1000,
});Returns:
[
{ timeStart: "2020-12-31", timeEnd: "2020-12-31", days: 1, costs: 500 },
{ timeStart: "2021-01-01", timeEnd: "2021-01-01", days: 1, costs: 500 },
];If the full range is inside one calendar year, the function returns one period with the full totalCost.
calculateDailyDeferredCost
Calculates the daily cost for an inclusive date range.
import { calculateDailyDeferredCost } from "abschreibung";
const costPerDay = calculateDailyDeferredCost({
startDate: new Date("2020-01-01"),
endDate: new Date("2020-01-03"),
totalCost: 1000,
});Returns:
333.3333333333333;calculateInclusiveDayCount
Counts calendar days between two dates, including both the start and end date.
import { calculateInclusiveDayCount } from "abschreibung";
const days = calculateInclusiveDayCount(new Date("2020-02-01"), new Date("2020-03-01"));Returns:
30;Publishing
Pushing to the main branch publishes the package to npm through GitHub Actions.
Required repository secret:
NPM_TOKEN: npm automation token with publish access forabschreibung.
The workflow checks whether the current package.json version already exists on npm. If it does, it bumps the patch version until it finds an unpublished version, commits the updated package.json and package-lock.json back to main, and publishes that version.
