Skip to content

Instantly share code, notes, and snippets.

@LevPewPew
Last active January 17, 2022 23:46
Show Gist options
  • Select an option

  • Save LevPewPew/48871626649ff10a680a0bb54ca51248 to your computer and use it in GitHub Desktop.

Select an option

Save LevPewPew/48871626649ff10a680a0bb54ca51248 to your computer and use it in GitHub Desktop.
simplest way to create fake data in typescript using faker package and a iterable array helper method
import faker from "faker";
function createIterable(length: number) {
return Array.from(Array(length));
}
function createFakeData<T>(datumCreator: any, length: number): T[] {
return createIterable(length).map(() => datumCreator());
}
interface Product {
id: string;
name: string;
price: number;
}
const products = createFakeData<Product>(
() => ({
id: faker.datatype.uuid(),
name: faker.commerce.productName(),
price: faker.commerce.price(),
}),
50
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment