数据占位符定义规范DPD
Mock.Random 是一个工具类,用于生成各种随机数据。
Mock.Random 的方法在数据模板中称为『占位符』,书写格式为 @占位符(参数 [, 参 数]) 。
一、 内置方法列表
Type | Methord |
---|---|
Basic | boolean, natural, integer, float, character, string, range, date, time, datetime, now |
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 |
一、 基本方法
可以随机生成的基本数据类型
- string 字符串
- Integer 整数
- date 日期
1. 随机模拟上面的三种类型数据
var data = Mock.mock({
'list|6': [{
'id|+1': 1,
'name': '@string',
'point': '@integer',
'birthday': '@date',
}]
})
console.log(JSON.stringify(data, null, 4)); //
{
"list": [
{
"id": 1,
"name": "RI2a^",
"point": -6630762082626940,
"birthday": "1998-08-06"
},
{
"id": 2,
"name": "XY#zss",
"point": 6231647203686884,
"birthday": "1985-11-16"
},
{
"id": 3,
"name": "FCgV",
"point": -6835140496971368,
"birthday": "1980-09-08"
},
{
"id": 4,
"name": "fR3k6^",
"point": 8528829814768156,
"birthday": "1982-09-23"
},
{
"id": 5,
"name": "i#FV*J",
"point": -6649086826211580,
"birthday": "2002-11-06"
},
{
"id": 6,
"name": "H!6w",
"point": -3541546718675356,
"birthday": "1995-12-30"
}
]
}
2. 随机生成图像
image 随机生成图片地址
var data = Mock.mock({
'list|3': [{
'id|+1': 1,
'name': '@string',
'point': '@integer',
'birthday': '@date',
'pic': '@image',
}]
})
console.log(JSON.stringify(data, null, 4)); //
{
"list": [
{
"id": 1,
"name": "f@ECMd",
"point": 7215953988793520,
"birthday": "2010-01-15",
"pic": "http://dummyimage.com/120x600"
},
{
"id": 2,
"name": "oA^dwfP",
"point": 6154493426468412,
"birthday": "1984-12-30",
"pic": "http://dummyimage.com/120x90"
},
{
"id": 3,
"name": "J94O",
"point": -8837096452760392,
"birthday": "1971-07-11",
"pic": "http://dummyimage.com/240x400"
}
]
}
3. 文本方法
@title: 标题
@cword(100) :文本内容参数为字数
var data = Mock.mock({
'list|3': [{
'id|+1': 1,
'name': '@string',
'point': '@integer',
'birthday': '@date',
'pic': '@image',
'title': '@title', // 生成标题
'content': '@cword(100)', // 文本内容
}]
})
console.log(JSON.stringify(data, null, 4)); //
{
"list": [
{
"id": 1,
"name": "*Ej*",
"point": 6817599281171608,
"birthday": "2016-12-07",
"pic": "http://dummyimage.com/120x90",
"title": "Rshqnrltwq Sbiikrtc Wxwubau Phtxu Ikflfknr Lqotyu",
"content": "研织量我主知京一命数低又人究设局院才步性战解却放照及很代论广真通证利政极权将法思革点办适书局开精阶元九节铁分维且界果多济太难必土段然区学则活至图委作话特及周即收明议定强数员离一复改商会况般因际酸省八每"
},
{
"id": 2,
"name": "eDw]Pdn",
"point": -5950339924516028,
"birthday": "1974-08-01",
"pic": "http://dummyimage.com/120x600",
"title": "Eboczmare Seimq Xwfdhm See Rkpytpnlfm Nxe Reep",
"content": "号边增听济率应先并到观角济并手听团也维角由几行没率一强何严明确想见系边元还商对见年派总必外件程从成选选济点集流积目等以机话你至生素身己即反所报比老半造飞那张成己器受毛名线声十发将本族儿设看适有斗记先按"
},
{
"id": 3,
"name": "MJinXSm",
"point": -3366934644351828,
"birthday": "2018-12-29",
"pic": "http://dummyimage.com/120x240",
"title": "Mqf Xpoesx Jtzoxv",
"content": "定毛名象无性定个力已许须导领科发族识联共好维便于第华件件确却历相头指位意别象持连细儿议导明式县水内到非称管元起料再论问界国工打电适八四车政代进目先了天军得拉满连计眼六斗条有商近带步气声划理电按型具领权"
}
]
}
4. 名称方法
cname :中文名称
cfirst:中文姓氏
Last:英文姓氏
var data = Mock.mock({
'list|3': [{
'id|+1': 1,
'name': '@cname', // 中文名称
'ename': '@last', // 英文形式
'cfirst': '@cfirst', // 中文姓
'point': '@integer',
'birthday': '@date',
'pic': '@image',
'title': '@title', // 生成标题
'content': '@cword(100)', // 句子,以及句子的长度的使用
}]
})
console.log(JSON.stringify(data, null, 4)); //
{
"list": [
{
"id": 1,
"name": "曹静",
"ename": "White",
"cfirst": "袁",
"point": 884405172233340,
"birthday": "1980-08-23",
"pic": "http://dummyimage.com/240x400",
"title": "Apcz Bifii Xlw Ekmhnby Nitmgpeyt",
"content": "志元开小据较响议细正且已利组音律前度造京线元天位土连亲少到市来斯队农马界应委委况然头想法传引将群公九农领只确这当正层但议与至大过道备利几严速律记张根理包什究领权我育相才马每极为马取结需响装积每已员连支"
},
{
"id": 2,
"name": "周秀兰",
"ename": "Perez",
"cfirst": "潘",
"point": 673304620398428,
"birthday": "2010-09-22",
"pic": "http://dummyimage.com/234x60",
"title": "Eekggmhr Itxwmdmp Hqpqlotrlv Siclynnxr Grmbdyc",
"content": "矿光张影外为写许十争权快亲成权给却马重义白因门果复响最使老便点直强由间除别重花证话世也为革重打京也教应治应应西保内心据到需题感因电复位在少石干极处电外元利场党如本其飞资指采的时养本是院易要到格美争着红"
},
{
"id": 3,
"name": "卢娟",
"ename": "Moore",
"cfirst": "段",
"point": 4838996581783764,
"birthday": "1977-08-14",
"pic": "http://dummyimage.com/336x280",
"title": "Obpswemvn Rtxc Iqxuq Punwfrp Ytothbe",
"content": "术表解片结音向手量列可据研给积矿族式打确别革历由很对越了机需计但为动圆院展律如县王只条话他况政集候真以局或织斗建维况斯都合看想列信事那战划外走结素传次阶建十信红组规物只西过求都命体节院素品政放机多每东"
}
]
}
5. 网络方法
可以生成url ip email等网络相关信息
var data = Mock.mock({
'list|3': [{
'id|+1': 1,
'name': '@cname', // 中文名称
'ename': '@last', // 英文形式
'cfirst': '@cfirst', // 中文姓
'point': '@integer',
'birthday': '@date',
'pic': '@image',
'title': '@title', // 生成标题
'content': '@cword(100)', // 句子,以及句子的长度的使用
'url': '@url', /// 随机生成url
'ip': '@ip', /// 随机生成ip
'email': '@email' // 随机生成email
}]
})
console.log(JSON.stringify(data, null, 4)); //
{
"list": [
{
"id": 1,
"name": "梁超",
"ename": "Martin",
"cfirst": "高",
"point": -8680722381760872,
"birthday": "1980-09-20",
"pic": "http://dummyimage.com/720x300",
"title": "Kexbhkamy Wsspwnbg Hkkpbmtqj Dmkpijcmz Rweft",
"content": "平知八至提干而用共想其性对等真员马共红想问连积看转研代文运斯体整地造方温变严至共放想断从品片处斯反铁文着米划于火给切我段率可集同步局带放接织该花水造一林指真们书引来一给时每方样治能或格社设达备劳边将准",
"url": "mid://ktwl.my/knsbz",
"ip": "198.194.221.46",
"email": "[email protected]"
},
{
"id": 2,
"name": "萧杰",
"ename": "Perez",
"cfirst": "范",
"point": -8886945658975056,
"birthday": "2000-04-10",
"pic": "http://dummyimage.com/125x125",
"title": "Vglpj Ubwnb Crlfg Ptaq Icxofnqd Mgkj Gtot",
"content": "在拉段何究商代全形千较书光究没三统名油用般它完音有备色转志料要热权它话安增到拉全他而部做各体整口便点照众立各风之也史阶力素值党心商术林况质别过深格多给员验子太天事山干今列完支龙精来没往生路空列先利做易",
"url": "ftp://lxre.pn/nmrjsqd",
"ip": "59.239.120.251",
"email": "[email protected]"
},
{
"id": 3,
"name": "乔杰",
"ename": "Thompson",
"cfirst": "方",
"point": 2459679842103796,
"birthday": "1979-05-18",
"pic": "http://dummyimage.com/88x31",
"title": "Cljev Mjtxknpqhc Uvtnque Upywjkr Hunoomsxxi Mdd",
"content": "次可准好省积四边争定着日权装备米力图西少委原从组花收级通求图论无更集你米强没计温则基听元采利以是科广素专战老日务难快便名低劳复太过组气细身儿快积结府件交定空史界政系青约白人验有具果深政严置族确准再使说",
"url": "cid://bve.nr/eywfoi",
"ip": "157.27.34.229",
"email": "[email protected]"
}
]
}
6. 地址方法
@region 区域
@county 省市县
var data = Mock.mock({
'list|3': [{
'id|+1': 1,
'name': '@cname', // 中文名称
'ename': '@last', // 英文形式
'cfirst': '@cfirst', // 中文姓
'point': '@integer',
'birthday': '@date',
'pic': '@image',
'title': '@title', // 生成标题
'content': '@cword(100)', // 句子,以及句子的长度的使用
'url': '@url', /// 随机生成url
'ip': '@ip', /// 随机生成ip
'email': '@email', // 随机生成email
'area': '@region', // 随机生成地区
'address': '@county(true)', // 随机生成省市区
}]
})
console.log(JSON.stringify(data, null, 4)); //
{
"list": [
{
"id": 1,
"name": "周杰",
"ename": "Perez",
"cfirst": "徐",
"point": 6682726732685588,
"birthday": "1979-11-03",
"pic": "http://dummyimage.com/120x90",
"title": "Yzr Knt Ykkr",
"content": "较维正要才于件县受法知己史度况问命什叫提外直量太观这层干看气管下山价义万表程为世利目也得必省参容内比单学设厂易起力按完难线般周其关门的物到装铁整运际体查几热切包场原而将深具思层层而联电展风状层市改市给",
"url": "mid://wfpwpxvmwm.gd/edxq",
"ip": "110.213.132.134",
"email": "[email protected]",
"area": "华南",
"address": "山东省 菏泽市 鄄城县"
},
{
"id": 2,
"name": "唐丽",
"ename": "Taylor",
"cfirst": "黎",
"point": -6846526650793976,
"birthday": "2017-04-14",
"pic": "http://dummyimage.com/120x90",
"title": "Gicplf Sefxrxs Wxgpdpvtx Yupf Qceu Vudxirte",
"content": "意派连半可天响原目大次数合而华江县引领般与准安性员维代报路县展者还验办算政个东导本门南日又历长眼本教产民东民转院周县己难历龙在被片易接委实专知则她质要整工物便连照油量六在般不布成住天意导天动委术好装物",
"url": "http://eyyw.biz/oibutxex",
"ip": "133.139.218.152",
"email": "[email protected]",
"area": "华北",
"address": "黑龙江省 齐齐哈尔市 甘南县"
},
{
"id": 3,
"name": "方伟",
"ename": "Walker",
"cfirst": "易",
"point": 1875229080272072,
"birthday": "1998-01-13",
"pic": "http://dummyimage.com/728x90",
"title": "Cexvst Kenbu Anija Jlmdjhixw Rfflxbk Jgde Inpbwgxy",
"content": "在种族史导圆我林立接队约业局积设矿队干根更山场书集空少利成声别个政着它时代王非规厂大分手离据也国前西西实儿美离数没质两象属铁装照局织结现保正光完易通此转市得话每从求与装想命它织机决小具出管为期各济今土",
"url": "telnet://wceku.pt/lvuvx",
"ip": "174.46.35.133",
"email": "[email protected]",
"area": "华南",
"address": "辽宁省 鞍山市 铁西区"
}
]
}