---
warning: This file is auto generated by `yarn web:inject`, do not edit by hand
examples:
- array_pad([ 7, 8, 9 ], 2, 'a')
- array_pad([ 7, 8, 9 ], 5, 'a')
- array_pad([ 7, 8, 9 ], 5, 2)
- array_pad([ 7, 8, 9 ], -5, 'a')
returns:
- '[ 7, 8, 9]'
- '[ 7, 8, 9, ''a'', ''a'']'
- '[ 7, 8, 9, 2, 2]'
- '[ ''a'', ''a'', 7, 8, 9 ]'
dependencies: []
authors:
original by:
- Waldo Malqui Silva (https://waldo.malqui.info)
notes: []
parityVerified:
- PHP 8.3
isTypescript: true
type: function
layout: function
title: PHP's array_pad in TypeScript
description: >-
Here's what our current TypeScript equivalent to PHP's array_pad
looks like.
function: array_pad
category: array
language: php
permalink: php/array/array_pad/
alias:
- /functions/php/array_pad/
- /functions/array/array_pad/
- /php/array_pad/
- /functions/array_pad/
---
{% codeblock lang:typescript %}import type { PhpRuntimeList, PhpRuntimeValue } from '../_helpers/_phpTypes.ts'
type PadValue = PhpRuntimeValue
type NonArrayPadInput = Exclude
export function array_pad(
input: TInput[],
padSize: number,
padValue: TPad,
): Array
export function array_pad(input: NonArrayPadInput, padSize: number, padValue: TPad): TPad[]
export function array_pad(
input: TInput[] | NonArrayPadInput,
padSize: number,
padValue: TPad,
): Array {
// discuss at: https://locutus.io/php/array_pad/
// parity verified: PHP 8.3
// original by: Waldo Malqui Silva (https://waldo.malqui.info)
// example 1: array_pad([ 7, 8, 9 ], 2, 'a')
// returns 1: [ 7, 8, 9]
// example 2: array_pad([ 7, 8, 9 ], 5, 'a')
// returns 2: [ 7, 8, 9, 'a', 'a']
// example 3: array_pad([ 7, 8, 9 ], 5, 2)
// returns 3: [ 7, 8, 9, 2, 2]
// example 4: array_pad([ 7, 8, 9 ], -5, 'a')
// returns 4: [ 'a', 'a', 7, 8, 9 ]
let pad: Array = []
const newArray: Array = []
let newLength = 0
let diff = 0
let i = 0
if (Array.isArray(input) && !Number.isNaN(padSize)) {
newLength = padSize < 0 ? padSize * -1 : padSize
diff = newLength - input.length
if (diff > 0) {
for (i = 0; i < diff; i++) {
newArray[i] = padValue
}
pad = padSize < 0 ? [...newArray, ...input] : [...input, ...newArray]
} else {
pad = input
}
}
return pad
}
{% endcodeblock %}
{% codeblock lang:javascript %}
export function array_pad(input, padSize, padValue) {
// discuss at: https://locutus.io/php/array_pad/
// parity verified: PHP 8.3
// original by: Waldo Malqui Silva (https://waldo.malqui.info)
// example 1: array_pad([ 7, 8, 9 ], 2, 'a')
// returns 1: [ 7, 8, 9]
// example 2: array_pad([ 7, 8, 9 ], 5, 'a')
// returns 2: [ 7, 8, 9, 'a', 'a']
// example 3: array_pad([ 7, 8, 9 ], 5, 2)
// returns 3: [ 7, 8, 9, 2, 2]
// example 4: array_pad([ 7, 8, 9 ], -5, 'a')
// returns 4: [ 'a', 'a', 7, 8, 9 ]
let pad = []
const newArray = []
let newLength = 0
let diff = 0
let i = 0
if (Array.isArray(input) && !Number.isNaN(padSize)) {
newLength = padSize < 0 ? padSize * -1 : padSize
diff = newLength - input.length
if (diff > 0) {
for (i = 0; i < diff; i++) {
newArray[i] = padValue
}
pad = padSize < 0 ? [...newArray, ...input] : [...input, ...newArray]
} else {
pad = input
}
}
return pad
}{% endcodeblock %}