Skip to content

test(helpers): mustache special cases #1875

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

Merged
merged 8 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
4 changes: 3 additions & 1 deletion src/modules/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,10 @@ export class HelpersModule {

for (const p in data) {
const re = new RegExp(`{{${p}}}`, 'g');
const value = data[p];
let value = data[p];
if (typeof value === 'string') {
// escape $, source: https://stackoverflow.com/a/6969486/6897682
value = value.replace(/\$/g, '$$$$');
str = str.replace(re, value);
} else {
str = str.replace(re, value);
Expand Down
14 changes: 13 additions & 1 deletion test/helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -720,12 +720,24 @@ describe('helpers', () => {

it('supports function replace values faker values', () => {
const actual = faker.helpers.mustache('1{{value}}3', {
value: faker.string.sample(2),
value: faker.string.alphanumeric({ length: 2 }),
});

expect(actual).toHaveLength(4);
});

it.each([
['$&', 4],
["$'", 4],
])('supports replace value %s', (value, expectedLength) => {
const actual = faker.helpers.mustache('1{{value}}3', {
value,
});

expect(actual).toBe(`1${value}3`);
expect(actual).toHaveLength(expectedLength);
});

it('supports function replace values faker function', () => {
const actual = faker.helpers.mustache('1{{value}}3', {
value: () => faker.string.sample(3),
Expand Down