Skip to content

Mock.Random

mozhi.gyy edited this page Dec 15, 2015 · 7 revisions

Mock.Random 是一个工具类,用于生成各种随机数据。

Mock.Random 的方法在数据模板中称为『占位符』,书写格式为 @占位符(参数 [, 参数])

var Random = Mock.Random
Random.email()
// => "n.clark@miller.io"
Mock.mock('@email')
// => "y.lee@lewis.org"
Mock.mock( { email: '@email' } )
// => { email: "v.lewis@hall.gov" }

注意

方法

Mock.Random 提供的完整方法(占位符)如下:

Type Method
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
<script id="fixPlaceholderLink" type="text/javascript"> $('#fixPlaceholderLink').prev('table') .find('td:nth-child(1)').each(function(index, td) { $(td).contents().wrapAll( $('').attr('href', '#' + $(td).text()) ) }) .end() .find('td:nth-child(2)').each(function(index, td) { var methods = $(td).text().split(' ') var links = $() $(methods).each(function(mindex, m) { links.push( $('').attr('href', '#' + m).text(m)[0] ) if (mindex < methods.length - 1) { links.push( $('').text(', ')[0] ) } }) $(td).empty().append(links) }) .end() </script>

扩展

Mock.Random 中的方法与数据模板的 @占位符 一一对应,在需要时还可以为 Mock.Random 扩展方法,然后在数据模板中通过 @扩展方法 引用。例如:

Random.extend({
    constellation: function(date) {
        var constellations = ['白羊座', '金牛座', '双子座', '巨蟹座', '狮子座', '处女座', '天秤座', '天蝎座', '射手座', '摩羯座', '水瓶座', '双鱼座']
        return this.pick(constellations)
    }
})
Random.constellation()
// => "水瓶座"
Mock.mock('@CONSTELLATION')
// => "天蝎座"
Mock.mock({
    constellation: '@CONSTELLATION'
})
// => { constellation: "射手座" }
Clone this wiki locally