deno.land / std@0.166.0 / collections / sample.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.// This module is browser compatible.
import { randomInteger } from "./_utils.ts";
/** * Returns a random element from the given array. * * Example: * * ```ts * import { sample } from "https://deno.land/std@$STD_VERSION/collections/sample.ts" * import { assert } from "https://deno.land/std@$STD_VERSION/testing/asserts.ts"; * * const numbers = [1, 2, 3, 4]; * const random = sample(numbers); * * assert(numbers.includes(random as number)); * ``` */export function sample<T>(array: readonly T[]): T | undefined { const length = array.length; return length ? array[randomInteger(0, length - 1)] : undefined;}
std
Deno standard library
GitHub Repositorydenoland/deno_std
GitHub Stars
2458

Version Info

Tagged at
2 months ago