Installation
1 | yarn add mockjs |
Simple Usage
1 | const Mock = require('mock') |
Data Template Definition(DTD)
Each attribute consists of three parts:
1 | // attribute name |
rule is optional
1 | // rules |
1 | 'name|min-max': string // repeat 'string' less than or equal to 'min', more than or equal 'max' |
1 | Mock.mock({ |
Data Placeholder Definition(DPD)
Placeholder hold a position in attribute string, and will dismiss in result.
Use Mock.Random to generate data.
1 | @placeholder |
1 | Mock.mock({ |
Mock.mock()
Mock.mock(rurl?, rtype?, template | function(options))
Mock.mock(template)
Generate mock data
Mock.mock(rurl, template)
Intercept rurl
(regexp) request, and generate according template.
Mock.mock(rurl, function(options))
Intercept rurl
request and call function(options) to return the mock data.
Mock.mock(rurl, rtype, template)
Intercept the request matching rurl
and rtype
.
Mock.mock(rurl, rtype, function(options))
Intercept the request matching rurl
and rtype
.
options conssits of (url, type, body)
Mock.setup()
Mock.setup(settings)
Set action when intercept ajax request.
1 | { |
- timeout: set time responding time, ‘300’, ‘300-600’, default to ‘10-100’
1 | Mock.setup({ |
Mock.Random
Utils to generate Random data.
1 | var Random = Mock.Random |
| Type | Mehtod |
| Baisc | 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 |
Mock.Random.boolean(min?, max?, current?)
‘min’ and ‘max’ decide the possibility of ‘current’
‘current’ will appear in possibility of min/(min + max) to max(min + max)
Mock.Random.natural(min?, max?)
Mock.Random.integer(min?, max?)
Mock.Random.float(min?, max?, dmin?, dmax?)
Mock.Random.character(pool?)
pool represents the character pool
pool: ‘lower/upper/number/symbol’
1 | { |
Mock.Random.string(pool?, min?, max?)
pool: available string pool
min: min-length, default to 3
max: max-length, default to 7
Mock.Random.range(start?, stop, step?)
Mock.Random.date(format?)
Mock.Random.time(format?)
Mock.Random.datetime(format?)
Mock.Random.now(unit?, format?)
Mock.Random.image(size?, background?, foreground?, format?, text?)
Mock.Random.dataImage(size?, text?)
Mock.Random.color()
Mock.Random.hex()
Mock.Random.rgb()
Mock.Random.rbga()
Mock.Random.hsl()
Mock.Random.paragraph(null | length | min, max) | Mock.Random.cparagraph(null | length | min, max)
Mock.Random.sentence(null | length | min, max) | Mock.Random.csentence(null | length | min, max)
Mock.Random.workd(null | length | min, max) | Mock.Random.cword(pool?, min?, max?)
Mock.Random.title(null | length | min, max) | Mock.Random.ctitle(null | length | min, max)
Mock.Random.first() | Mock.Random.last() | Mock.Random.name(middle?) | Mock.Random.cfirst() | Mock.Random.clast() | Mock.Random.cname()
Mock.Random.url(protocol?, host?)
Mock.Random.protocol() | Mock.Random.domain() | Mock.Random.tld() | Mock.Random.email(domain?) | Mock.Random.ip()
Mock.Random.region() | Mock.Random.province() | Mock.Random.city(prefix?) | Mock.Random.county(prefix?) | Mock.Random.zip()
Mock.Random.captilize(word) | Mock.Random.upper(str) | Mock.Random.lower(str) | Mock.Random.pick(arr) | Mock.Random.shuffle(arr)
Mock.Random.guid()
Generate a GUID
Mock.Random.id()
Generate a Personal Identity
Mock.Random.increment(step?)
Mock.valid(template, data)
Check if the data is matching the template