Small dependency-free fake data generator
fakemake creates arrays of objects or values using configurable string, number and boolean generators. It works in Node.js, browsers, Bun, Deno and other JavaScript runtimes
npm install fakemakeimport fakemake from "fakemake";
const users = fakemake(({ string, number, bool }) => ({
name: string(2),
age: number(18, 80),
active: bool(),
}));
console.log(users);By default, one item is generated.
[
{
name: "lorem ipsum",
age: 34,
active: true,
},
]Use the amount option:
// you can use named export too
import { fakemake } from "fakemake";
const users = fakemake(
({ string, number, bool }) => ({
id: number(1, 10_000),
name: string(2),
active: bool(),
}),
{ amount: 10 },
);The return type is inferred from the callback
function fakemake<T>(
callback: (generators: Generators) => T,
options?: Options,
): T[];type Generators = {
string: (amount?: number, ref?: readonly string[]) => string;
number: (min?: number, max?: number) => number;
bool: (chance?: number) => boolean;
};type Options = {
amount?: number;
string?: {
amount?: number;
ref?: readonly string[];
};
number?: {
min?: number;
max?: number;
};
bool?: {
chance?: number;
};
};Creates amount values by calling callback with a set of generators
function string(amount?: number, ref?: readonly string[]): string;string(); // uses configured defaults
string(3); // generates 3 words from options ref
string(2, ["red", "green", "blue"]) // generates 2 words from provided ref| Argument | Default |
|---|---|
amount |
1 |
ref |
["lorem", "ipsum", "dolor", "sit", "amet"] |
Important
Words are selected independently, so duplicates are possible
function number(min?: number, max?: number): number;number(); // uses configured defaults
number(10, 50) // generates a random integer between 10 and 50
number(0.2, 0.8); // if the range contains no integer, the generator throws RangeError| Argument | Default |
|---|---|
min |
0 |
max |
100 |
Important
Both boundaries are inclusive
function bool(chance?: number): boolean;bool(); // uses configured defaults
bool(0.8); // 80% chance of true
bool(0); // always false
bool(1); // always true| Argument | Default |
|---|---|
chance |
0.5 |
Important
Values below 0 are treated as 0, and values above 1 are treated as 1
The callback can return any value, not only objects:
const ids = fakemake(({ number }) => number(1, 100), { amount: 5 });const labels = fakemake(({ string }) => string(2), { amount: 3 });The generator factories are also exported separately
import { createStringGenerator } from "fakemake";
const word = createStringGenerator();
const color = createStringGenerator(1, ["red", "green", "blue"]);
word();
color();
color(2);import { createNumberGenerator } from "fakemake";
const percentage = createNumberGenerator(0, 100);
percentage();
percentage(1, 10);import { createBoolGenerator } from "fakemake";
const mostlyTrue = createBoolGenerator(0.8);
mostlyTrue();
mostlyTrue(0.2);Note
Arguments passed when calling a generator override the defaults passed to its factory