provoke-array
v1.0.6
Published
Array generator
Downloads
8
Maintainers
Readme
provoke-array generates an array for you
Installation
npm install provoke-array
or
yarn add provoke-array
provokeArray((length: Number), (customReturn: Function));
Examples
Basic
By default it returns an array with 3 positions containing empty objects (this is helpful in tests to use mocked data).
import provokeArray from "provoke-array";
provokeArray();
// Result:
// [{}, {}, {}]
Custom return
You can customize what it returns by passing a function as 2nd argument.
import provokeArray from "provoke-array";
provokeArray(2, (i) => ({
id: i,
name: `Name: ${i}`
}));
// Result:
// [
// {
// id: 0,
// name: 'Name: 0'
// },
// {
// id: 1,
// name: 'Name: 1'
// }
// ]
Use with faker
You can use it with faker to generate more custom data.
import provokeArray from "provoke-array";
import faker from "faker";
provokeArray(2, i => ({
id: i,
name: faker.name.firstName()
}));
// Result:
// [
// {
// id: 0,
// name: 'John'
// },
// {
// id: 1,
// name: 'Alice'
// }
// ]