UNPKG

2.81 kBJavaScriptView Raw
1// 内部方法
2const dataParse = require("./http/parse/data");
3const getChar = require("./util/char").getChar;
4const isProcesser = require("./util/processer").isProcessor;
5const faker = require("faker");
6
7faker.locale = "zh_CN";
8
9module.exports = {
10 string: function() {
11 return "我是测试的string对象";
12 },
13
14 number: function() {
15 const MAX = 100;
16 const MIN = 1;
17 return Math.floor(Math.random() * (MAX - MIN) + MIN);
18 },
19
20 id: function() {
21 return faker.finance.account();
22 },
23
24 enum: function(params) {
25 let payload = params;
26 if (Array.isArray(payload)) {
27 let data = payload[Math.floor(Math.random() * payload.length)];
28 if (isProcesser(data) && this[getChar(data)]) {
29 data = this[getChar(data)]();
30 }
31 return data;
32 } else {
33 console.error(`⚠️⚠️⚠️ params 格式有问题哦`);
34 return payload;
35 }
36 },
37
38 list: function(params) {
39 let payload = params.payload;
40 let number = params.number || 1;
41 let result = [];
42 let data = payload;
43 for (let i = 0; i < number; i++) {
44 if (typeof data === "object" && !Array.isArray(data)) {
45 result.push(dataParse(data, this));
46 } else {
47 if (isProcesser(data)) {
48 data = this[getChar(data)]();
49 }
50 result.push(data);
51 }
52 }
53 return result;
54 },
55
56 province: function() {
57 return faker.address.state();
58 },
59
60 price: function() {
61 return faker.commerce.price();
62 },
63
64 weekday: function() {
65 return faker.date.weekday();
66 },
67
68 month: function() {
69 return faker.date.month();
70 },
71
72 recentDate: function() {
73 return faker.date.recent();
74 },
75
76 futureDate: function() {
77 return faker.date.future();
78 },
79
80 pastDate: function() {
81 return faker.date.past();
82 },
83
84 avatar: function() {
85 return faker.image.avatar();
86 },
87
88 animals: function() {
89 return faker.image.animals();
90 },
91
92 food: function() {
93 return faker.image.food();
94 },
95
96 people: function() {
97 return faker.image.people();
98 },
99
100 sentences: function() {
101 return faker.lorem.sentences();
102 },
103
104 lines: function() {
105 return faker.lorem.lines();
106 },
107
108 name: function() {
109 return `${faker.name.firstName()}${faker.name.lastName()}`;
110 },
111
112 phoneNumber: function() {
113 let prefixArray = new Array(
114 "130",
115 "131",
116 "132",
117 "133",
118 "135",
119 "137",
120 "138",
121 "156",
122 "159",
123 "170",
124 "187",
125 "189"
126 );
127
128 let i = parseInt(10 * Math.random());
129
130 let prefix = prefixArray[i];
131
132 for (let j = 0; j < 8; j++) {
133 prefix = prefix + Math.floor(Math.random() * 10);
134 }
135
136 return prefix;
137 },
138
139 boolean: function() {
140 return faker.random.boolean();
141 },
142
143 uuid: function() {
144 return faker.random.uuid();
145 }
146};