Files
xarticleaudio/src/lib/credits.js

37 lines
1.1 KiB
JavaScript

"use strict";
function assertPositiveInt(name, value) {
if (!Number.isInteger(value) || value <= 0) {
throw new Error(`${name} must be a positive integer`);
}
}
function validateCreditConfig(config) {
assertPositiveInt("baseCredits", config.baseCredits);
assertPositiveInt("includedChars", config.includedChars);
assertPositiveInt("stepChars", config.stepChars);
assertPositiveInt("stepCredits", config.stepCredits);
assertPositiveInt("maxCharsPerArticle", config.maxCharsPerArticle);
}
function calculateCredits(charCount, config) {
validateCreditConfig(config);
if (!Number.isInteger(charCount) || charCount <= 0) {
throw new Error("charCount must be a positive integer");
}
if (charCount > config.maxCharsPerArticle) {
throw new Error("article_too_long");
}
const overage = Math.max(0, charCount - config.includedChars);
const extraSteps = overage === 0 ? 0 : Math.ceil(overage / config.stepChars);
return config.baseCredits + (extraSteps * config.stepCredits);
}
module.exports = {
calculateCredits,
validateCreditConfig,
};