Skip to content

castArrayIfExists

Cast a non-nullish value into an array

94 bytes

Usage

The castArrayIfExists function ensures that a non-nullish 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. Nullish values (null or undefined) are passed through as is.

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

Casts the given value to an array if it's not equal to null or undefined. If the value is an array, it returns a shallow copy of the array. Otherwise, it returns a new array containing the value.

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

@example

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

@version12.2.0

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

Casts the given value to an array if it's not equal to null or undefined. If the value is an array, it returns a shallow copy of the array. Otherwise, it returns a new array containing the value.

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

@example

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

@version12.2.0

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

Casts the given value to an array if it's not equal to null or undefined. If the value is an array, it returns a shallow copy of the array. Otherwise, it returns a new array containing the value.

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

@example

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

@version12.2.0

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

Casts the given value to an array if it's not equal to null or undefined. If the value is an array, it returns a shallow copy of the array. Otherwise, it returns a new array containing the value.

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

@example

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

@version12.2.0

castArrayIfExists
(null) // => null
const
const undefinedToArray: undefined
undefinedToArray
=
import _
_
.
castArrayIfExists<undefined>(value: undefined): undefined
export castArrayIfExists

Casts the given value to an array if it's not equal to null or undefined. If the value is an array, it returns a shallow copy of the array. Otherwise, it returns a new array containing the value.

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

@example

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

@version12.2.0

castArrayIfExists
(
var undefined
undefined
) // => undefined