Grammar specification learning of Mock.js

Mock.js has a complete set of grammatical specifications, you can study hard.
insert image description here

The grammar specification of Mock.js consists of two parts:

Data Template Definition (DTD)
Data Placeholder Definition (DPD)

Data Template Definition Specification DTD

Each attribute in the data template consists of 3 parts: attribute name, generation rule, and attribute value:

// Attribute name name
// Generation rule rule
// Attribute value value
'name|rule': value

Notice:

A vertical bar | is used to separate property names and generation rules.
Generation rules are optional.
There are 7 formats for generating 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
The meaning of the generation rule depends on the type of the attribute value to determine.
Attribute values ​​can contain @ placeholders.
The attribute value also specifies the initial value and type of the final value.

Build rules and examples:

1. The attribute value is a string String

'name|min-max': string
generates a string by repeating string, the number of repetitions is greater than or equal to min and less than or equal to max.

'name|count': string
Generates a string by repeating string equal to count.

2. The attribute value is a number Number

'name|+1':
The value of the number attribute is automatically increased by 1, and the initial value is number.

'name|min-max': number
generates an integer greater than or equal to min, less than or equal to max, the attribute value number is only used to determine the type.

'name|min-max.dmin-dmax': number
generates a floating-point number, the integer part is greater than or equal to min, less than or equal to max, and the decimal part retains bits from dmin to dmax.

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
}

3. The attribute value is Boolean

'name|1': boolean
randomly generates a Boolean value, the probability of being true is 1/2, and the probability of being false is also 1/2.

'name|min-max': value
randomly generates a Boolean value, the probability of being value is min / (min + max), and the probability of being !value is max / (min + max).

4. The property value is an Object

'name|count': object
randomly selects count attributes from the attribute value object.

'name|min-max': object
randomly selects min to max attributes from the attribute value object.

5. The property value is an Array

'name|1': array
Randomly select 1 element from the attribute value array as the final value.

'name|+1': array
Sequentially select 1 element from the attribute value array as the final value.

'name|min-max': array
generates a new array by repeating the attribute value array, the number of repetitions is greater than or equal to min and less than or equal to max.

'name|count': array
generates a new array by repeating the attribute value array, the number of repetitions is count.

6. The attribute value is a function

'name': function
executes the function function, takes its return value as the final attribute value, and the context of the function is the object where the attribute 'name' is located.

7. The attribute value is a regular expression RegExp

'name': regexp
generates a string matching it in reverse according to the regular expression regexp. Used to generate custom formatted strings.

Mock.mock({
‘regexp1’: /[a-z][A-Z][0-9]/,
‘regexp2’: /wWsSdD/,
‘regexp3’: /d{5,10}/
})
// =>
{
“regexp1”: “pJ7”,
“regexp2”: “F) p1G”,
“regexp3”: “561659409”
}

Data Placeholder Definition Specification DPD

Placeholders just take up a position in the attribute value string and do not appear in the final attribute value.

The format of the placeholder is:
@placeholder
@placeholder(parameter[,parameter])

Notice:

Use @ to identify the following string is a placeholder.
The placeholders refer to methods in Mock.Random.
Extend custom placeholders via Mock.Random.extend().
Placeholders can also refer to properties in the data template.
Placeholders will take precedence to refer to attributes in the data template.
Placeholders support relative and absolute paths.

Mock.mock({
name: {
first: ‘@FIRST’,
middle: ‘@FIRST’,
last: ‘@LAST’,
full: ‘@first @middle @last’
}
})

{
“name”: {
“first”: “Charles”,
“middle”: “Brenda”,
“last”: “Lopez”,
“full”: “Charles Brenda Lopez”
}
}

Supongo que te gusta

Origin blog.csdn.net/cuclife/article/details/130891006
Recomendado
Clasificación