2017/01/13

今天学习了mock.js里的一些方法,总结如下:

必须引用mock.js源码,然后
var Mock = require(mockjs)
var Random = Mock.Random

以下内容中的?表示可选值

  • Basic:
boolean(min?, max?, current?)
// 返回一个布尔值
natural(min?, max?)
// 返回一个自然数(大于等于0的整数)
integer(min?, max?)
// 返回一个整数
float(min?, max?, dmin?, dmax?)
// 返回一个浮点数
character(pool?)
 // 返回随机字符,pool表示字符池,可选择目标'lower', 'upper', 'number', 'symbol'
string(pool?, min?, max?)
// 返回一个字符串
range(start, stop?, step?)
// 返回一个整型数组
  • Date:
Format Description Example
yyyy A full numeric representation of a year, 4 digits 1999 or 2003
yy A two digit representation of a year 99 or 03
y A two digit representation of a year 99 or 03
MM Numeric representation of a month, with leading zeros 01 to 12
M Numeric representation of a month, without leading zeros 1 to 12
dd Day of the month, 2 digits with leading zeros 01 to 31
d Day of the month without leading zeros 1 to 31
HH 24-hour format of an hour with leading zeros 00 to 23
H 24-hour format of an hour without leading zeros 0 to 23
hh 12-hour format of an hour without leading zeros 1 to 12
h 12-hour format of an hour with leading zeros 01 to 12
mm Minutes, with leading zeros 00 to 59
m Minutes, without leading zeros 0 to 59
ss Seconds, with leading zeros 00 to 59
s Seconds, without leading zeros 0 to 59
SS Milliseconds, with leading zeros 000 to 999
S Milliseconds, without leading zeros 0 to 999
A Uppercase Ante meridiem and Post meridiem AM or PM
a Lowercase Ante meridiem and Post meridiem am or pm
T Milliseconds, since 1970-1-1 00:00:00 UTC 759883437303
date(format?)
// 返回一个日期字符串
time(format?)
// 返回一个时间字符串
datetime(format?)
// 返回一个日期时间字符串
now(unit?, format?)
// 返回现在的日期和时间字符串
  • image
image(size?, background?, foreground?, format?, text?)
// 返回一个图片地址
dataImage(size?, text?)
// 返回一个base64地址
  • color
color()
// 返回一个有吸引力的颜色,格式为 '#RRGGBB'。
hex()
// 返回一个有吸引力的颜色,格式为 '#RRGGBB'。
rgb()
// 返回一个有吸引力的颜色,格式为 'rgb(255, 255, 0)。
rgba()
// 格式为 'rgba(255, 255, 0, .5)'。
hsl()
// 格式为 'hsl(h, s, l)'。
  • text
paragraph(len?) // len是指文本中句子的个数
paragraph(min?, max?) // min指文本中句子最小的个数,max指最大的个数
// 返回一个段落
cparagraph(len?)
cparagraph(min?, max?)
// 返回一个中文段落
sentence(len?)
sentence(min?, max?)
// 返回一个段落,首字母大写
csentence(len?)
csentence(min?, max?)
// 返回一个中文段落
word(len?)
word(min?, max?)
// 返回一个单词
cword(pool?)
cword(min?, max?)
// 返回一个中文汉字
title(len?)
title(min?, max?)
// 返回一个标题
ctitle(len?)
ctitle(min?, max?)
// 返回一个中文标题
  • name:
first()
// 返回一个英文名
last()
// 返回一个英文姓
name(middle?)// middle是一个布尔值,表示是否要生成中间名
// 返回一个常见的英文姓名
cfirst()
// 返回中文姓
clast()
// 返回中文名
cname()
// 返回中文姓名
  • web:
url(protocol?, host?)
// 返回一个域名
protocol()
domain()
tld()
// 返回一顶级域名
email(domain?)
ip()
// 返回ip地址
  • address:
region()
// 返回一个中文大区,如‘华北’
province()
// 省
city(prefix?)// prefix是布尔值,表示是否要生成所属的省份
// 市
country(prefix?)
// 县
zip()
// 邮政编码
  • helper:
capitalize(word)
// 把word的第一个字母大写
upper(word)
// 全部大写
lower(word)
// 全部小写
pick(arr)
// 从数组中随机选取一个元素
shuffle(arr)
// 打乱数组中元素的顺序并返回
  • miscellaneous:
guid()
id()
// 随机生成一个18位身份证id
increment(step?)
// 生成一个全局自增函数

你可能感兴趣的:(2017/01/13)