-
Notifications
You must be signed in to change notification settings - Fork 0
/
mock.js
137 lines (133 loc) · 4.08 KB
/
mock.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//get file data
var fs = require('fs');
var arg = process.argv;
var ChineseCharacters = '百度移动云搜索钱包开疆拓土';
ChineseCharacters = ChineseCharacters.split('');
var AsciaCharacters = 'abcdefghigklmnopqrstuvwsyzABCDEFGHIGKLMNOPQRSTUVWXYZ';
AsciaCharacters = AsciaCharacters.split('');
var numberCharacters = '0123456789';
numberCharacters = numberCharacters.split('');
if(arg.length !==4){
console.log('usage: node mock.js filePath resultFilePath');
return -1;
}
function isArray(obj){
return Object.prototype.toString.call(obj) === "[object Array]" ?true: false;
}
function isObject(obj){
return (Object.prototype.toString.call(obj) === "[object Object]")? true : false;
}
var filePath = process.argv[2];
var resultFilePath = process.argv[3];
//var dirName = process.argv.[1]
var mockFileContent = fs.readFileSync(filePath,{encoding:'utf8'});
//parse as json
var jsonContent = JSON.parse(mockFileContent);
//recrusive parse object
function mock(obj){
for(var i in obj){
if( obj.hasOwnProperty(i) ){
if( isObject(obj[i] ) ){
mock(obj[i]);
}else{
generateData(obj,[i],obj[i]);
}
}
}
}
function writeJsonToFile(jsonData,fileName){
var jsonData = JSON.stringify(jsonData,null,4);
fs.writeFileSync(fileName,jsonData);
}
function generateData(context,arr,patterns){
if(typeof patterns == 'number'){
context[arr] = patterns;
return 0;
}
var actionMap = {
'a' : generateStr,
'1' : generateNum,
'一': generateChineseCharacter,
'[]': generateArr,
'{}': generateObj
};
var pattern ;
isArray(arr)?arr:arr=[arr];
patterns = !isArray(patterns)?patterns.split('|'):patterns;
pattern = patterns.shift();
pattern = pattern.split(',');
var p,type,property,ps;
if( arr.length !== pattern.length){
console.log(arr , 'not match ',pattern);
return -1;
}
for(var i = 0 ,len = arr.length ;i<len ;i++){
p = pattern[i];
type = p.split('@')[0];
property = p.split('@')[1];
if(actionMap[type]){
actionMap[type](context,arr[i],property,patterns);
}else{
context[arr[i]] = type;
}
}
}
function generateChineseCharacter(context,p,ccPattern){
context[p] = generateCharacter(ccPattern,ChineseCharacters);
}
function generateCharacter(pattern,characterArr){
var rangePattern = /\[(\d+)\s*-(\d+)\]/;
var rangeLeftLimit ,rangeRightLimit,match;
if(rangePattern.test(pattern)){
match = pattern.match(rangePattern);
rangeLeftLimit = parseInt(match[1]||1);
rangeRightLimit = parseInt(match[2]||10);
}
rangeLeftLimit?true: rangeLeftLimit = parseInt(pattern);
rangeRightLimit?true: rangeRightLimit= rangeLeftLimit;
var character=null;
var result = [];
var len =rangeLeftLimit+Math.random()*(rangeRightLimit - rangeLeftLimit) || 1;
var clen = characterArr.length;
for(var i = 0 ;i<len; i++){
character = characterArr[parseInt((Math.random()*clen))] || 'a';
result.push(character);
}
return result.join('');
};
function generateNum(context,p,numPattern){
var rangePattern = /\[(\d+)\s*-(\d+)\]/;
var rangeLeftLimit ,rangeRightLimit,match;
if(rangePattern.test(numPattern)){
match = numPattern.match(rangePattern);
rangeLeftLimit = parseInt(match[1]||1);
rangeRightLimit = parseInt(match[2]||10);
context[p] = parseInt( Math.random()*(rangeRightLimit - rangeLeftLimit)) + rangeLeftLimit;
}else{
context[p] = '-1';
}
}
function generateStr(context,p,strPattern){
context[p] = generateCharacter(strPattern,AsciaCharacters);
}
function generateArr(context,p,attibute,patterns){
context[ p ] = [];
var ps = [];
for(var j = 0 ;j<parseInt(attibute);j++){
context[ p ].push(0);
ps.push(j);
generateData(context[p],[j],patterns.slice(0));
}
}
function generateObj(context,p,attibute,patterns){
context [ p ] = {};
var objProperty = attibute.split(':');
ps = [];
for(var j = 0 ;j<objProperty.length;j++){
context[ p ][objProperty[j]] = null;
ps.push(objProperty[j]);
}
generateData(context[p],ps,patterns);
}
mock(jsonContent);
writeJsonToFile(jsonContent,resultFilePath);