今天學(xué)習(xí)了mock.js里的一些方法,總結(jié)如下:
必須引用mock.js源碼寓搬,然后
var Mock = require(mockjs)
var Random = Mock.Random
以下內(nèi)容中的?
表示可選值
- Basic:
boolean(min?, max?, current?)
// 返回一個布爾值
natural(min?, max?)
// 返回一個自然數(shù)(大于等于0的整數(shù))
integer(min?, max?)
// 返回一個整數(shù)
float(min?, max?, dmin?, dmax?)
// 返回一個浮點數(shù)
character(pool?)
// 返回隨機(jī)字符珍昨,pool表示字符池,可選擇目標(biāo)'lower', 'upper', 'number', 'symbol'
string(pool?, min?, max?)
// 返回一個字符串
range(start, stop?, step?)
// 返回一個整型數(shù)組
- 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?)
// 返回現(xiàn)在的日期和時間字符串
- 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是指文本中句子的個數(shù)
paragraph(min?, max?) // min指文本中句子最小的個數(shù)哑姚,max指最大的個數(shù)
// 返回一個段落
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?)
// 返回一個標(biāo)題
ctitle(len?)
ctitle(min?, max?)
// 返回一個中文標(biāo)題
- name:
first()
// 返回一個英文名
last()
// 返回一個英文姓
name(middle?)// middle是一個布爾值芜茵,表示是否要生成中間名
// 返回一個常見的英文姓名
cfirst()
// 返回中文姓
clast()
// 返回中文名
cname()
// 返回中文姓名
- web:
url(protocol?, host?)
// 返回一個域名
protocol()
domain()
tld()
// 返回一頂級域名
email(domain?)
ip()
// 返回ip地址
- address:
region()
// 返回一個中文大區(qū)叙量,如‘華北’
province()
// 省
city(prefix?)// prefix是布爾值,表示是否要生成所屬的省份
// 市
country(prefix?)
// 縣
zip()
// 郵政編碼
- helper:
capitalize(word)
// 把word的第一個字母大寫
upper(word)
// 全部大寫
lower(word)
// 全部小寫
pick(arr)
// 從數(shù)組中隨機(jī)選取一個元素
shuffle(arr)
// 打亂數(shù)組中元素的順序并返回
- miscellaneous:
guid()
id()
// 隨機(jī)生成一個18位身份證id
increment(step?)
// 生成一個全局自增函數(shù)