@ssts/item
v0.0.6
Published
Get a random item from an array.
Downloads
5
Maintainers
Readme
Get a random item from an array
One straight forward function ~~that~~ returns a random item
from an array 😍
Install
npm i @ssts/item
Import
import seniorITEM from "@ssts/item";
Use
const randomItem = seniorITEM(["Item 1", "Item 2", "Item 3"]);
Arguments
Returns a random item from an array or undefined if the array is empty
| Order | Parameter | Type | Default | | :---: | :---------: | :------: | :-------: | | 1 | array | any[] | required |
Examples
import seniorITEM from "@ssts/item";
const snacks = ["Peanuts", "Chips", "Candy", "Chocolate", "Popcorn", "Cookies", "Donuts", "Ice cream", "Yogurt", "Brownies", "Cupcakes", "Gummies", "Muffins", "Your favorite drink lol"] as const;
const randomSnack = seniorITEM(snacks);
console.log(randomSnack); // 'Chips'
type Snacks = (typeof snacks)[number];
// It's okay With/Without a generic
console.log(seniorITEM(snacks)); // 'Cookies'
console.log(seniorITEM<Snacks>(snacks)); // 'Candy'
type Item = {
key: string;
name: string;
imgSrc: string;
};
type Items = Item[];
const items: Items = [/* ... */];
console.log(seniorITEM([])); // undefined
console.log(seniorITEM(items)); // { key: "17", name: "Item 17", imgSrc: "https://picsum.photos/200" }
console.log(seniorITEM<Item>(items)); // { key: "5", name: "Item 5", imgSrc: "https://picsum.photos/200" }
Made With Chocolate By Senior Sedo 😎
Be Chocolateful 💙😍
ssts stands for senior-sedo-typescript 💙
So instead of ~~@senior-sedo-typescript/rand-item~~
@ssts/item
for simplicity sake 😍