UNPKG

11.8 kBJavaScriptView Raw
1var random_ua = require('../vendor/user-agent');
2
3/**
4 *
5 * @namespace faker.internet
6 */
7var Internet = function (faker) {
8 var self = this;
9 /**
10 * avatar
11 *
12 * @method faker.internet.avatar
13 */
14 self.avatar = function () {
15 return 'https://cdn.fakercloud.com/avatars/' + faker.random.arrayElement(faker.definitions.internet.avatar_uri);
16 };
17
18 self.avatar.schema = {
19 "description": "Generates a URL for an avatar.",
20 "sampleResults": ["https://cdn.fakercloud.com/avatars/sydlawrence_128.jpg"]
21 };
22
23 /**
24 * email
25 *
26 * @method faker.internet.email
27 * @param {string} firstName
28 * @param {string} lastName
29 * @param {string} provider
30 */
31 self.email = function (firstName, lastName, provider) {
32 provider = provider || faker.random.arrayElement(faker.definitions.internet.free_email);
33 return faker.helpers.slugify(faker.internet.userName(firstName, lastName)) + "@" + provider;
34 };
35
36 self.email.schema = {
37 "description": "Generates a valid email address based on optional input criteria",
38 "sampleResults": ["foo.bar@gmail.com"],
39 "properties": {
40 "firstName": {
41 "type": "string",
42 "required": false,
43 "description": "The first name of the user"
44 },
45 "lastName": {
46 "type": "string",
47 "required": false,
48 "description": "The last name of the user"
49 },
50 "provider": {
51 "type": "string",
52 "required": false,
53 "description": "The domain of the user"
54 }
55 }
56 };
57 /**
58 * exampleEmail
59 *
60 * @method faker.internet.exampleEmail
61 * @param {string} firstName
62 * @param {string} lastName
63 */
64 self.exampleEmail = function (firstName, lastName) {
65 var provider = faker.random.arrayElement(faker.definitions.internet.example_email);
66 return self.email(firstName, lastName, provider);
67 };
68
69 /**
70 * userName
71 *
72 * @method faker.internet.userName
73 * @param {string} firstName
74 * @param {string} lastName
75 */
76 self.userName = function (firstName, lastName) {
77 var result;
78 firstName = firstName || faker.name.firstName();
79 lastName = lastName || faker.name.lastName();
80 switch (faker.datatype.number(2)) {
81 case 0:
82 result = firstName + faker.datatype.number(99);
83 break;
84 case 1:
85 result = firstName + faker.random.arrayElement([".", "_"]) + lastName;
86 break;
87 case 2:
88 result = firstName + faker.random.arrayElement([".", "_"]) + lastName + faker.datatype.number(99);
89 break;
90 }
91 result = result.toString().replace(/'/g, "");
92 result = result.replace(/ /g, "");
93 return result;
94 };
95
96 self.userName.schema = {
97 "description": "Generates a username based on one of several patterns. The pattern is chosen randomly.",
98 "sampleResults": [
99 "Kirstin39",
100 "Kirstin.Smith",
101 "Kirstin.Smith39",
102 "KirstinSmith",
103 "KirstinSmith39",
104 ],
105 "properties": {
106 "firstName": {
107 "type": "string",
108 "required": false,
109 "description": "The first name of the user"
110 },
111 "lastName": {
112 "type": "string",
113 "required": false,
114 "description": "The last name of the user"
115 }
116 }
117 };
118
119 /**
120 * protocol
121 *
122 * @method faker.internet.protocol
123 */
124 self.protocol = function () {
125 var protocols = ['http','https'];
126 return faker.random.arrayElement(protocols);
127 };
128
129 self.protocol.schema = {
130 "description": "Randomly generates http or https",
131 "sampleResults": ["https", "http"]
132 };
133
134 /**
135 * method
136 *
137 * @method faker.internet.httpMethod
138 */
139 self.httpMethod = function () {
140 var httpMethods = ['GET','POST', 'PUT', 'DELETE', 'PATCH'];
141 return faker.random.arrayElement(httpMethods);
142 };
143
144 self.httpMethod.schema = {
145 "description": "Randomly generates HTTP Methods (GET, POST, PUT, DELETE, PATCH)",
146 "sampleResults": ["GET","POST", "PUT", "DELETE", "PATCH"]
147 };
148
149 /**
150 * url
151 *
152 * @method faker.internet.url
153 */
154 self.url = function () {
155 return faker.internet.protocol() + '://' + faker.internet.domainName();
156 };
157
158 self.url.schema = {
159 "description": "Generates a random URL. The URL could be secure or insecure.",
160 "sampleResults": [
161 "http://rashawn.name",
162 "https://rashawn.name"
163 ]
164 };
165
166 /**
167 * domainName
168 *
169 * @method faker.internet.domainName
170 */
171 self.domainName = function () {
172 return faker.internet.domainWord() + "." + faker.internet.domainSuffix();
173 };
174
175 self.domainName.schema = {
176 "description": "Generates a random domain name.",
177 "sampleResults": ["marvin.org"]
178 };
179
180 /**
181 * domainSuffix
182 *
183 * @method faker.internet.domainSuffix
184 */
185 self.domainSuffix = function () {
186 return faker.random.arrayElement(faker.definitions.internet.domain_suffix);
187 };
188
189 self.domainSuffix.schema = {
190 "description": "Generates a random domain suffix.",
191 "sampleResults": ["net"]
192 };
193
194 /**
195 * domainWord
196 *
197 * @method faker.internet.domainWord
198 */
199 self.domainWord = function () {
200 return faker.name.firstName().replace(/([\\~#&*{}/:<>?|\"'])/ig, '').toLowerCase();
201 };
202
203 self.domainWord.schema = {
204 "description": "Generates a random domain word.",
205 "sampleResults": ["alyce"]
206 };
207
208 /**
209 * ip
210 *
211 * @method faker.internet.ip
212 */
213 self.ip = function () {
214 var randNum = function () {
215 return (faker.datatype.number(255)).toFixed(0);
216 };
217
218 var result = [];
219 for (var i = 0; i < 4; i++) {
220 result[i] = randNum();
221 }
222
223 return result.join(".");
224 };
225
226 self.ip.schema = {
227 "description": "Generates a random IP.",
228 "sampleResults": ["97.238.241.11"]
229 };
230
231 /**
232 * ipv6
233 *
234 * @method faker.internet.ipv6
235 */
236 self.ipv6 = function () {
237 var randHash = function () {
238 var result = "";
239 for (var i = 0; i < 4; i++) {
240 result += (faker.random.arrayElement(["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"]));
241 }
242 return result
243 };
244
245 var result = [];
246 for (var i = 0; i < 8; i++) {
247 result[i] = randHash();
248 }
249 return result.join(":");
250 };
251
252 self.ipv6.schema = {
253 "description": "Generates a random IPv6 address.",
254 "sampleResults": ["2001:0db8:6276:b1a7:5213:22f1:25df:c8a0"]
255 };
256
257 /**
258 * port
259 *
260 * @method faker.internet.port
261 */
262 self.port = function() {
263 return faker.datatype.number({ min: 0, max: 65535 });
264 };
265
266 self.port.schema = {
267 "description": "Generates a random port number.",
268 "sampleResults": ["4422"]
269 };
270
271 /**
272 * userAgent
273 *
274 * @method faker.internet.userAgent
275 */
276 self.userAgent = function () {
277 return random_ua.generate(faker);
278 };
279
280 self.userAgent.schema = {
281 "description": "Generates a random user agent.",
282 "sampleResults": ["Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10_7_5 rv:6.0; SL) AppleWebKit/532.0.1 (KHTML, like Gecko) Version/7.1.6 Safari/532.0.1"]
283 };
284
285 /**
286 * color
287 *
288 * @method faker.internet.color
289 * @param {number} baseRed255
290 * @param {number} baseGreen255
291 * @param {number} baseBlue255
292 */
293 self.color = function (baseRed255, baseGreen255, baseBlue255) {
294 baseRed255 = baseRed255 || 0;
295 baseGreen255 = baseGreen255 || 0;
296 baseBlue255 = baseBlue255 || 0;
297 // based on awesome response : http://stackoverflow.com/questions/43044/algorithm-to-randomly-generate-an-aesthetically-pleasing-color-palette
298 var red = Math.floor((faker.datatype.number(256) + baseRed255) / 2);
299 var green = Math.floor((faker.datatype.number(256) + baseGreen255) / 2);
300 var blue = Math.floor((faker.datatype.number(256) + baseBlue255) / 2);
301 var redStr = red.toString(16);
302 var greenStr = green.toString(16);
303 var blueStr = blue.toString(16);
304 return '#' +
305 (redStr.length === 1 ? '0' : '') + redStr +
306 (greenStr.length === 1 ? '0' : '') + greenStr +
307 (blueStr.length === 1 ? '0': '') + blueStr;
308
309 };
310
311 self.color.schema = {
312 "description": "Generates a random hexadecimal color.",
313 "sampleResults": ["#06267f"],
314 "properties": {
315 "baseRed255": {
316 "type": "number",
317 "required": false,
318 "description": "The red value. Valid values are 0 - 255."
319 },
320 "baseGreen255": {
321 "type": "number",
322 "required": false,
323 "description": "The green value. Valid values are 0 - 255."
324 },
325 "baseBlue255": {
326 "type": "number",
327 "required": false,
328 "description": "The blue value. Valid values are 0 - 255."
329 }
330 }
331 };
332
333 /**
334 * mac
335 *
336 * @method faker.internet.mac
337 * @param {string} sep
338 */
339 self.mac = function(sep){
340 var i,
341 mac = "",
342 validSep = ':';
343
344 // if the client passed in a different separator than `:`,
345 // we will use it if it is in the list of acceptable separators (dash or no separator)
346 if (['-', ''].indexOf(sep) !== -1) {
347 validSep = sep;
348 }
349
350 for (i=0; i < 12; i++) {
351 mac+= faker.datatype.number(15).toString(16);
352 if (i%2==1 && i != 11) {
353 mac+=validSep;
354 }
355 }
356 return mac;
357 };
358
359 self.mac.schema = {
360 "description": "Generates a random mac address.",
361 "sampleResults": ["78:06:cc:ae:b3:81"]
362 };
363
364 /**
365 * password
366 *
367 * @method faker.internet.password
368 * @param {number} len
369 * @param {boolean} memorable
370 * @param {string} pattern
371 * @param {string} prefix
372 */
373 self.password = function (len, memorable, pattern, prefix) {
374 len = len || 15;
375 if (typeof memorable === "undefined") {
376 memorable = false;
377 }
378 /*
379 * password-generator ( function )
380 * Copyright(c) 2011-2013 Bermi Ferrer <bermi@bermilabs.com>
381 * MIT Licensed
382 */
383 var consonant, letter, vowel;
384 letter = /[a-zA-Z]$/;
385 vowel = /[aeiouAEIOU]$/;
386 consonant = /[bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ]$/;
387 var _password = function (length, memorable, pattern, prefix) {
388 var char, n;
389 if (length == null) {
390 length = 10;
391 }
392 if (memorable == null) {
393 memorable = true;
394 }
395 if (pattern == null) {
396 pattern = /\w/;
397 }
398 if (prefix == null) {
399 prefix = '';
400 }
401 if (prefix.length >= length) {
402 return prefix;
403 }
404 if (memorable) {
405 if (prefix.match(consonant)) {
406 pattern = vowel;
407 } else {
408 pattern = consonant;
409 }
410 }
411 n = faker.datatype.number(94) + 33;
412 char = String.fromCharCode(n);
413 if (memorable) {
414 char = char.toLowerCase();
415 }
416 if (!char.match(pattern)) {
417 return _password(length, memorable, pattern, prefix);
418 }
419 return _password(length, memorable, pattern, "" + prefix + char);
420 };
421 return _password(len, memorable, pattern, prefix);
422 }
423
424 self.password.schema = {
425 "description": "Generates a random password.",
426 "sampleResults": [
427 "AM7zl6Mg",
428 "susejofe"
429 ],
430 "properties": {
431 "length": {
432 "type": "number",
433 "required": false,
434 "description": "The number of characters in the password."
435 },
436 "memorable": {
437 "type": "boolean",
438 "required": false,
439 "description": "Whether a password should be easy to remember."
440 },
441 "pattern": {
442 "type": "regex",
443 "required": false,
444 "description": "A regex to match each character of the password against. This parameter will be negated if the memorable setting is turned on."
445 },
446 "prefix": {
447 "type": "string",
448 "required": false,
449 "description": "A value to prepend to the generated password. The prefix counts towards the length of the password."
450 }
451 }
452 };
453
454};
455
456
457module["exports"] = Internet;