Skip to content

Repository files navigation

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 fakemake

Basic 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:

// 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

API

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

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

Number

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

Boolean

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

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

Note

Arguments passed when calling a generator override the defaults passed to its factory

About

Small dependency-free fake data generator

Resources

Stars

0 stars

Watchers

0 watching

Forks

Contributors

Languages