-
-
Notifications
You must be signed in to change notification settings - Fork 931
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: migrate fake #79
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import type { Faker } from '.'; | ||
|
||
/** | ||
* Generator method for combining faker methods based on string input | ||
*/ | ||
export class Fake { | ||
constructor(private readonly faker: Faker) { | ||
// Bind `this` so namespaced is working correctly | ||
for (const name of Object.getOwnPropertyNames(Fake.prototype)) { | ||
if (name === 'constructor' || typeof this[name] !== 'function') { | ||
continue; | ||
} | ||
this[name] = this[name].bind(this); | ||
} | ||
} | ||
|
||
/** | ||
* Generator method for combining faker methods based on string input | ||
* | ||
* __Example:__ | ||
* | ||
* ``` | ||
* console.log(faker.fake('{{name.lastName}}, {{name.firstName}} {{name.suffix}}')); | ||
* // outputs: "Marks, Dean Sr." | ||
* ``` | ||
* | ||
* This will interpolate the format string with the value of methods | ||
* [name.lastName]{@link faker.name.lastName}, [name.firstName]{@link faker.name.firstName}, | ||
* and [name.suffix]{@link faker.name.suffix} | ||
* | ||
* @method faker.fake | ||
* @param str | ||
*/ | ||
fake(str: string): string { | ||
// setup default response as empty string | ||
let res = ''; | ||
|
||
// if incoming str parameter is not provided, return error message | ||
if (typeof str !== 'string' || str.length === 0) { | ||
throw new Error('string parameter is required!'); | ||
} | ||
|
||
// find first matching {{ and }} | ||
const start = str.search('{{'); | ||
const end = str.search('}}'); | ||
|
||
// if no {{ and }} is found, we are done | ||
if (start === -1 || end === -1) { | ||
return str; | ||
} | ||
|
||
// console.log('attempting to parse', str); | ||
|
||
// extract method name from between the {{ }} that we found | ||
// for example: {{name.firstName}} | ||
const token = str.substr(start + 2, end - start - 2); | ||
let method = token.replace('}}', '').replace('{{', ''); | ||
|
||
// console.log('method', method) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be removed, this will not break anything |
||
|
||
// extract method parameters | ||
const regExp = /\(([^)]+)\)/; | ||
const matches = regExp.exec(method); | ||
let parameters = ''; | ||
if (matches) { | ||
method = method.replace(regExp, ''); | ||
parameters = matches[1]; | ||
} | ||
|
||
// split the method into module and function | ||
const parts = method.split('.'); | ||
|
||
if (typeof this.faker[parts[0]] === 'undefined') { | ||
throw new Error('Invalid module: ' + parts[0]); | ||
} | ||
|
||
if (typeof this.faker[parts[0]][parts[1]] === 'undefined') { | ||
throw new Error('Invalid method: ' + parts[0] + '.' + parts[1]); | ||
} | ||
|
||
// assign the function from the module.function namespace | ||
const fn = this.faker[parts[0]][parts[1]]; | ||
|
||
// If parameters are populated here, they are always going to be of string type | ||
// since we might actually be dealing with an object or array, | ||
// we always attempt to the parse the incoming parameters into JSON | ||
let params: any; | ||
// Note: we experience a small performance hit here due to JSON.parse try / catch | ||
// If anyone actually needs to optimize this specific code path, please open a support issue on github | ||
try { | ||
params = JSON.parse(parameters); | ||
} catch (err) { | ||
// since JSON.parse threw an error, assume parameters was actually a string | ||
params = parameters; | ||
} | ||
|
||
let result: string; | ||
if (typeof params === 'string' && params.length === 0) { | ||
result = fn.call(this); | ||
} else { | ||
result = fn.call(this, params); | ||
} | ||
|
||
// replace the found tag with the returned fake value | ||
res = str.replace('{{' + token + '}}', result); | ||
|
||
// return the response recursively until we are done finding all tags | ||
return this.fake(res); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be removed, this will not break anything