export type ScaleFunction = Function & {
  inverse: Function
}

/**
 *
 * @param domain example: [0, 100]
 * @param range examaple: [0, width]
 */
export function Scale(domain: number[], range: number[]): ScaleFunction {
  function Fn(value: number): number {
    return ((value - domain[0]) * (range[1] - range[0])) / (domain[1] - domain[0]) + range[0]
  }

  Fn.inverse = function (value: number): number {
    return ((value - range[0]) * (domain[1] - domain[0])) / (range[1] - range[0]) + domain[0]
  }

  return Fn
}
