diff options
| author | Craig Morris <[email protected]> | 2015-12-01 11:49:13 +0000 |
|---|---|---|
| committer | Marak <[email protected]> | 2016-02-14 13:03:35 -0500 |
| commit | 0e739eb0926a67e230674fe83eb98062a8dd5a64 (patch) | |
| tree | 62f6e538c0c2b6d645e8e0a71b2858a6212e2ebe /lib | |
| parent | 5f4843c6474c0cc60b0480b56ae0e6c0b53f6d4a (diff) | |
| download | faker-0e739eb0926a67e230674fe83eb98062a8dd5a64.tar.xz faker-0e739eb0926a67e230674fe83eb98062a8dd5a64.zip | |
Support parameters
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/fake.js | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/lib/fake.js b/lib/fake.js index c2e43aa4..611d9b6b 100644 --- a/lib/fake.js +++ b/lib/fake.js @@ -28,11 +28,19 @@ function Fake (faker) { // extract method name from between the {{ }} that we found // for example: {{name.firstName}} - var method = str.substr(start + 2, end - start - 2); - method = method.replace('}}', ''); - method = method.replace('{{', ''); + var token = str.substr(start + 2, end - start - 2); + var method = token.replace('}}', '').replace('{{', ''); // console.log('method', method) + + // extract method parameters + var regExp = /\(([^)]+)\)/; + var matches = regExp.exec(method); + var parameters = ''; + if (matches) { + method = method.replace(regExp, ''); + parameters = matches[1]; + } // split the method into module and function var parts = method.split('.'); @@ -49,7 +57,8 @@ function Fake (faker) { var fn = faker[parts[0]][parts[1]]; // replace the found tag with the returned fake value - res = str.replace('{{' + method + '}}', fn()); + eval('var result = fn(' + parameters + ');'); + res = str.replace('{{' + token + '}}', result); // return the response recursively until we are done finding all tags return fake(res); |
