Skip to content
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

fix: fake behavior with special replacement patterns #688

Merged
merged 5 commits into from
Mar 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 8 additions & 14 deletions src/fake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,32 +46,25 @@ export class Fake {
* faker.fake('I flipped the coin an got: {{random.arrayElement(["heads", "tails"])}}') // 'I flipped the coin an got: tails'
*/
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('}}');
const start = str.search(/{{[a-z]/);
const end = str.indexOf('}}', start);

// 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.substring(start + 2, end);
const token = str.substring(start + 2, end + 2);
let method = token.replace('}}', '').replace('{{', '');

// console.log('method', method)

// extract method parameters
const regExp = /\(([^)]+)\)/;
const matches = regExp.exec(method);
Expand Down Expand Up @@ -111,13 +104,14 @@ export class Fake {

let result: string;
if (typeof params === 'string' && params.length === 0) {
result = fn();
result = String(fn());
} else {
result = fn(params);
result = String(fn(params));
}

// replace the found tag with the returned fake value
res = str.replace('{{' + token + '}}', result);
// Replace the found tag with the returned fake value
// We cannot use string.replace here because the result might contain evaluated characters
const res = str.substring(0, start) + result + str.substring(end + 2);

if (res === '') {
return '';
Expand Down
34 changes: 34 additions & 0 deletions test/fake.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,39 @@ describe('fake', () => {
it('should be able to return empty strings', () => {
expect(faker.fake('{{helpers.repeatString}}')).toBe('');
});

it('should be able to handle only {{ brackets', () => {
expect(faker.fake('{{hello')).toBe('{{hello');
expect(faker.fake('hello{{')).toBe('hello{{');
});

it('should be able to handle only }} brackets', () => {
expect(faker.fake('hello}}')).toBe('hello}}');
expect(faker.fake('}}hello')).toBe('}}hello');
});

it('should be able to handle reverted brackets', () => {
expect(faker.fake('}}hello{{')).toBe('}}hello{{');
});

it('should be able to handle random }} brackets', () => {
expect(faker.fake('}}hello{{random.alpha}}')).toMatch(/^}}hello[a-z]$/);
});

it('should be able to handle connected brackets', () => {
expect(faker.fake('{{{random.alpha}}}')).toMatch(/^{[a-z]}$/);
});

it('should be able to handle empty brackets', () => {
expect(faker.fake('{{}}')).toBe('{{}}');
});
Shinigami92 marked this conversation as resolved.
Show resolved Hide resolved

it('should be able to handle special replacement patterns', () => {
(faker.random as any).special = () => '$&';

expect(faker.fake('{{random.special}}')).toBe('$&');

delete (faker.random as any).special;
});
});
});