Usage of Mock

Installation

1
yarn add mockjs

Simple Usage

1
2
3
4
5
6
7
const Mock = require('mock')
const data = Mock.mock({
'list|1-10': [{
'id|+1': 1
}]
})
console.log(JSON.stringify(data))

Data Template Definition(DTD)

Each attribute consists of three parts:

1
2
3
4
5
// attribute name
// attribute rule
// attribute value

'name|rule': value

rule is optional

1
2
3
4
5
6
7
8
9
10
// rules
{
'name|min-max': value,
'name|count': value,
'name|min-max.dmin-dmax': value,
'name|min-max.dcount': value,
'name|count.dmin-dmax': value,
'name|count.dcount': value,
'name|+step': value,
}
1
2
3
4
5
'name|min-max': string // repeat 'string' less than or equal to 'min', more than or equal 'max'
'name|count': string // repeat 'string' 'count' times.
'name|+1': 0, // auto incresing with step 1 from 0
'name|min-max': number // generate a number >=min and <=max
'name|min-max.dmin-dmax': number // generate a float, the integer part >=min and <=max, the deciaml part remains dmin - dmax bits.
1
2
3
4
5
6
7
8
9
10
11
12
13
Mock.mock({
'number1|1-100.1-10': 1,
'number2|123.1-10': 1,
'number3|123.3': 1,
'number4|123.10': 1.123
})
// =>
{
"number1": 12.92,
"number2": 123.51,
"number3": 123.777,
"number4": 123.1231091814
}

Data Placeholder Definition(DPD)

Placeholder hold a position in attribute string, and will dismiss in result.

Use Mock.Random to generate data.

1
2
@placeholder
@placeholder(opt1[, opt2])
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Mock.mock({
name: {
first: '@FIRST',
middle: '@FIRST',
last: '@LAST',
full: '@first @middle @last'
}
})
// =>
{
"name": {
"first": "Charles",
"middle": "Brenda",
"last": "Lopez",
"full": "Charles Brenda Lopez"
}
}

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
2
3
{
timeout: ''
}
  • timeout: set time responding time, ‘300’, ‘300-600’, default to ‘10-100’
1
2
3
Mock.setup({
timeout: '200-400',
})

Mock.Random

Utils to generate Random data.

1
2
3
4
5
6
7
8
var Random = Mock.Random
Random.email()

Mock.mock('@email') // use with placeholder

Mock.mock({
email: '@email'
})

| 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
2
3
4
5
6
{
lower: 'abcdefghijklmnopqrstuvwxyz',
upper: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
number: 0123456789,
symbol: '!@#$%^&*()[]'
}

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

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×