# 生成器(@mokia/producer)

# Basic

# boolean 布尔类型

boolean(chance?: number)
Run
boolean();
boolean(1);

# integer 整型

integer(max?: number)
Run
integer();
integer(5);
integer(min: number, max: number)
Run
integer(5, 10);

# natural 自然数

natural(max?: number)
Run
natural();
natural(5);
natural(min: number, max: number)
Run
natural(5, 10);

# float 浮点数

float(max?: number)
Run
float();
float(5);
float(min: number, max: number)
Run
float(5, 10);
float(min: number, max: number, fixed: number)
Run
float(5, 10, 3);
float(min: number, max: number, dMin: number, dMax: number)
Run
float(5, 10, 3, 5);

# char 字符

char(pool?: string)
Run
char();
char('ABC');

# string 字符串

string(length?: number)
Run
string();
string(5);
string(pool: string, length?: number)
Run
string('ABC');
string('ABC', 5);
string(pool: string, min: number, max: number)
Run
string('ABC', 5, 10);

# Color

# hex 十六进制颜色字符串

hex()
Run
hex();

# rgb RGB 颜色字符串

rgb()
Run
rgb();

# rgba RGBA 颜色字符串

rgba()
Run
rgba();

# hsl HSL 颜色字符串

hsl()
Run
hsl();

# Complex

# iterate 迭代器

iterate(iterator: Function, length?: number)
Run
iterate(() => integer());
iterate(() => integer(), 3);

# oneOf 取其中的一个

oneOf(list: any[])
Run
oneOf(['male', 'female', 'unknown']);

# manyOf 取其中的多个

manyOf(list: any[], length?: number)
Run
manyOf(['apple', 'banana', 'grape', 'orange', 'pear']);
manyOf(['apple', 'banana', 'grape', 'orange', 'pear'], 3);
manyOf(list: any[], min: number, max: number)
Run
manyOf(['apple', 'banana', 'grape', 'orange', 'pear'], 3, 5);

# pick 挑选属性

pick(obj: any, length?: number)
Run
pick({ a: 1, b: 2, c: 3 });
pick({ a: 1, b: 2, c: 3 }, 3);
pick(obj: any, props: string[])
Run
pick({ a: 1, b: 2, c: 3 }, ['a', 'b']);
pick(obj: any, min: number, max: number)
Run
pick({ a: 1, b: 2, c: 3 }, 2, 3);

# Date

# datetime 日期时间字符串

TIP

基于 dayjs,支持格式请查看这里 (opens new window)

datetime(format?: string)
Run
datetime();
datetime('DD/MM/YYYY HH:mm:ss');
datetime(format: string, min: Date | number | string, max: Date | number | string)
Run
datetime('YYYY-MM-DD', 0, Date.now());
datetime('YYYY-MM-DD', '2021-01-01', '2021-12-31');

# date 日期字符串

date(format?: string)
Run
date();

# time 时间字符串

time(format?: string)
Run
time();

# now 当前时间字符串

now(format?: string)
Run
now();

# timestamp 当前时间字符串

timestamp(format?: string)
Run
timestamp();

# Generate

# generate 复合数据

generate(schema: any)
Run
generate(5);
generate([1, '2', true]);
generate({ a: 1, b: '2', c: true });
generate(schema: any)
Run
generate(() => integer());
generate([() => integer()]);
generate({ foo: () => integer() });
generate({
  foo: {
    bar: {
      baz: () => integer(),
    },
  },
});
generate(schema: any)
Run
class Person {
  constructor() {
    this.name = fullName();
  }
}

generate(Person);

# list 列表

TIP

等价于 iterategenerate 的组合。

list(schema: any, length?: number)
Run
list({ foo: () => integer() });
list({ foo: () => integer() }, 3);
list(schema: any, min: number, max: number)
Run
list({ foo: () => integer() }, 3, 5);

# Id

# uuid UUID 字符串

uuid()
Run
uuid();

# increment 自增 ID 数字

increment()
Run
increment();

# Image

# imageDataURL 图片 DataURL

imageDataURL(size?: string, text?: string, background?: string, foreground?: string)
Run
<img id="image1" src="" />
<img id="image2" src="" />
<img id="image3" src="" />
<img id="image4" src="" />
<img id="image5" src="" />
$('#image1').src = imageDataURL();
$('#image2').src = imageDataURL('128x128');
$('#image3').src = imageDataURL('200x100', 'Hello World');
$('#image4').src = imageDataURL('200x100', 'Custom Color', '#499DF2');
$('#image5').src = imageDataURL('200x100', 'Custom Color', 'white', 'black');

# Misc

# pagination 分页

pagination(schema: any)
Run
pagination({ foo: 1 });
pagination(schema: any, options: PaginationOptions)
Run
pagination({ foo: 1 }, { page: 1, pageSize: 5, total: 25 });
pagination(
  { foo: 1 },
  {
    fields: {
      list: 'data',
      page: 'current',
      pageSize: 'size',
      total: 'count',
      totalPages: 'pageCount',
    },
  },
);

# Person

# age 年龄

age(min?: number, max?: number)
Run
age();
age(18);
age(0, 18);

# birthday 生日

birthday(format?: string)
Run
birthday();
birthday('DD/MM/YYYY');

# firstName 名

firstName()
Run
firstName();

# lastName 姓

lastName()
Run
lastName();

# fullName 姓名

fullName()
Run
fullName();

# Text

# word 单词

word(length?: number)
Run
word();
word(5);
word(min: number, max: number)
Run
word(5, 10);

# title 标题

title(length?: number)
Run
title();
title(5);
title(min: number, max: number)
Run
title(2, 5);

# sentence 句子

sentence(length?: number)
Run
sentence();
sentence(5);
sentence(min: number, max: number)
Run
sentence(5, 10);

# paragraph 段落

paragraph(length?: number)
Run
paragraph();
paragraph(5);
paragraph(min: number, max: number)
Run
paragraph(3, 5);

# passage 文章

passage(length?: number)
Run
passage();
passage(5);
passage(min: number, max: number)
Run
passage(3, 5);

# Web

# protocol 协议

protocol()
Run
protocol();

# tld 顶级域名

tld()
Run
tld();

# ip IP v4

ip()
Run
ip();

# ipv6 IP v6

ipv6()
Run
ipv6();

# port 端口号

port()
Run
port();

# domain 域名

domain(tld?: string)
Run
domain();
domain('com');

# pathname 路径

pathname(length?: number)
Run
pathname();
pathname(5);

# url URL

url(protocol?: string, host?: string, prefix?: string)
Run
url();
url('https');
url('https', 'github.com');
url('https', 'github.com', '/varharrie');

# email 电子邮箱地址

email(domain?: string)
Run
email();
email('gmail.com');

# [zh-CN] Person

# firstName 名

zhCN.firstName()
Run
zhCN.firstName();

# lastName 姓

zhCN.lastName()
Run
zhCN.lastName();

# fullName 姓名

zhCN.fullName()
Run
zhCN.fullName();

# phoneNumber 手机号码

zhCN.phoneNumber()
Run
zhCN.phoneNumber();

# idNumber 身份证号码

zhCN.idNumber()
Run
zhCN.idNumber();

# [zh-CN] Region

# provinceName 省

zhCN.provinceName()
Run
zhCN.provinceName();

# cityName 市

zhCN.cityName()
Run
zhCN.cityName();

# countyName 县

zhCN.countyName()
Run
zhCN.countyName();

# regionName 地区

zhCN.regionName(separator?: string)
Run
zhCN.regionName();
zhCN.regionName(',');

# [zh-CN] Text

# word 字

zhCN.word(length?: number)
Run
zhCN.word();
zhCN.word(5);
zhCN.word(min: number, max: number)
Run
zhCN.word(5, 10);

# title 标题

zhCN.title(length?: number)
Run
zhCN.title();
zhCN.title(5);
zhCN.title(min: number, max: number)
Run
zhCN.title(2, 5);

# sentence 句子

zhCN.sentence(length?: number)
Run
zhCN.sentence();
zhCN.sentence(5);
zhCN.sentence(min: number, max: number)
Run
zhCN.sentence(5, 10);

# paragraph 段落

zhCN.paragraph(length?: number)
Run
zhCN.paragraph();
zhCN.paragraph(5);
zhCN.paragraph(min: number, max: number)
Run
zhCN.paragraph(3, 5);

# passage 文章

zhCN.passage(length?: number)
Run
zhCN.passage();
zhCN.passage(5);
zhCN.passage(min: number, max: number)
Run
zhCN.passage(3, 5);