PHP's array_change_key_case in TypeScript

How to use

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

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

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
1array_change_key_case(42)false
2array_change_key_case([ 3, 5 ])[3, 5]
3array_change_key_case({ FuBaR: 42 }){"fubar": 42}
4array_change_key_case({ FuBaR: 42 }, 'CASE_LOWER'){"fubar": 42}
5array_change_key_case({ FuBaR: 42 }, 'CASE_UPPER'){"FUBAR": 42}
6array_change_key_case({ FuBaR: 42 }, 2){"FUBAR": 42}

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 array_change_key_case looks like.

import { type PhpArrayLike, type PhpAssoc, type PhpRuntimeValue, toPhpArrayObject } from '../_helpers/_phpTypes.ts'

type ChangeValue = PhpRuntimeValue
type ArrayChangeInput<TValue extends ChangeValue> = number | PhpArrayLike<TValue> | null
type ChangeKeyCaseMode = 0 | 1 | 2 | 'CASE_LOWER' | 'CASE_UPPER'

export function array_change_key_case(array: number | null, cs?: ChangeKeyCaseMode): false
export function array_change_key_case<TValue extends ChangeValue>(array: TValue[], cs?: ChangeKeyCaseMode): TValue[]
export function array_change_key_case<TValue extends ChangeValue>(
array: PhpAssoc<TValue>,
cs?: ChangeKeyCaseMode,
): PhpAssoc<TValue>
export function array_change_key_case<TValue extends ChangeValue>(
array: ArrayChangeInput<TValue>,
cs?: ChangeKeyCaseMode,
): PhpArrayLike<TValue> | false {
// discuss at: https://locutus.io/php/array_change_key_case/
// original by: Ates Goral (https://magnetiq.com)
// improved by: marrtins
// improved by: Brett Zamir (https://brett-zamir.me)
// example 1: array_change_key_case(42)
// returns 1: false
// example 2: array_change_key_case([ 3, 5 ])
// returns 2: [3, 5]
// example 3: array_change_key_case({ FuBaR: 42 })
// returns 3: {"fubar": 42}
// example 4: array_change_key_case({ FuBaR: 42 }, 'CASE_LOWER')
// returns 4: {"fubar": 42}
// example 5: array_change_key_case({ FuBaR: 42 }, 'CASE_UPPER')
// returns 5: {"FUBAR": 42}
// example 6: array_change_key_case({ FuBaR: 42 }, 2)
// returns 6: {"FUBAR": 42}

let result: PhpArrayLike<TValue> | false

if (Array.isArray(array)) {
result = array
} else if (!array || typeof array !== 'object') {
result = false
} else {
const caseFunction: 'toLowerCase' | 'toUpperCase' =
cs === undefined || cs === 0 || cs === 'CASE_LOWER' ? 'toLowerCase' : 'toUpperCase'
const source = toPhpArrayObject<TValue>(array)
const transformed: PhpAssoc<TValue> = {}

for (const [key, value] of Object.entries(source)) {
transformed[key[caseFunction]()] = value
}
result = transformed
}

return result
}

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