---
warning: This file is auto generated by `yarn web:inject`, do not edit by hand
examples:
- array_map( function (a){return (a * a * a)}, [1, 2, 3, 4, 5] )
returns:
- '[ 1, 8, 27, 64, 125 ]'
dependencies:
- php/_helpers/_callbackResolver
authors:
original by:
- Andrea Giammarchi (https://webreflection.blogspot.com)
improved by:
- Kevin van Zonneveld (https://kvz.io)
- Brett Zamir (https://brett-zamir.me)
input by:
- thekid
notes:
- |-
If the callback is a string (or object, if an array is supplied),
it can only work if the function name is in the global context
parityVerified: null
isTypescript: true
type: function
layout: function
title: PHP's array_map in TypeScript
description: >-
Here's what our current TypeScript equivalent to PHP's array_map
looks like.
function: array_map
category: array
language: php
permalink: php/array/array_map/
alias:
- /functions/php/array_map/
- /functions/array/array_map/
- /php/array_map/
- /functions/array_map/
---
{% codeblock lang:typescript %}import { resolvePhpCallable } from '../_helpers/_callbackResolver.ts'
import type { PhpCallableDescriptor, PhpList, PhpRuntimeValue } from '../_helpers/_phpTypes.ts'
type ArrayMapValue = PhpRuntimeValue
type ArrayMapInputs = [firstArray: PhpList
, ...restArrays: Array>]
type ArrayMapTupleArgs[]> = [
...{
[K in keyof TInputs]: TInputs[K] extends PhpList ? TValue | undefined : ArrayMapValue | undefined
},
]
type ArrayMapCallbackArgsVariadic = Array
export function array_map(
callback: PhpCallableDescriptor, TResult>,
...inputArrays: TInputs
): PhpList
export function array_map(
callback: null | undefined,
...inputArrays: TInputs
): PhpList>
export function array_map(
callback: PhpCallableDescriptor | null | undefined,
...inputArrays: ArrayMapInputs
): PhpList | PhpList {
// discuss at: https://locutus.io/php/array_map/
// original by: Andrea Giammarchi (https://webreflection.blogspot.com)
// improved by: Kevin van Zonneveld (https://kvz.io)
// improved by: Brett Zamir (https://brett-zamir.me)
// input by: thekid
// note 1: If the callback is a string (or object, if an array is supplied),
// note 1: it can only work if the function name is in the global context
// example 1: array_map( function (a){return (a * a * a)}, [1, 2, 3, 4, 5] )
// returns 1: [ 1, 8, 27, 64, 125 ]
const argc = inputArrays.length + 1
const itemCount = inputArrays[0]?.length ?? 0
const resolved =
callback === null || typeof callback === 'undefined'
? null
: resolvePhpCallable(callback, {
invalidMessage: 'array_map(): Invalid callback',
missingScopeMessage: (scopeName: string) => 'Object not found: ' + scopeName,
})
if (resolved) {
const mapped: PhpList = []
for (let itemIndex = 0; itemIndex < itemCount; itemIndex++) {
const args: ArrayMapCallbackArgsVariadic = []
for (let arrayIndex = 0; arrayIndex < argc - 1; arrayIndex++) {
args.push(inputArrays[arrayIndex]?.[itemIndex])
}
mapped[itemIndex] = resolved.fn.apply(resolved.scope, args)
}
return mapped
}
const mapped: PhpList = []
for (let itemIndex = 0; itemIndex < itemCount; itemIndex++) {
const args: ArrayMapCallbackArgsVariadic = []
for (let arrayIndex = 0; arrayIndex < argc - 1; arrayIndex++) {
args.push(inputArrays[arrayIndex]?.[itemIndex])
}
mapped[itemIndex] = args
}
return mapped
}
{% endcodeblock %}
{% codeblock lang:javascript %}import { resolvePhpCallable } from '../_helpers/_callbackResolver.ts'
export function array_map(callback, ...inputArrays) {
// discuss at: https://locutus.io/php/array_map/
// original by: Andrea Giammarchi (https://webreflection.blogspot.com)
// improved by: Kevin van Zonneveld (https://kvz.io)
// improved by: Brett Zamir (https://brett-zamir.me)
// input by: thekid
// note 1: If the callback is a string (or object, if an array is supplied),
// note 1: it can only work if the function name is in the global context
// example 1: array_map( function (a){return (a * a * a)}, [1, 2, 3, 4, 5] )
// returns 1: [ 1, 8, 27, 64, 125 ]
const argc = inputArrays.length + 1
const itemCount = inputArrays[0]?.length ?? 0
const resolved =
callback === null || typeof callback === 'undefined'
? null
: resolvePhpCallable(callback, {
invalidMessage: 'array_map(): Invalid callback',
missingScopeMessage: (scopeName) => 'Object not found: ' + scopeName,
})
if (resolved) {
const mapped = []
for (let itemIndex = 0; itemIndex < itemCount; itemIndex++) {
const args = []
for (let arrayIndex = 0; arrayIndex < argc - 1; arrayIndex++) {
args.push(inputArrays[arrayIndex]?.[itemIndex])
}
mapped[itemIndex] = resolved.fn.apply(resolved.scope, args)
}
return mapped
}
const mapped = []
for (let itemIndex = 0; itemIndex < itemCount; itemIndex++) {
const args = []
for (let arrayIndex = 0; arrayIndex < argc - 1; arrayIndex++) {
args.push(inputArrays[arrayIndex]?.[itemIndex])
}
mapped[itemIndex] = args
}
return mapped
}{% endcodeblock %}
{% codeblock lang:typescript %}// php/_helpers/_phpTypes (Locutus helper dependency)
type PhpNullish = null | undefined
type PhpInput = {} | PhpNullish
type PhpScalar = string | number | boolean
type PhpPrimitive = PhpScalar | bigint
type PhpList
= T[]
type PhpAssoc = { [key: string]: T }
type PhpArrayLike = PhpList | PhpAssoc
type PhpFunctionValue = (...args: PhpInput[]) => PhpInput
interface PhpRuntimeAssoc {
[key: string]: PhpRuntimeValue
}
interface PhpRuntimeList extends Array {}
type PhpRuntimeValue = PhpPrimitive | PhpNullish | PhpRuntimeList | PhpRuntimeAssoc | PhpFunctionValue
type PhpCallableArgs = PhpInput[]
type PhpCallable = (
...args: TArgs
) => TResult
type PhpCallableScope = PhpInput
type PhpCallableTuple = readonly [
PhpCallableScope,
string | PhpCallable,
]
type PhpCallableDescriptor =
| string
| PhpCallable
| PhpCallableTuple
function isObjectLike(value: PhpInput): value is PhpArrayLike {
return typeof value === 'object' && value !== null
}
function isPhpCallable(
value: PhpInput,
): value is PhpCallable {
return typeof value === 'function'
}
// php/_helpers/_phpRuntimeState (Locutus helper dependency)
interface LocutusRuntimeContainer {
php?: PhpAssoc
}
interface PhpGlobalProcessLike {
env?: PhpAssoc
}
interface PhpGlobalBufferLike {
from?: (...args: PhpInput[]) => PhpInput
}
interface PhpGlobalKnownEntryMap {
process: PhpGlobalProcessLike
Buffer: PhpGlobalBufferLike
}
type GlobalWithLocutus = {
$locutus?: LocutusRuntimeContainer
[key: string]: PhpInput
}
const globalContext: GlobalWithLocutus =
typeof window === 'object' && window !== null ? window : typeof global === 'object' && global !== null ? global : {}
function getPhpGlobalEntry(
key: TKey,
): PhpGlobalKnownEntryMap[TKey] | undefined
function getPhpGlobalEntry(key: string): PhpInput | undefined
function getPhpGlobalEntry(key: string): PhpInput | undefined {
const value = globalContext[key]
return typeof value === 'undefined' ? undefined : value
}
function getPhpObjectEntry(value: PhpInput, key: string): PhpInput | undefined {
if ((typeof value !== 'object' && typeof value !== 'function') || value === null) {
return undefined
}
let current: object | null = value
while (current) {
const descriptor = Object.getOwnPropertyDescriptor(current, key)
if (descriptor) {
if (typeof descriptor.get === 'function') {
const getterValue = descriptor.get.call(value)
return typeof getterValue === 'undefined' ? undefined : getterValue
}
const directValue = descriptor.value
return typeof directValue === 'undefined' ? undefined : directValue
}
current = Object.getPrototypeOf(current)
}
return undefined
}
// php/_helpers/_callbackResolver (Locutus helper dependency)
type CallbackValue = PhpInput
interface CallbackResolverOptions {
invalidMessage: string
missingScopeMessage?: (scopeName: string) => string
}
interface ResolvedCallback {
fn: PhpCallable
scope: CallbackValue
}
function resolvePhpCallable(
callback: PhpCallableDescriptor,
options: CallbackResolverOptions,
): ResolvedCallback {
if (isPhpCallable(callback)) {
return { fn: callback, scope: null }
}
if (typeof callback === 'string') {
const candidate = getPhpGlobalEntry(callback)
if (isPhpCallable(candidate)) {
return { fn: candidate, scope: null }
}
throw new Error(options.invalidMessage)
}
if (Array.isArray(callback) && callback.length >= 2) {
const scopeDescriptor = callback[0]
const callableDescriptor = callback[1]
let scope: CallbackValue
if (typeof scopeDescriptor === 'string') {
scope = getPhpGlobalEntry(scopeDescriptor)
if (typeof scope === 'undefined' && options.missingScopeMessage) {
throw new Error(options.missingScopeMessage(scopeDescriptor))
}
} else {
scope = scopeDescriptor
}
if (isPhpCallable(callableDescriptor)) {
return { fn: callableDescriptor, scope }
}
if (typeof callableDescriptor === 'string' && (isObjectLike(scope) || typeof scope === 'function')) {
const candidate = getPhpObjectEntry(scope, callableDescriptor)
if (isPhpCallable(candidate)) {
return { fn: candidate, scope }
}
}
}
throw new Error(options.invalidMessage)
}
// php/array/array_map (target function module)
type ArrayMapValue = PhpRuntimeValue
type ArrayMapInputs = [firstArray: PhpList, ...restArrays: Array>]
type ArrayMapTupleArgs[]> = [
...{
[K in keyof TInputs]: TInputs[K] extends PhpList ? TValue | undefined : ArrayMapValue | undefined
},
]
type ArrayMapCallbackArgsVariadic = Array
function array_map(
callback: PhpCallableDescriptor, TResult>,
...inputArrays: TInputs
): PhpList
function array_map(
callback: null | undefined,
...inputArrays: TInputs
): PhpList>
function array_map(
callback: PhpCallableDescriptor | null | undefined,
...inputArrays: ArrayMapInputs
): PhpList | PhpList {
// discuss at: https://locutus.io/php/array_map/
// original by: Andrea Giammarchi (https://webreflection.blogspot.com)
// improved by: Kevin van Zonneveld (https://kvz.io)
// improved by: Brett Zamir (https://brett-zamir.me)
// input by: thekid
// note 1: If the callback is a string (or object, if an array is supplied),
// note 1: it can only work if the function name is in the global context
// example 1: array_map( function (a){return (a * a * a)}, [1, 2, 3, 4, 5] )
// returns 1: [ 1, 8, 27, 64, 125 ]
const argc = inputArrays.length + 1
const itemCount = inputArrays[0]?.length ?? 0
const resolved =
callback === null || typeof callback === 'undefined'
? null
: resolvePhpCallable(callback, {
invalidMessage: 'array_map(): Invalid callback',
missingScopeMessage: (scopeName: string) => 'Object not found: ' + scopeName,
})
if (resolved) {
const mapped: PhpList = []
for (let itemIndex = 0; itemIndex < itemCount; itemIndex++) {
const args: ArrayMapCallbackArgsVariadic = []
for (let arrayIndex = 0; arrayIndex < argc - 1; arrayIndex++) {
args.push(inputArrays[arrayIndex]?.[itemIndex])
}
mapped[itemIndex] = resolved.fn.apply(resolved.scope, args)
}
return mapped
}
const mapped: PhpList = []
for (let itemIndex = 0; itemIndex < itemCount; itemIndex++) {
const args: ArrayMapCallbackArgsVariadic = []
for (let arrayIndex = 0; arrayIndex < argc - 1; arrayIndex++) {
args.push(inputArrays[arrayIndex]?.[itemIndex])
}
mapped[itemIndex] = args
}
return mapped
}{% endcodeblock %}
{% codeblock lang:javascript %}// php/_helpers/_phpTypes (Locutus helper dependency)
function isObjectLike(value) {
return typeof value === 'object' && value !== null
}
function isPhpCallable(value) {
return typeof value === 'function'
}
// php/_helpers/_phpRuntimeState (Locutus helper dependency)
const globalContext =
typeof window === 'object' && window !== null ? window : typeof global === 'object' && global !== null ? global : {}
function getPhpGlobalEntry(key) {
const value = globalContext[key]
return typeof value === 'undefined' ? undefined : value
}
function getPhpObjectEntry(value, key) {
if ((typeof value !== 'object' && typeof value !== 'function') || value === null) {
return undefined
}
let current = value
while (current) {
const descriptor = Object.getOwnPropertyDescriptor(current, key)
if (descriptor) {
if (typeof descriptor.get === 'function') {
const getterValue = descriptor.get.call(value)
return typeof getterValue === 'undefined' ? undefined : getterValue
}
const directValue = descriptor.value
return typeof directValue === 'undefined' ? undefined : directValue
}
current = Object.getPrototypeOf(current)
}
return undefined
}
// php/_helpers/_callbackResolver (Locutus helper dependency)
function resolvePhpCallable(callback, options) {
if (isPhpCallable(callback)) {
return { fn: callback, scope: null }
}
if (typeof callback === 'string') {
const candidate = getPhpGlobalEntry(callback)
if (isPhpCallable(candidate)) {
return { fn: candidate, scope: null }
}
throw new Error(options.invalidMessage)
}
if (Array.isArray(callback) && callback.length >= 2) {
const scopeDescriptor = callback[0]
const callableDescriptor = callback[1]
let scope
if (typeof scopeDescriptor === 'string') {
scope = getPhpGlobalEntry(scopeDescriptor)
if (typeof scope === 'undefined' && options.missingScopeMessage) {
throw new Error(options.missingScopeMessage(scopeDescriptor))
}
} else {
scope = scopeDescriptor
}
if (isPhpCallable(callableDescriptor)) {
return { fn: callableDescriptor, scope }
}
if (typeof callableDescriptor === 'string' && (isObjectLike(scope) || typeof scope === 'function')) {
const candidate = getPhpObjectEntry(scope, callableDescriptor)
if (isPhpCallable(candidate)) {
return { fn: candidate, scope }
}
}
}
throw new Error(options.invalidMessage)
}
// php/array/array_map (target function module)
function array_map(callback, ...inputArrays) {
// discuss at: https://locutus.io/php/array_map/
// original by: Andrea Giammarchi (https://webreflection.blogspot.com)
// improved by: Kevin van Zonneveld (https://kvz.io)
// improved by: Brett Zamir (https://brett-zamir.me)
// input by: thekid
// note 1: If the callback is a string (or object, if an array is supplied),
// note 1: it can only work if the function name is in the global context
// example 1: array_map( function (a){return (a * a * a)}, [1, 2, 3, 4, 5] )
// returns 1: [ 1, 8, 27, 64, 125 ]
const argc = inputArrays.length + 1
const itemCount = inputArrays[0]?.length ?? 0
const resolved =
callback === null || typeof callback === 'undefined'
? null
: resolvePhpCallable(callback, {
invalidMessage: 'array_map(): Invalid callback',
missingScopeMessage: (scopeName) => 'Object not found: ' + scopeName,
})
if (resolved) {
const mapped = []
for (let itemIndex = 0; itemIndex < itemCount; itemIndex++) {
const args = []
for (let arrayIndex = 0; arrayIndex < argc - 1; arrayIndex++) {
args.push(inputArrays[arrayIndex]?.[itemIndex])
}
mapped[itemIndex] = resolved.fn.apply(resolved.scope, args)
}
return mapped
}
const mapped = []
for (let itemIndex = 0; itemIndex < itemCount; itemIndex++) {
const args = []
for (let arrayIndex = 0; arrayIndex < argc - 1; arrayIndex++) {
args.push(inputArrays[arrayIndex]?.[itemIndex])
}
mapped[itemIndex] = args
}
return mapped
}{% endcodeblock %}