mock
文档:http://mockjs.com/examples.html#String
npm安装:npm install mockjs --save-dev
1、支持不携带参数。例@boolean
2、支持携带空参数。例@boolean()
3、支持携带参数。例@boolean(1, 9, true)
数据占位符定义
Basic
@boolean( min?, max?, current? )
@natural( min?, max? )
@integer( min?, max? )
@float( min?, max?, dmin?, dmax? ) // 小数点的位数限制dmin-dmax
@character( pool? ) // pool = lower/upper/number/symbol
@string( pool?, min?, max? )
@range(start?, stop, step?)
Date
@date( format? ) // format日期格式
@time( format? )
@datetime( format? )
@now( unit?, format? )
Image
@image( size?, background?, foreground?, format?, text? ) // size = 长*宽
@dataImage( size?, text? )
// 例 image('200x100', '#894FC4', '#FFF', 'png', '!')
// (长*宽, 背景颜色,字体颜色,图片类型,文本信息)
Color
@color()
@hex()
@rgb()
@rgba()
@hsl()
Address [区域|省|市|国家|邮政编码]
@region()
@province()
@city( prefix? )
@county( prefix? )
@zip()
Web
@url()
@domain()
@protocol()
@tld()
@email()
@ip()
Name [中英文-姓|名|姓名]
@first()
@last()
@name( middle? )
@cfirst()
@clast()
@cname()
Text [中英文-段落|词组|标题|句子(最小值,最大值;随机3-5的数量)]
@paragraph( min?, max? )
@sentence( min?, max? )
@word( min?, max? )
@title( min?, max? )
@cparagraph( min?, max? )
@csentence( min?, max? )
@cword( pool?, min?, max? )
@ctitle( min?, max? )
Helper
@capitalize( word )
@upper( str )
@lower( str )
@pick( arr )
@shuffle( arr )
Miscellaneous
@guid()
@id()
@increment( step? )
let dlist = Mock.mock({
"string|1-10": "好", // 随机生成1-10个字符
"string|3": "好", // 生成3倍
"number|+1": 1, // 按递增的数字结果返回
"number|1-100": 1, // 1-100的随机数
"number|1-100.1-10": 1, // 1-100的随机数+随机1-10位小数
"number|123.1-10": 1, // 数值123+随机1-10位的小数
"number|123.3": 1, // 数值123+3位小数
"number|123.10": 1.123, // 数值123+10位的小数
"boolean|1": true, // 布尔值
"boolean|1-2": true, // 随机布尔值
"object|2": { // 数量为2个属性的对象
"310000": "上海市",
"320000": "江苏省",
"330000": "浙江省",
"340000": "安徽省"
},
"object|2-4": { // 随机数量为2-4个属性的对象
"110000": "北京市",
"120000": "天津市",
"130000": "河北省",
"140000": "山西省"
},
"array|1": [ // 长度为1的数组
"AMD",
"CMD",
"UMD"
],
"array|+1": [ // 按递增顺序返回
"AMD",
"CMD",
"UMD"
],
"array|1-10": [ // 随机为1-10长度的对象数组
{
"name|+1": [
"Hello",
"Mock.js",
"!"
]
}
],
"array|1-10": [ // 随机为1-10数量的字符型数组
"Mock.js"
],
"array|1-10": [
"Hello",
"Mock.js",
"!"
],
'foo': 'Syntax Demo',
'name': function() {
return this.foo
},
// regexp
'regexp': /[a-z][A-Z][0-9]/,
'regexp': /\w\W\s\S\d\D/,
'regexp': /\d{5,10}/,
'regexp|3': /\d{5,10}\-/,
'regexp|1-5': /\d{5,10}\-/,
// Path
"foo1": "Hello",
"nested1": {
"a": {
"b": {
"c": "Mock.js"
}
}
},
"absolutePath": "@/foo1 @/nested1/a/b/c",
"foo2": "Hello",
"nested2": {
"a": {
"b": {
"c": "Mock.js"
}
}
},
"relativePath": {
"a": {
"b": {
"c": "@../../../foo2 @../../../nested2/a/b/c"
}
}
},
"list|5": [
{
online: '@boolean',
age: '@natural(0, 100)', // 0-100的自然数
classes: '@integer(1, 10)', // 1-10的整数
score: '@float(0, 100, 3)', // 0-100的浮点数第三位为小数点
id: '@id',
cname: '@cname',
ename: '@name',
sex: '@cword("01")',
address: '@county(true)',
birthday: '@date',
colour: function() {
let c = [ "red", "yellow", "pink", "blue", "green", "black", "white" ]
return [c[Mock.mock('@integer(0, 6)')], c[Mock.mock('@integer(2, 6)')]]
},
email: '@email',
phone: /^1(3|4|5|6|7|8|9)\d{9}$/,
status: '@cword("IO")',
introduce: '@cparagraph(1, 3)',
readBook: '@ctitle(3, 5)',
readProgress: '@float(10, 100, 3, 2)',
readRecommend: '@ctitle'+'-'+'@cword'+'-'+'@sentence',
pgRecommend: '@paragraph(2)',
group: '@cword("零一二三四五六七八九十", 1)',
language: '@float(0, 100, 3)'
math: '@float(0, 100, 3)',
english: '@float(0, 100, 3)',
}
]
})
浙公网安备 33010602011771号