mock的使名用一(生成隨機數據)

Mock.Random 是一個工具類,用於生成各類隨機數據。javascript

Mock.Random 的方法在數據模板中稱爲『佔位符』,書寫格式爲 @佔位符(參數 [, 參數]) 。java

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" }

  

注意git

方法

Mock.Random 提供的完整方法(佔位符)以下:github

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 擴展方法,而後在數據模板中經過 @擴展方法 引用。例如:api

Random.extend({
    constellation: function(date) {
        var constellations = ['白羊座', '金牛座', '雙子座', '巨蟹座', '獅子座', '處女座', '天秤座', '天蠍座', '射手座', '摩羯座', '水瓶座', '雙魚座']
        return this.pick(constellations)
    }
})
Random.constellation()
// => "水瓶座"
Mock.mock('@CONSTELLATION')
// => "天蠍座"
Mock.mock({
    constellation: '@CONSTELLATION'
})
// => { constellation: "射手座" }
相關文章
相關標籤/搜索