fakemake
v0.0.2
Published
Small dependency-free fake data generator
Readme
fakemake
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
Installation
npm install fakemakeBasic usage
import 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,
},
]Generate multiple items
Use the amount option:
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
API
fakemake(callback, options?)
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
Generators
String
string(amount?, ref?)
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"] |
Words are selected independently, so duplicates are possible
Number
number(min?, max?)
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 |
Both boundaries are inclusive
Boolean
bool(chance?)
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 |
Values below 0 are treated as 0, and values above 1 are treated as 1
Generate primitive values
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 });Standalone generators
The generator factories are also exported separately
String generator
import { createStringGenerator } from "fakemake";
const word = createStringGenerator();
const color = createStringGenerator(1, ["red", "green", "blue"]);
word();
color();
color(2);Number generator
import { createNumberGenerator } from "fakemake";
const percentage = createNumberGenerator(0, 100);
percentage();
percentage(1, 10);Boolean generator
import { createBoolGenerator } from "fakemake";
const mostlyTrue = createBoolGenerator(0.8);
mostlyTrue();
mostlyTrue(0.2);Arguments passed when calling a generator override the defaults passed to its factory
