File size: 673 Bytes
cfbec3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
const UNIT_SECONDS = {
  second: 1,
  minute: 60,
  hour: 3600,
  day: 86400,
  month: 30.44 * 86400,
  year: 365.25 * 86400,
};

export function unitSeconds(unit) {
  return UNIT_SECONDS[unit] ?? 3600;
}

// displayed = base + increment * elapsed / (every * unitSeconds)
export function costAt(cost, nowMs) {
  const t0 = Date.parse(cost.baseTime);
  const elapsedSec = (nowMs - t0) / 1000;
  return cost.base + (cost.increment * elapsedSec) / (cost.every * unitSeconds(cost.unit));
}

export function rateText(cost) {
  const unit = cost.every === 1 ? cost.unit : `${cost.every} ${cost.unit}s`;
  return `+$${Number(cost.increment).toLocaleString("en-US")} / ${unit}`;
}