Skip to content

castArray

Cast a value into an array

76 bytes

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 _
_
.
castArray<number>(value: number): number[]
export castArray

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 _
_
.
castArray<number[]>(value: number[]): number[]
export castArray

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 _
_
.
castArray<string>(value: string): string[]
export castArray

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 _
_
.
castArray<null>(value: null): null[]
export castArray

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 _
_
.
castArray<undefined>(value: undefined): undefined[]
export castArray

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]