import { PercentageOption , PossibilitySpacing , PossibilitySpacingOption , RandomNumberBetweenGenerator , Spacing , } from "./types" ; param min param min returns export type RandomBetweenGenerator = ( min : number , max : number ) => number ; param choice returns export type OptionChooser < T extends PercentageOption > = ( choices : T [ ] ) => T ; /**
 * Utility to generate distances based on possibility schemas.
 */ export class SpacingCalculator { /**
     * A random number generator that returns a decimal within [min, max).
     */ public randomBetween : RandomBetweenGenerator ; /**
     * From an array of potential choice Objects, returns one chosen at random.
     */ public chooseAmong : OptionChooser < PossibilitySpacingOption > ; param randomBetween param chooseAmong public constructor ( randomBetween , chooseAmong ) { this . randomBetween = randomBetween ; this . chooseAmong = chooseAmong ; } param spacing returns public calculateFromSpacing ( spacing ) { if ( ! spacing ) { return 0 ; } switch ( spacing . constructor ) { case Array : if ( ( spacing as number [ ] ) [ 0 ] . constructor === Number ) { return this . randomBetween ( ( spacing as number [ ] ) [ 0 ] , ( spacing as number [ ] ) [ 1 ] ) ; } return this . calculateFromPossibilities ( spacing as PossibilitySpacingOption [ ] ) ; case Object : return this . calculateFromPossibility ( spacing as PossibilitySpacing ) ; case Number : return spacing as number ; default : throw new Error ( `Unknown spacing requested: '${ spacing }'.` ) ; } } param spacing returns public calculateFromPossibility ( spacing ) { const spacingObject = spacing ; const min = spacingObject . min ; const max = spacingObject . max ; const units = spacingObject . units || 1 ; return this . randomBetween ( min / units , max / units ) * units ; } param spacing returns public calculateFromPossibilities ( spacing ) { return this . calculateFromPossibility ( this . chooseAmong ( spacing ) . value ) ; } }