refactor(pure): extract MS_PER_DAY constant and document computeEstimate precondition
This commit is contained in:
+15
-1
@@ -1,4 +1,5 @@
|
|||||||
const SUFFIXES = { k: 1e3, m: 1e6, b: 1e9, t: 1e12 };
|
const SUFFIXES = { k: 1e3, m: 1e6, b: 1e9, t: 1e12 };
|
||||||
|
const MS_PER_DAY = 86_400_000;
|
||||||
|
|
||||||
export function parseTarget(input) {
|
export function parseTarget(input) {
|
||||||
if (input === null || input === undefined || input === '') return null;
|
if (input === null || input === undefined || input === '') return null;
|
||||||
@@ -24,6 +25,19 @@ export function parseTarget(input) {
|
|||||||
return Math.floor(num * multiplier);
|
return Math.floor(num * multiplier);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Estimate remaining work and ETA.
|
||||||
|
*
|
||||||
|
* Preconditions: `current` and `target` are expected to be non-negative
|
||||||
|
* integers. This function does not validate its inputs — callers are
|
||||||
|
* responsible for ensuring sane values.
|
||||||
|
*
|
||||||
|
* @param {number} current current stat value (non-negative integer)
|
||||||
|
* @param {number} target target stat value (non-negative integer)
|
||||||
|
* @param {number} perTrain stat gain per training session
|
||||||
|
* @param {number} perDay stat gain per day
|
||||||
|
* @returns {{remaining:number, trainsToGo:number, days:number, eta:Date|null}}
|
||||||
|
*/
|
||||||
export function computeEstimate(current, target, perTrain, perDay) {
|
export function computeEstimate(current, target, perTrain, perDay) {
|
||||||
const remaining = Math.max(0, target - current);
|
const remaining = Math.max(0, target - current);
|
||||||
if (remaining === 0) {
|
if (remaining === 0) {
|
||||||
@@ -32,7 +46,7 @@ export function computeEstimate(current, target, perTrain, perDay) {
|
|||||||
|
|
||||||
const trainsToGo = perTrain > 0 ? Math.ceil(remaining / perTrain) : 0;
|
const trainsToGo = perTrain > 0 ? Math.ceil(remaining / perTrain) : 0;
|
||||||
const days = perDay > 0 ? Math.ceil(remaining / perDay) : 0;
|
const days = perDay > 0 ? Math.ceil(remaining / perDay) : 0;
|
||||||
const eta = days > 0 ? new Date(Date.now() + days * 86_400_000) : null;
|
const eta = days > 0 ? new Date(Date.now() + days * MS_PER_DAY) : null;
|
||||||
|
|
||||||
return { remaining, trainsToGo, days, eta };
|
return { remaining, trainsToGo, days, eta };
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user