-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathgenerator.js
45 lines (40 loc) · 1.18 KB
/
generator.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
const level3 = require('./data/level3.json');
const { getRandom } = require('./utils');
const checkSub = function(dict) {
return dict.regionEntitys && dict.regionEntitys instanceof Array && dict.regionEntitys.length > 0
}
// 生成一级地址
function generateLevel1() {
const province = getRandom(level3)
return {
region: province.region,
code: province.code
}
}
// 生成二级地址
function generateLevel2() {
const dict = level3.filter(i => checkSub(i));
const province = getRandom(dict);
const city = getRandom(province.regionEntitys);
return {
region: province.region + city.region,
code: city.code
};
}
// 生成三级地址
function generateLevel3() {
const dict = level3.filter(i => checkSub(i));
const province = getRandom(dict);
const city = getRandom(province.regionEntitys);
if(!city.regionEntitys) {
return generateLevel3(); // 如果没有三级区域。则迭代
}
const region = getRandom(city.regionEntitys);
return {
region: province.region + city.region + region.region,
code: region.code
};
}
exports.generateLevel1 = generateLevel1;
exports.generateLevel2 = generateLevel2;
exports.generateLevel3 = generateLevel3;