Skip to main content
zerotal

Documentation


Documentation / @zerotal/testing / index / fake

Variable: fake

const fake: object

Defined in: testing/src/fake.ts:150

Type Declaration

pick()

pick<T>(arr): T

Random element from an array.

Type Parameters

T

T

Parameters

arr

readonly T[]

Returns

T

shuffle()

shuffle<T>(arr): T[]

Shuffled copy of an array.

Type Parameters

T

T

Parameters

arr

readonly T[]

Returns

T[]

sample()

sample<T>(arr, n): T[]

n unique random elements from an array.

Type Parameters

T

T

Parameters

arr

readonly T[]

n

number

Returns

T[]

number()

number(min?, max?): number

Random integer between min and max (inclusive).

Parameters

min?

number = 0

max?

number = 1000

Returns

number

float()

float(min?, max?, decimals?): number

Random float between min and max with optional decimal places.

Parameters

min?

number = 0

max?

number = 1

decimals?

number = 2

Returns

number

boolean()

boolean(trueWeight?): boolean

Random boolean, optionally weighted (default 50/50).

Parameters

trueWeight?

number = 0.5

Returns

boolean

uuid()

uuid(): string

UUID v4.

Returns

string

string()

string(length?): string

Random alphanumeric string of given length.

Parameters

length?

number = 10

Returns

string

maybe()

maybe<T>(value, probability?): T | null

Return value with given probability; null otherwise.

Type Parameters

T

T

Parameters

value

T

probability?

number = 0.5

Returns

T | null

Example

fake.maybe(fake.phone(), 0.7)  // phone 70% of the time

date()

date(from?, to?): Date

Random Date in the given range (defaults: last year → now).

Parameters

from?

Date

to?

Date

Returns

Date

pastDate()

pastDate(years?): Date

Random Date in the past, within years years.

Parameters

years?

number = 3

Returns

Date

futureDate()

futureDate(years?): Date

Random Date in the future, within years years.

Parameters

years?

number = 2

Returns

Date

isoDate()

isoDate(from?, to?): string

ISO 8601 date string (e.g. "2024-03-15T10:23:00.000Z").

Parameters

from?

Date

to?

Date

Returns

string

timestamp()

timestamp(): number

Unix timestamp in seconds.

Returns

number

firstName()

firstName(): string

Random first name drawn from the full SA-diverse pool.

Returns

string

lastName()

lastName(): string

Random last name drawn from the full SA-diverse pool.

Returns

string

name()

name(): string

Full name: "FirstName LastName".

Returns

string

email()

email(opts?): string

Realistic email address.

Parameters

opts?
name?

string

Seed string (defaults to a generated name).

corporate?

boolean

If true, uses a slugified company domain (.co.za).

number?

boolean

Append a number to the local part (default: 50% chance).

Returns

string

phone()

phone(): string

SA mobile phone number. Format: "0XX XXX XXXX"

Returns

string

city()

city(): string

Returns

string

province()

province(): string

Returns

string

suburb()

suburb(): string

Returns

string

streetAddress()

streetAddress(): string

"12 Mandela Street" style address line.

Returns

string

postalCode()

postalCode(): string

4-digit SA postal code.

Returns

string

address()

address(): string

Full postal address.

Returns

string

company()

company(): string

Returns

string

jobTitle()

jobTitle(): string

Returns

string

department()

department(): string

Returns

string

word()

word(): string

Single word from the lorem vocabulary.

Returns

string

words()

words(n?): string

n space-joined words.

Parameters

n?

number = 5

Returns

string

sentence()

sentence(opts?): string

A single sentence.

Parameters

opts?
length?

TextLength

'short' (5-9 words) | 'medium' (11-18) | 'long' (22-38). Default: 'medium'.

words?

number

Exact word count override.

Returns

string

sentences()

sentences(n?, opts?): string

n sentences joined with a space.

Parameters

n?

number = 3

opts?
length?

TextLength

Returns

string

paragraph()

paragraph(opts?): string

A paragraph of sentences.

Parameters

opts?
length?

TextLength

'short' (2-3 sentences) | 'medium' (3-5) | 'long' (5-8). Default: 'medium'.

Returns

string

paragraphs()

paragraphs(n?, opts?): string

n paragraphs joined with a blank line.

Parameters

n?

number = 3

opts?
length?

TextLength

Returns

string

title()

title(): string

A realistic article/post title. Returns a pre-written phrase 70% of the time; constructs one otherwise.

Returns

string

slug()

slug(text?): string

URL-safe slug from a title or generated one.

Parameters

text?

string

Returns

string

url()

url(opts?): string

Random HTTPS URL.

Parameters

opts?
path?

boolean

Returns

string

password()

password(opts?): string

Realistic-looking password.

Parameters

opts?
length?

number

Total length (default: 12).

simple?

boolean

If true, lowercase + digits only (for test fixtures).

Returns

string