toArray

toArray

Returns array representation of a value.

  • when the value is already an array it return the value.
  • when the value is undefined or null it returns [].
  • when the value is iterable it returns array representation of the value.
  • otherwise it wraps the value in an array.

Import

import { toArray } from '@fullstacksjs/toolbox';

Signature

function toArray(value: null | undefined): [];
function toArray<T extends any[]>(value: T): T;
function toArray<T>(value: Iterable<T> | T[]): T[];
function toArray<T>(value: T): [T];
function toArray<T>(value: Iterable<T> | T | T[] | null | undefined) {}

Examples

toArray(null)      // []
toArray(undefined) // []
toArray('str')     // ['s', 't', 'r']
toArray([1])       // [1]
toArray(2)         // [2]