exp-mock
v2.0.6
Published
Parse expressions, generate mock data.
Downloads
759
Readme
exp-mock
Parse expressions and generate mock data.
This library mainly contains three functions.
DEMO
const {mockExp, parseExp, getFakerObject} = require('exp-mock');
_.forEach([
'{{$mockjs.telephone|padStart(0,text)}}',
`{{'123aBcD'|upper|length}}`,
`{{123|concat(dsadsad)|upper|length}}`,
`{{$fakerjs.word.adjective(min=3,max=7)|concat(text)}}`,
`{{name|md5|sha1}}`
], (item) => {
console.log(item,'==>',mockExp(item, { name: "John" },'ja'))
});
Localization support
The third parameter specifies the localization language pack. The currently supported languages include:
- zh-ch: Simplified Chinese
- zh-tw: Traditional Chinese
- en: English
- ja: Japanese
- de: German
- fr: French
- id: Indonesian
parseExp
This function converts a string like {{$mockjs.telephone(1,100)|padStart(0,text)}}
into an object, making it easier to render and edit in the UI. Here are some examples:
{{$mockjs.telephone(1,100)|padStart(0,text)}} ==> {
"dist": {
"type": "mockjs",
"expression": "@telephone(1,100)",
"value": "15831593361"
},
"functions": [
{
"function": "padStart",
"paras": {
"length": 0,
"text": "text"
},
"tmpValue": "15831593361"
}
]
}
{{'123aBcD'|upper|length}} ==> {
"dist": {
"type": "fixed",
"expression": "123aBcD",
"value": 7
},
"functions": [
{
"function": "upper",
"paras": {},
"tmpValue": "123ABCD"
},
{
"function": "length",
"paras": {},
"tmpValue": 7
}
]
}
{{123|concat(dsadsad)|upper|length}} ==> {
"dist": {
"type": "fixed",
"expression": "123",
"value": 10
},
"functions": [
{
"function": "concat",
"paras": {
"text": "dsadsad"
},
"tmpValue": "123dsadsad"
},
{
"function": "upper",
"paras": {},
"tmpValue": "123DSADSAD"
},
{
"function": "length",
"paras": {},
"tmpValue": 10
}
]
}
{{$fakerjs.word.adjective(min=3,max=7)|concat(text)}} ==> {
"dist": {
"type": "fakerjs",
"expression": {
"module": "Word",
"function": "adjective",
"paras": {
"length": {
"min": 3,
"max": 7
}
}
},
"value": "subtletext"
},
"functions": [
{
"function": "concat",
"paras": {
"text": "text"
},
"tmpValue": "subtletext"
}
]
}
{{name|md5|sha1|substr(3,2)}} ==> {
"dist": {
"type": "variable",
"expression": "name",
"value": "01"
},
"functions": [
{
"function": "md5",
"paras": {},
"tmpValue": "5e027396789a18c37aeda616e3d7991b"
},
{
"function": "sha1",
"paras": {},
"tmpValue": "b560159b4838f42343708c62df126fe79ed35552"
},
{
"function": "substr",
"paras": {
"start": 3,
"length": 2
},
"tmpValue": "01"
}
]
}
mockExp
This function generates preview values from strings like {{$mockjs.telephone(1,100)|padStart(0,text)}}
. Here are some examples:
{{$mockjs.telephone(1,100)|padStart(0,text)}} ==> 17230215009
{{'123aBcD'|upper|length}} ==> 7
{{123|concat(dsadsad)|upper|length}} ==> 10
{{$fakerjs.word.adjective(min=3,max=7)|concat(text)}} ==> genuinetext
{{name|md5|sha1}} ==> b560159b4838f42343708c62df126fe79ed35552
getFakerObject
This function returns an object made up of modules and methods from Faker.js. Here’s an example:
[
{
"module": "Airline",
"function": "aircraftType",
"description": {
"zh-CN": "返回一种随机的飞机类型。",
"zh-TW": "返回一種隨機的飛機類型。",
"en": "Returns a random aircraft type.",
"ja": "ランダムな航空機の種類を返します。",
"id": "Mengembalikan jenis pesawat secara acak."
},
"params": {}
},
{
"module": "Airline",
"function": "airline",
"description": {
"zh-CN": "生成一家随机的航空公司。",
"zh-TW": "生成一家隨機的航空公司。",
"en": "Generates a random airline.",
"ja": "ランダムな航空会社を生成します。",
"id": "Menghasilkan maskapai acak."
},
"params": {}
}
]