Skip to content

toArray ​

Category
Export size
min 0.05 kB · gzip 0.07 kB
See also

Normalizes a value or array into an array. The returned array MUST be treated as read-only — when the input is already an array it is returned as-is (no copy) to avoid a per-call allocation on hook hot paths.

ts
  import { 
toArray
} from 'feathers-utils/helpers';

Example ​

ts
import { toArray } from 'feathers-utils/helpers'

toArray(1) // => [1]
toArray([1, 2]) // => [1, 2] (same reference)

// single or multi
for (const item of toArray(context.data)) { ... }

Type declaration ​

Show Type Declarations
ts
/**
 * Normalizes a value or array into an array. The returned array MUST be treated
 * as read-only — when the input is already an array it is returned as-is (no
 * copy) to avoid a per-call allocation on hook hot paths.
 *
 * @example
 * ```ts
 *
 *
 * toArray(1) // => [1]
 * toArray([1, 2]) // => [1, 2] (same reference)
 *
 * // single or multi
 * for (const item of toArray(context.data)) { ... }
 * ```
 *
 * @see https://utils.feathersjs.com/helpers/to-array.html
 */
export declare const toArray: <T>(value: T | readonly T[]) => T[]
ArgumentTypeDescription
valueT | readonly T[]

Coming from lodash ​

Like _.castArray(value), minus the special case for no argument: _.castArray() is [], toArray(undefined) is [undefined].

Released under the MIT License.