refactor(pure): share MS_PER_DAY with tests and document pruneHistory

This commit is contained in:
dev
2026-06-01 15:48:52 -05:00
parent ca753bf196
commit a2e23341ac
2 changed files with 11 additions and 3 deletions
+9 -1
View File
@@ -1,5 +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 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;
@@ -53,6 +53,14 @@ export function computeEstimate(current, target, perTrain, perDay) {
const THIRTY_DAYS_MS = 30 * MS_PER_DAY; const THIRTY_DAYS_MS = 30 * MS_PER_DAY;
/**
* Returns a new array of entries from the last 30 days, with the 30-day
* boundary treated as exclusive (an entry exactly 30 days old is dropped).
* Does not mutate the input.
* @param {{ts:number, delta:number}[]} entries
* @param {number} [now] Reference time in ms (defaults to Date.now()).
* @returns {{ts:number, delta:number}[]}
*/
export function pruneHistory(entries, now = Date.now()) { export function pruneHistory(entries, now = Date.now()) {
const cutoff = now - THIRTY_DAYS_MS; const cutoff = now - THIRTY_DAYS_MS;
return entries.filter((e) => e.ts > cutoff); return entries.filter((e) => e.ts > cutoff);
+2 -2
View File
@@ -1,8 +1,8 @@
import { test } from 'node:test'; import { test } from 'node:test';
import assert from 'node:assert/strict'; import assert from 'node:assert/strict';
import { parseTarget, computeEstimate, pruneHistory } from '../src/pure.js'; import { parseTarget, computeEstimate, pruneHistory, MS_PER_DAY } from '../src/pure.js';
const DAY = 86_400_000; const DAY = MS_PER_DAY;
const NOW = 1_700_000_000_000; // fixed reference const NOW = 1_700_000_000_000; // fixed reference
test('parseTarget: integer numbers', () => { test('parseTarget: integer numbers', () => {