Skip to content

castArray

Cast a value into an array

76 bytes
since v12.2.0

Usage

The castArray function ensures that the input value is always returned as an array. If the input is already an array, it returns a shallow copy of the array. If the input is not an array, it wraps the input in a new array.

import * as
import _
_
from 'radashi'
const
const numberToArray: number[]
numberToArray
=
import _
_
.
function castArray<number>(value: number): number[]

Casts the given value to an array. If the value is already an array, a shallow copy is returned. Otherwise, a new array containing the value is returned.

@seehttps://radashi.js.org/reference/array/castArray

@example

castArray(1) // => [1]
castArray([1, 2]) // => [1, 2]
castArray(null) // => [null]
castArray(undefined) // => [undefined]

@version12.2.0

castArray
(1) // => [1]
const
const arrayToArray: number[]
arrayToArray
=
import _
_
.
function castArray<number[]>(value: number[]): number[]

Casts the given value to an array. If the value is already an array, a shallow copy is returned. Otherwise, a new array containing the value is returned.

@seehttps://radashi.js.org/reference/array/castArray

@example

castArray(1) // => [1]
castArray([1, 2]) // => [1, 2]
castArray(null) // => [null]
castArray(undefined) // => [undefined]

@version12.2.0

castArray
([1, 2, 3]) // => [1, 2, 3]
const
const stringToArray: string[]
stringToArray
=
import _
_
.
function castArray<string>(value: string): string[]

Casts the given value to an array. If the value is already an array, a shallow copy is returned. Otherwise, a new array containing the value is returned.

@seehttps://radashi.js.org/reference/array/castArray

@example

castArray(1) // => [1]
castArray([1, 2]) // => [1, 2]
castArray(null) // => [null]
castArray(undefined) // => [undefined]

@version12.2.0

castArray
('hello') // => ['hello']
const
const nullToArray: null[]
nullToArray
=
import _
_
.
function castArray<null>(value: null): null[]

Casts the given value to an array. If the value is already an array, a shallow copy is returned. Otherwise, a new array containing the value is returned.

@seehttps://radashi.js.org/reference/array/castArray

@example

castArray(1) // => [1]
castArray([1, 2]) // => [1, 2]
castArray(null) // => [null]
castArray(undefined) // => [undefined]

@version12.2.0

castArray
(null) // => [null]
const
const undefinedToArray: undefined[]
undefinedToArray
=
import _
_
.
function castArray<undefined>(value: undefined): undefined[]

Casts the given value to an array. If the value is already an array, a shallow copy is returned. Otherwise, a new array containing the value is returned.

@seehttps://radashi.js.org/reference/array/castArray

@example

castArray(1) // => [1]
castArray([1, 2]) // => [1, 2]
castArray(null) // => [null]
castArray(undefined) // => [undefined]

@version12.2.0

castArray
(
var undefined
undefined
) // => [undefined]