-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: replace
randomIntByModel
, randomIntInclusiveByModel
wit…
…h `randomByModel` BREAKING CHANGE: - Removed `randomIntByModel`, `randomIntInclusiveByModel` - Rewritten `randomByModel`
- Loading branch information
1 parent
9cedbd3
commit 8ff1f62
Showing
9 changed files
with
103 additions
and
201 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,43 @@ | ||
import { isArray, isNumber } from 'extra-utils' | ||
import { Getter, NonEmptyArray } from 'justypes' | ||
import { isArray, isFunction, isNumber } from 'extra-utils' | ||
import { randomByWeightModel } from './random-by-weight-model.js' | ||
import { IRandomModel } from './types.js' | ||
import { random } from './random.js' | ||
import { randomInt } from './random-int.js' | ||
import { randomIntInclusive } from './random-int-inclusive.js' | ||
|
||
export enum Type { | ||
Float | ||
, Integer | ||
, IntegerInclusive | ||
} | ||
|
||
export type IRandomModel = | ||
| number | ||
| Getter<number> | ||
| { | ||
type: Type | ||
min: number | ||
max: number | ||
} | ||
| NonEmptyArray<{ | ||
weight: number | ||
value: IRandomModel | ||
}> | ||
|
||
export function randomByModel(model: IRandomModel): number { | ||
if (isNumber(model)) { | ||
return model | ||
} else if (isArray(model)) { | ||
const subModel = randomByWeightModel(model) | ||
return randomByModel(subModel) | ||
} else if (isFunction(model)) { | ||
return model() | ||
} else { | ||
const { min, max } = model | ||
return random(min, max) | ||
const { type, min, max } = model | ||
switch (type) { | ||
case Type.Float: return random(min, max) | ||
case Type.Integer: return randomInt(min, max) | ||
case Type.IntegerInclusive: return randomIntInclusive(min, max) | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.