Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@jfrazx/asarray

jfrazx1.8kMIT1.0.1TypeScript support: included

Ensures returned value is always an Array

array, arguments, convert, object, typed array, keys, typescript

readme

asArray

Ensure that the information you are working with is always an Array.
Optionally, you may pass a callback that will transform a single value, or map key value pairs from an object


devDependencies Status Master Build Status codecov License

Install

npm install @jfrazx/asarray

or

yarn add @jfrazx/asarray

Usage

asArray<T, TResult>(data: T | T[], callback?: (value: T) => TResult): Array<T | TResult>

asArray<T extends Object, K extends keyof T>(data: T, extractObjectValues?: boolean, callback?: (key: K) => T[K]): Array<T | T[K]>

import { asArray } from '@jfrazx/asarray';

const person: Person = { name: 'Bart Simpson', age: 10 };

asArray('this string will be in an array');
// => ['this string will be in an array']

asArray([8, 6, 7, 5, 3, 0, 9]);
// => [8, 6, 7, 5, 3, 0, 9]

asArray(person);
// => [{ name: 'Bart Simpson', age: 10 }];

asArray(person, (object: T) => object.name);
// => ['Bart Simpson'];

asArray(person, true);
// => ['Bart Simpson', 10];

asArray(person, true, (key: K, object: T, index: number, array: K[]) => [k, o[k]]);
// => [['name', 'Bart Simpson'], ['age', 10]];

function things() {
  asArray(arguments);
  // => ['koalas', 'marsupial']
}

things('koalas', 'marsupial');