import type { PhpArrayLike, PhpRuntimeValue } from '../_helpers/_phpTypes.ts' type RandomValue = PhpRuntimeValue export function array_rand(array: PhpArrayLike): string | null export function array_rand( array: PhpArrayLike, num: null | undefined, ): string | null export function array_rand(array: PhpArrayLike, num: 1): string | null export function array_rand( array: PhpArrayLike, num: number, ): string | string[] | null export function array_rand( array: PhpArrayLike, num?: number | null, ): string | string[] | null { // discuss at: https://locutus.io/php/array_rand/ // original by: Waldo Malqui Silva (https://waldo.malqui.info) // reimplemented by: Rafał Kukawski // example 1: array_rand( ['Kevin'], 1 ) // returns 1: '0' // By using Object.keys we support both, arrays and objects // which phpjs wants to support const keys = Object.keys(array) if (typeof num === 'undefined' || num === null) { num = 1 } else { num = +num } if (isNaN(num) || num < 1 || num > keys.length) { return null } // shuffle the array of keys for (let i = keys.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)) // 0 ≤ j ≤ i const tmp = keys[j] const current = keys[i] if (tmp === undefined || current === undefined) { return null } keys[j] = current keys[i] = tmp } return num === 1 ? (keys[0] ?? null) : keys.slice(0, num) }