Functional Weave
Code in TypeScript

manufacturing.yield-scrap@1.0.0

impl/typescript.ts

1,003 bytes · the TypeScript implementation · view raw

Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.

import { roundDiv } from "./math_round_div.ts";  ← from math.round-div ^1.0.0 · built alongside by fune
import { type YieldPlan } from "./manufacturing_yield_scrap_types.ts";

const MAX_OUTPUT = Math.floor(Number.MAX_SAFE_INTEGER / 10000);

/**
 * Starts needed for a good output at a yield, rounded up in integers so an
 * exact answer (95 at 95% is 100) never becomes 101 through a float.
 */
export function inputForYield(requiredOutput: number, yieldBasisPoints: number): YieldPlan {
  if (!Number.isInteger(requiredOutput) || requiredOutput < 0 || requiredOutput > MAX_OUTPUT) {
    throw new RangeError(`requiredOutput must be a whole number of units from 0 to ${MAX_OUTPUT}, received ${requiredOutput}`);
  }
  if (!Number.isInteger(yieldBasisPoints) || yieldBasisPoints < 1 || yieldBasisPoints > 10000) {
    throw new RangeError(`yieldBasisPoints must be a whole number from 1 to 10000, received ${yieldBasisPoints}`);
  }
  const input = roundDiv(requiredOutput * 10000, yieldBasisPoints, "up");
  return { input, scrap: input - requiredOutput };
}