Mock.js数据生成器4_数据占位符定义规范DPD

Mock.Random 是一个工具类,用于生成各种随机数据,使得数据更加的真实。

Mock.Random 类中的方法在数据模板中称为『占位符』,书写格式为 '属性名': @占位符

Type(类型)

Method(占位符)

Basic(基本类型)

boolean , natural (自然数,大于等于 0 的整数), integer , float , character , string ,

range (整型数组),

Date(日期)

date (年月日), time (时分秒), datetime (年月日时分秒)

Image(图片)

image, dataImage

Color

color

Text(文本)

paragraph, sentence, word, title(标题), cparagraph, csentence(内容), cword, ctitle

Name(姓名)

first(英文姓), last(英文名), name(英文姓名), cfirst(中), clast, cname

Web

url, domain, email, ip, tld

Address(地址)

area, region

Helper(不常用)

capitalize, upper, lower, pick, shuffle

Miscellaneous(不常用)

guid, id

 

练习代码:

const Mock=require('mockjs')

const data=Mock.mock({
  'empList|6':[
    {
      'id|+1':1,
      'name': '@cname', //中文名称
      'price':'@float',
      'status':'@boolean',
      'birthday':'@date("yyyy/MM/dd")',
      'createDate':'@datetime("yyyy/MM/dd HH-mm-ss" )',
      'pic':'@image',
      'title':'@title',
      'ctitle':'@ctitle(10,16)',
      'content':'@csentence(100,200)',
      'first':'@cfirst',  //姓
      'last':'@clast',  //名
      'url':'@url("https","cc.com")',
      'domain':'@domain',
      'ip':'@ip',
      'area':'@region',
      'address':'@county(true)',
      'zip':'@zip',
      'phone|11':'@integer(0,9)' //数据模板和占位符结合使用
    }
  ]
})

console.log(JSON.stringify(data,null,2))

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值