-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
195 lines (155 loc) · 4.87 KB
/
index.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
*
The MIT License (MIT)
Copyright (c) 2010-2015 Allenice
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
* */
var fs = require('fs'),
path = require('path');
var faker = require('./lib/faker'),
fakerFuncNames = Object.keys(faker),
fakerFuncs = [],
_ = require('lodash'),
mkdirp = require('mkdirp');
fakerFuncNames.forEach(function(name) {
fakerFuncs.push(faker[name]);
});
// match {{ }}
var interpolateReg = /{{([\s\S]+?)}}/g;
// parsers of different data type
var parsers = {
// parse string template, if the parent template is an array, it will pass the index value to the child template
'_string': function(str, index) {
// replace {{ xxx }}
var obj = this
str = str.replace(interpolateReg, function(match, interpolate) {
try {
/*jslint evil: true */
var funcNames = ['','index'].concat(fakerFuncNames).concat(['return ' + interpolate + ';']),
func = new (Function.prototype.bind.apply(Function, funcNames)),
funcs = [function(){return index}].concat(fakerFuncs);
return func.apply(obj, funcs);
} catch(e) {
return e.message;
}
});
// if result is true or false, parse it to boolean
if(/^(true|false)$/.test(str)) {
str = str === 'true';
}
// if result is digit, parse it to float
if(/^[-+]?\d*\.?\d+$/.test(str)) {
str = parseFloat(str);
}
return str;
},
// parse object, it will generate each property
'_object': function(obj, index) {
var funcKey = [];
for(var key in obj) {
if(obj.hasOwnProperty(key)) {
// If this is a function, generate it later.
if(typeof obj[key] === 'function') {
funcKey.push(key);
continue;
}
obj[key] = generate.call(obj, obj[key], index);
}
}
// parse function
funcKey.forEach(function(key) {
obj[key] = generate.call(obj, obj[key], index);
});
return obj;
},
// parse array
'_array': function(arr) {
var repeatReg = /{{\s*repeat\((\d+),?\s*(\d*)?\)\s*}}/,
resultArray = [];
for(var i = 0; i < arr.length; i++) {
var item = arr[i];
// if item is string and has repeat method, repeat the next item to result array
if(typeof item === 'string' && repeatReg.test(item)) {
var nextItem = arr[++i],
min = parseInt(RegExp.$1) || 0,
max = parseInt(RegExp.$2),
length;
length = isNaN(max) ? min : _.random(min, max);
if(nextItem) {
for(var j = 0; j < length; j++) {
// parse the next item and pass the index value
resultArray.push(generate(_.clone(nextItem), j));
}
}
} else {
resultArray.push(generate(item));
}
}
return resultArray;
},
// parse function
'_function': function(func, index) {
return func.call(this, faker, index);
},
// get parser according to the data type
getParser: function(template) {
if(_.isArray(template)) return this._array;
return this['_' + (typeof template)];
}
};
/**
* generate
* @param template - json scheme
* @param [index] - array index
* @returns {*}
*/
function generate(template, index) {
var data,
parser = parsers.getParser(template);
if(typeof parser === 'function') {
data = parser.call(this, template, index);
} else {
data = template;
}
return data;
}
/**
* save as json file
* @param template - json scheme
* @param path - path to save file
*/
function save(template, distpath) {
var data = generate(template);
var dir = path.dirname(distpath);
mkdirp(dir, function(err) {
if(err) {
console.log(err.message);
return;
}
fs.writeFile(distpath, JSON.stringify(data, null, 2), function(err) {
if(err) {
console.log(err.message);
return;
}
});
});
}
module.exports = {
faker: faker,
generate: generate,
save: save
};