1.0.1 • Published 3 years ago

@jfrazx/asarray v1.0.1

Weekly downloads
139
License
MIT
Repository
github
Last release
3 years ago

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) => TK): Array<T | TK>

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');