1 | /*
|
2 | fake.js - generator method for combining faker methods based on string input
|
3 |
|
4 | */
|
5 |
|
6 | function Fake (faker) {
|
7 |
|
8 | /**
|
9 | * Generator method for combining faker methods based on string input
|
10 | *
|
11 | * __Example:__
|
12 | *
|
13 | * ```
|
14 | * console.log(faker.fake('{{name.lastName}}, {{name.firstName}} {{name.suffix}}'));
|
15 | * //outputs: "Marks, Dean Sr."
|
16 | * ```
|
17 | *
|
18 | * This will interpolate the format string with the value of methods
|
19 | * [name.lastName]{@link faker.name.lastName}, [name.firstName]{@link faker.name.firstName},
|
20 | * and [name.suffix]{@link faker.name.suffix}
|
21 | *
|
22 | * @method faker.fake
|
23 | * @param {string} str
|
24 | */
|
25 | this.fake = function fake (str) {
|
26 | // setup default response as empty string
|
27 | var res = '';
|
28 |
|
29 | // if incoming str parameter is not provided, return error message
|
30 | if (typeof str !== 'string' || str.length === 0) {
|
31 | throw new Error('string parameter is required!');
|
32 | }
|
33 |
|
34 | // find first matching {{ and }}
|
35 | var start = str.search('{{');
|
36 | var end = str.search('}}');
|
37 |
|
38 | // if no {{ and }} is found, we are done
|
39 | if (start === -1 && end === -1) {
|
40 | return str;
|
41 | }
|
42 |
|
43 | // console.log('attempting to parse', str);
|
44 |
|
45 | // extract method name from between the {{ }} that we found
|
46 | // for example: {{name.firstName}}
|
47 | var token = str.substr(start + 2, end - start - 2);
|
48 | var method = token.replace('}}', '').replace('{{', '');
|
49 |
|
50 | // console.log('method', method)
|
51 |
|
52 | // extract method parameters
|
53 | var regExp = /\(([^)]+)\)/;
|
54 | var matches = regExp.exec(method);
|
55 | var parameters = '';
|
56 | if (matches) {
|
57 | method = method.replace(regExp, '');
|
58 | parameters = matches[1];
|
59 | }
|
60 |
|
61 | // split the method into module and function
|
62 | var parts = method.split('.');
|
63 |
|
64 | if (typeof faker[parts[0]] === "undefined") {
|
65 | throw new Error('Invalid module: ' + parts[0]);
|
66 | }
|
67 |
|
68 | if (typeof faker[parts[0]][parts[1]] === "undefined") {
|
69 | throw new Error('Invalid method: ' + parts[0] + "." + parts[1]);
|
70 | }
|
71 |
|
72 | // assign the function from the module.function namespace
|
73 | var fn = faker[parts[0]][parts[1]];
|
74 |
|
75 | // If parameters are populated here, they are always going to be of string type
|
76 | // since we might actually be dealing with an object or array,
|
77 | // we always attempt to the parse the incoming parameters into JSON
|
78 | var params;
|
79 | // Note: we experience a small performance hit here due to JSON.parse try / catch
|
80 | // If anyone actually needs to optimize this specific code path, please open a support issue on github
|
81 | try {
|
82 | params = JSON.parse(parameters)
|
83 | } catch (err) {
|
84 | // since JSON.parse threw an error, assume parameters was actually a string
|
85 | params = parameters;
|
86 | }
|
87 |
|
88 | var result;
|
89 | if (typeof params === "string" && params.length === 0) {
|
90 | result = fn.call(this);
|
91 | } else {
|
92 | result = fn.call(this, params);
|
93 | }
|
94 |
|
95 | // replace the found tag with the returned fake value
|
96 | res = str.replace('{{' + token + '}}', result);
|
97 |
|
98 | // return the response recursively until we are done finding all tags
|
99 | return fake(res);
|
100 | }
|
101 |
|
102 | return this;
|
103 |
|
104 |
|
105 | }
|
106 |
|
107 | module['exports'] = Fake; |
\ | No newline at end of file |