import { type PhpAssoc, type PhpRuntimeValue, toPhpArrayObject } from '../_helpers/_phpTypes.ts' type ReverseValue = PhpRuntimeValue export function array_reverse(array: TValue[], preserveKeys?: false | undefined): TValue[] export function array_reverse(array: TValue[], preserveKeys: true): PhpAssoc export function array_reverse( array: PhpAssoc, preserveKeys?: false | undefined, ): PhpAssoc | TValue[] export function array_reverse( array: PhpAssoc, preserveKeys: true, ): PhpAssoc export function array_reverse( array: ReverseValue[] | PhpAssoc, preserveKeys?: boolean, ): ReverseValue[] | PhpAssoc { // discuss at: https://locutus.io/php/array_reverse/ // original by: Kevin van Zonneveld (https://kvz.io) // improved by: Karol Kowalski // example 1: array_reverse( [ 'php', '4.0', ['green', 'red'] ], true) // returns 1: { 2: ['green', 'red'], 1: '4.0', 0: 'php'} const preserve = preserveKeys === true let result: ReverseValue[] | PhpAssoc if (Array.isArray(array) && !preserve) { result = array.slice(0).reverse() } else { const source = toPhpArrayObject(array) if (preserve) { const keys = Object.keys(source) const reversed: PhpAssoc = {} for (let index = keys.length - 1; index >= 0; index -= 1) { const key = keys[index] if (typeof key === 'string') { reversed[key] = source[key] } } result = reversed } else { result = Object.values(source).reverse() } } return result }