A simulation data generator
仿真数据生成器
官方示例
数据模板 'name|rule': value
规则 | 说明 |
---|---|
'name|min-max': value | |
'name|count': value | |
'name|min-max.dmin-dmax': value | |
'name|min-max.dcount': value | |
'name|count.dmin-dmax': value | dmin~dmax 小数保留位数的范围 |
'name|count.dcount': value | |
'name|+step': value | step 数值累加, value为数组时表下标 |
-
name
是属性名 -
value
是初始值,部分情况会用到 -
rule
的 含义 需要依赖value
的类型 才能确定类型 生成效果 字符串 将value 重复次数 数字 取值范围 布尔值 min / (min + max) 的概率为 value,max / (min + max) 的概率为 !value 对象 随机选取若干属性构成对象 rule 决定选取的数量 数组 选取数组的元素,rule表示下标及数量;当 rule 为范围时,是按范围重复指定次数 value 生产一个新数组 函数 执行函数取返回值 正则 生成匹配的字符串 @regexp(/[0-9a-f]{6}/)
Path | @path 可直接获取对应位置值
占位符 @xxx(参数 [, 参数])
相当于引用
可以获得模板的value,并提供再次调用
Mock.mock()
参数
rurl?, // 需要拦截的 URL
rtype?, // 用于规定请求类型
template|function( options ) // 采用 数据模板 或是 通过函数生成
// options 含有 url、type 和 body 三个属性
Mock.setup( settings )
配置拦截 Ajax 请求时的行为
目前只支持设置 timeout: Number(ms) / ‘number1-number2’(范围内随机)
默认 ‘10-100’
Mock.Random
在数据模板中就是 @占位符 无参数时,括号可省略
详细参见
random | 占位符 |
---|---|
Random.boolean( min?, max?, current? ) | @boolean(1, 9, true) |
Random.natural( min?, max? ) | @natural(60, 100) |
Random.integer( min?, max? ) | |
Random.float( min?, max?, dmin?, dmax? ) | |
Random.character( pool? ) | pool: lower upper number symbol |
Random.string( pool?, min?, max? ) | |
Random.range(start?, stop, step?) | |
— | |
Random.date( format? ) | “yyyy yy y MM M dd d” |
Random.time( format? ) | “HH H hh h mm m ss s SS S A a T” |
Random.datetime( format? ) | |
Random.now( unit?, format? ) | |
Random.image( size?, background?, foreground?, format?, text? ) | ‘200x100’, ‘#894FC4’, ‘#FFF’, ‘png’, ‘!’ |
Random.dataImage( size?, text? ) | |
— | |
Random.color() | |
Random.hex() | |
Random.rgb() | |
Random.rgba() | |
Random.hsl() | |
— | |
Random.paragraph( min?, max? ) | |
Random.sentence( min?, max? ) | |
Random.word( min?, max? ) | |
Random.title( min?, max? ) | |
Random.cparagraph( min?, max? ) | |
Random.csentence( min?, max? ) | |
Random.cword( pool?, min?, max? ) | |
Random.ctitle( min?, max? ) | |
— name | |
Random.first() | |
Random.last() | |
Random.name( middle? ) | |
Random.cfirst() | |
Random.clast() | |
Random.cname() | |
— | |
Random.url() | |
Random.domain() | |
Random.protocol() | |
Random.tld() | |
Random.email() | |
Random.ip() | |
— | |
Random.region() | |
Random.province() | |
Random.city( prefix? ) | |
Random.county( prefix? ) | |
Random.zip() | |
— | |
Random.shuffle( arr ) | 数组乱序 |
Random.guid() | Mac地址 |
Random.id() | 随机长id |
Random.increment( step? ) | 定长步进 |
Mock.valid( template, data )
数据校验方法
Mock.toJSONSchema()
模板转化为JSON Schema 风格