From 3af744c24d9032d79041ab3bf6afebca9263ae0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herv=C3=A9=20BECHER?= Date: Wed, 8 May 2024 20:42:49 +0200 Subject: [PATCH] refactor random options init and merge --- src/random.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/random.ts b/src/random.ts index fce31eb..99e98b3 100644 --- a/src/random.ts +++ b/src/random.ts @@ -41,7 +41,7 @@ function mergeOptions(first: RandomOptions | undefined, second: RandomOpti } function withDefaultOptions(options: RandomOptions | undefined): Required> { - if (!options) { + if (!options || options === defaultOptions) { return defaultOptions; } @@ -52,8 +52,8 @@ function withDefaultOptions(options: RandomOptions | undefined): Required< }; } -export function getRandomElement(sequence: Iterable, options?: RandomOptions) { - const { predicate, weight, random } = withDefaultOptions(options); +function _getRandomElement(sequence: Iterable, options: Required>) { + const { predicate, weight, random } = options; let result: T | undefined = undefined; let resultIndex = -1; @@ -89,7 +89,7 @@ export function getRandomElement(sequence: Iterable, options?: RandomOptio export class RandomPicker { readonly #elements: Iterable; readonly #flags: BitArray; - readonly #options: RandomOptions; + readonly #options: Required>; public constructor(elements: T[], options?: RandomOptions) { this.#elements = elements; @@ -116,7 +116,7 @@ export class RandomPicker { } public next() { - const result = getRandomElement(this.#elements, this.#options); + const result = _getRandomElement(this.#elements, this.#options); if (result.index < 0) { this.reset();