Skip to content

Commit

Permalink
chore: improvements and reverts
Browse files Browse the repository at this point in the history
  • Loading branch information
ST-DDT committed Nov 30, 2023
1 parent 7aac074 commit 097a172
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
12 changes: 8 additions & 4 deletions src/modules/helpers/eval.ts
Expand Up @@ -137,7 +137,11 @@ function evalProcessFunction(
* @param input The input string to parse.
*/
function findParams(input: string): [continueIndex: number, params: unknown[]] {
let index = input.indexOf(')');
let index = input.indexOf(')', 1);
if (index === -1) {
throw new FakerError(`Missing closing parenthesis in '${input}'`);
}

Check warning on line 143 in src/modules/helpers/eval.ts

View check run for this annotation

Codecov / codecov/patch

src/modules/helpers/eval.ts#L142-L143

Added lines #L142 - L143 were not covered by tests

while (index !== -1) {
const params = input.substring(1, index);
try {
Expand All @@ -157,9 +161,9 @@ function findParams(input: string): [continueIndex: number, params: unknown[]] {
index = input.indexOf(')', index + 1);
}

throw new FakerError(
`Function parameters cannot be parsed as JSON or simple string: '${input}'`
);
index = input.lastIndexOf(')', 1);
const params = input.substring(1, index);
return [index, JSON.parse(`["${params}"]`) as unknown[]];
}

Check warning on line 167 in src/modules/helpers/eval.ts

View check run for this annotation

Codecov / codecov/patch

src/modules/helpers/eval.ts#L160-L167

Added lines #L160 - L167 were not covered by tests

/**
Expand Down
3 changes: 1 addition & 2 deletions src/modules/helpers/index.ts
Expand Up @@ -1423,8 +1423,7 @@ export class HelpersModule extends SimpleHelpersModule {
const method = token.replace('}}', '').replace('{{', '');

const result = fakeEval(method, this.faker);
const stringified =
typeof result === 'object' ? JSON.stringify(result) : String(result);
const stringified = String(result);

// Replace the found tag with the returned fake value
// We cannot use string.replace here because the result might contain evaluated characters
Expand Down
29 changes: 16 additions & 13 deletions test/modules/helpers.spec.ts
Expand Up @@ -1030,16 +1030,30 @@ describe('helpers', () => {
);
});

it('does allow missing method name', () => {
const actual = faker.helpers.fake('{{location}}');
expect(actual).toBe('[object Object]');
});

it('does not allow invalid method name', () => {
expect(() => faker.helpers.fake('{{location.foo}}')).toThrow(
new FakerError(`Cannot resolve expression 'location.foo'`)
);
});

it('does allow complex data', () => {
it('should support complex data', () => {
const actual = faker.helpers.fake('{{science.unit}}');
expect(actual).toBe('[object Object]');
});

it('should support resolving a value in a complex object', () => {
const complex = faker.helpers.fake('{{airline.airline}}');
expect(complex).toBe('[object Object]');

const actual = faker.helpers.fake('{{airline.airline.iataCode}}');
expect(actual).toBeTypeOf('string');
expect(
faker.definitions.science.unit.map((value) => JSON.stringify(value))
faker.definitions.airline.airline.map(({ iataCode }) => iataCode)
).toContain(actual);
});

Expand Down Expand Up @@ -1136,17 +1150,6 @@ describe('helpers', () => {
it('should not trim whitespace', () => {
expect(faker.helpers.fake(' --- ')).toBe(' --- ');
});

it('should support resolving a value in a complex object', () => {
const complex = faker.helpers.fake('{{airline.airline}}');
expect(complex).toMatch(/^{.+}$/i); // JSON string

const actual = faker.helpers.fake('{{airline.airline.iataCode}}');
expect(actual).toBeTypeOf('string');
expect(
faker.definitions.airline.airline.map(({ iataCode }) => iataCode)
).toContain(actual);
});
});

describe('rangeToNumber()', () => {
Expand Down

0 comments on commit 097a172

Please sign in to comment.