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 | |
| parent | 5f4843c6474c0cc60b0480b56ae0e6c0b53f6d4a (diff) | |
| download | faker-0e739eb0926a67e230674fe83eb98062a8dd5a64.tar.xz faker-0e739eb0926a67e230674fe83eb98062a8dd5a64.zip | |
Support parameters
| -rw-r--r-- | lib/fake.js | 17 | ||||
| -rw-r--r-- | test/fake.unit.js | 31 |
2 files changed, 44 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); diff --git a/test/fake.unit.js b/test/fake.unit.js new file mode 100644 index 00000000..d0faa0da --- /dev/null +++ b/test/fake.unit.js @@ -0,0 +1,31 @@ +if (typeof module !== 'undefined') { + var assert = require('assert'); + var sinon = require('sinon'); + var faker = require('../index'); +} + +describe.only("fake.js", function () { + describe("fake()", function () { + it("replaces a token with a random value for a method with no parameters", function () { + var name = faker.fake('{{phone.phoneNumber}}'); + assert.ok(name.match(/\d/)); + }); + + it("replaces multiple tokens with random values for methods with no parameters", function () { + var name = faker.fake('{{helpers.randomize}}{{helpers.randomize}}{{helpers.randomize}}'); + assert.ok(name.match(/[abc]{3}/)); + }); + + it("replaces a token with a random value for a methods with a simple parameter", function () { + var arr = ["one", "two", "three"]; + var random = faker.fake('{{helpers.slugify("Will This Work")}}'); + assert.ok(random === "Will-This-Work"); + }); + + it("replaces a token with a random value for a method with an array parameter", function () { + var arr = ["one", "two", "three"]; + var random = faker.fake('{{helpers.randomize(["one", "two", "three"])}}'); + assert.ok(arr.indexOf(random) > -1); + }); + }); +}); |
