Reference · Collections and piles: pure helpers
randomDrawIndexfunction
Choose a card whose name contains the filter, avoiding the current card when another match exists.
Import it
import { randomDrawIndex } from "@johnmorrisdotca/fuda-cards/collection";
Definition
function randomDrawIndex(cards: CollectionCard[], current: number, filter: string, random?: () => number): number | null
Parameters
| Name | Type | Description |
|---|---|---|
cards | CollectionCard[] | The collection. |
current | number | The index of the card now on top. |
filter | string | Text the name must contain, whatever its case; spaces round it are ignored. Empty matches every card. |
random (optional) | () => number | A source of numbers from 0 up to but not including 1. Unless said, |
Returns
number | null
The index of the card to draw, or null when no card matches.
Examples
import { randomDrawIndex } from "@johnmorrisdotca/fuda-cards/collection";
const cards = ["Alpha", "Beta", "Gamma"].map((name, i) => ({ id: String(i), name, image: `${i}.jpg` }));
console.log(randomDrawIndex(cards, 0, "", () => 0)); // 1
console.log(randomDrawIndex(cards, 0, "zzz")); // null1
nullWhat it prints (run when this page was built)