PHP's shuffle in TypeScript

How to use

Install via yarn add locutus and import: import { shuffle } from 'locutus/php/array/shuffle'.

Or with CommonJS: const { shuffle } = require('locutus/php/array/shuffle')

Use a bundler that supports tree-shaking so you only ship the functions you actually use. Vite, webpack, Rollup, and Parcel all handle this. For server-side use this is less of a concern.

Examples

These examples are extracted from test cases that automatically verify our functions against their native counterparts.

#codeexpected result
1var $data = {5:'a', 2:'3', 3:'c', 4:5, 'q':5} ini_set('locutus.sortByReference', true) shuffle($data) var $result = $data.q5

PHP arrays and TypeScript/JavaScript

Please note that Locutus uses TypeScript/JavaScript objects as substitutes for PHP arrays, they are the closest we can get to this hashtable-like data structure without rolling our own. While many TypeScript/JavaScript implementations preserve the order of object properties, the ECMAScript Language Specification explicitly states that:

The mechanics and order of enumerating the properties is not specified.

In practice most engines preserve insertion order, but if your code depends on key ordering across platforms, keep this caveat in mind.

To influence how Locutus treats objects as arrays, you can check out the locutus.objectsAsArrays setting.

Dependencies

This function uses the following Locutus functions:

Here's what our current TypeScript equivalent to PHP's shuffle looks like.

import { ini_get } from '../info/ini_get.ts'

export function shuffle<T>(inputArr: Record<string, T>): boolean | Record<string, T> | T[] {
// discuss at: https://locutus.io/php/shuffle/
// original by: Jonas Raoni Soares Silva (https://www.jsfromhell.com)
// revised by: Kevin van Zonneveld (https://kvz.io)
// revised by: Brett Zamir (https://brett-zamir.me)
// improved by: Brett Zamir (https://brett-zamir.me)
// example 1: var $data = {5:'a', 2:'3', 3:'c', 4:5, 'q':5}
// example 1: ini_set('locutus.sortByReference', true)
// example 1: shuffle($data)
// example 1: var $result = $data.q
// returns 1: 5

const valArr: T[] = []
let k = ''
let i = 0
let sortByReference = false
let populateArr: Record<string, T> | T[] = []

for (k in inputArr) {
// Get key and value arrays
if (inputArr.hasOwnProperty(k)) {
const value = inputArr[k]
if (value === undefined) {
continue
}
valArr.push(value)
if (sortByReference) {
delete inputArr[k]
}
}
}
valArr.sort(function () {
return 0.5 - Math.random()
})

const iniVal = ini_get('locutus.sortByReference') || 'on'
sortByReference = iniVal === 'on'
populateArr = sortByReference ? inputArr : populateArr

for (i = 0; i < valArr.length; i++) {
// Repopulate the old array
if (Array.isArray(populateArr)) {
const value = valArr[i]
if (value === undefined) {
continue
}
populateArr[i] = value
} else {
const value = valArr[i]
if (value === undefined) {
continue
}
populateArr[String(i)] = value
}
}

return sortByReference || populateArr
}

Improve this function

Locutus is a community effort following The McDonald's Theory: we ship first iterations, hoping others will improve them. If you see something that could be better, we'd love your contribution.

View on GitHub · Edit on GitHub · View Raw


« More PHP array functions


Star