Skip to content

Commit

Permalink
refactor: replace randomIntByModel, randomIntInclusiveByModel wit…
Browse files Browse the repository at this point in the history
…h `randomByModel`

BREAKING CHANGE:
- Removed `randomIntByModel`, `randomIntInclusiveByModel`
- Rewritten `randomByModel`
  • Loading branch information
BlackGlory committed Apr 18, 2024
1 parent 9cedbd3 commit 8ff1f62
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 201 deletions.
40 changes: 18 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,6 @@ yarn add extra-rand
```

## API
```ts
type IRandomModel =
| number
| {
min: number
max: number
}
| NonEmptyArray<{
weight: number
value: IRandomModel
}>
```
### random
```ts
function random(min: number, max: number): number
Expand Down Expand Up @@ -76,17 +63,26 @@ function randomByWeightModel<T>(model: IWeightModel<T>): number

### randomByModel
```ts
function randomByModel(model: IRandomModel): number
```
enum Type {
Float
, Integer
, IntegerInclusive
}
### randomIntByModel
```ts
function randomIntByModel(model: IRandomModel): number
```
type IRandomModel =
| number
| Getter<number>
| {
type: Type
min: number
max: number
}
| NonEmptyArray<{
weight: number
value: IRandomModel
}>
### randomIntInclusiveByModel
```ts
function randomIntInclusiveByModel(model: IRandomModel): number
function randomByModel(model: IRandomModel): number
```

### mapToRange
Expand Down
63 changes: 54 additions & 9 deletions __tests__/random-by-model.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { describe, test, expect } from 'vitest'
import { randomByModel } from '@src/random-by-model.js'
import { IRandomModel } from '@src/types.js'
import { randomByModel, IRandomModel, Type } from '@src/random-by-model.js'

describe('randomByModel', () => {
test('number', () => {
Expand All @@ -13,20 +12,66 @@ describe('randomByModel', () => {
}
})

test('min, max', () => {
const model: IRandomModel = {
min: 0.1
, max: 9.9
}
test('getter', () => {
const value = 0.5
const model: IRandomModel = () => value

for (let i = 10000; i--;) {
const result = randomByModel(model)

expect(result).toBeGreaterThanOrEqual(model.min)
expect(result).toBeLessThan(model.max)
expect(result).toBe(value)
}
})

describe('min, max', () => {
test('float', () => {
const model: IRandomModel = {
type: Type.Float
, min: 0.1
, max: 9.9
}

for (let i = 10000; i--;) {
const result = randomByModel(model)

expect(result).toBeGreaterThanOrEqual(model.min)
expect(result).toBeLessThan(model.max)
}
})

test('integer', () => {
const model: IRandomModel = {
type: Type.Integer
, min: 0.1
, max: 9.9
}

for (let i = 10000; i--;) {
const result = randomByModel(model)

expect(Number.isInteger(result)).toBe(true)
expect(result).toBeGreaterThanOrEqual(Math.ceil(model.min))
expect(result).toBeLessThan(Math.floor(model.max))
}
})

test('integer inclusive', () => {
const model: IRandomModel = {
type: Type.IntegerInclusive
, min: 0.1
, max: 9.9
}

for (let i = 10000; i--;) {
const result = randomByModel(model)

expect(Number.isInteger(result)).toBe(true)
expect(result).toBeGreaterThanOrEqual(Math.ceil(model.min))
expect(result).toBeLessThanOrEqual(Math.floor(model.max))
}
})
})

test('weighted', () => {
const loops = 10000
const model: IRandomModel = [
Expand Down
56 changes: 0 additions & 56 deletions __tests__/random-int-by-model.spec.ts

This file was deleted.

56 changes: 0 additions & 56 deletions __tests__/random-int-inclusive-by-model.spec.ts

This file was deleted.

4 changes: 0 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export { IRandomModel } from './types.js'

export * from './random.js'
export * from './random-int.js'
export * from './random-int-inclusive.js'
Expand All @@ -8,8 +6,6 @@ export * from './random-index-by-weight.js'
export * from './random-weighted.js'
export * from './random-by-weight-model.js'
export * from './random-by-model.js'
export * from './random-int-by-model.js'
export * from './random-int-inclusive-by-model.js'

export * from './map-to-range.js'
export * from './map-to-index-by-weight.js'
35 changes: 31 additions & 4 deletions src/random-by-model.ts
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)
}
}
}
19 changes: 0 additions & 19 deletions src/random-int-by-model.ts

This file was deleted.

19 changes: 0 additions & 19 deletions src/random-int-inclusive-by-model.ts

This file was deleted.

12 changes: 0 additions & 12 deletions src/types.ts

This file was deleted.

0 comments on commit 8ff1f62

Please sign in to comment.