Skip to content

Commit

Permalink
refactor: replace handlebar with regex
Browse files Browse the repository at this point in the history
  • Loading branch information
Gauravudia committed Feb 27, 2024
1 parent 8f73c44 commit fd5d594
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 21 deletions.
22 changes: 19 additions & 3 deletions src/v0/destinations/mp/util.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const lodash = require('lodash');
const set = require('set-value');
const get = require('get-value');
const Handlebars = require('handlebars');
const { InstrumentationError, ConfigurationError } = require('@rudderstack/integrations-lib');
const {
isDefined,
Expand Down Expand Up @@ -309,15 +308,32 @@ function trimTraits(traits, contextTraits, setOnceProperties) {
* @param {string} userDefinedEventTemplate - The user-defined event template to be used for generating the event name.
* @throws {ConfigurationError} If the event template is missing.
* @returns {string} The generated custom event name.
* @example
* const userDefinedEventTemplate = "Viewed {{ category }} {{ name }} Page";
* const message = {name: 'Home', properties: {category: 'Index'}};
* output: "Viewed Index Home Page"
*/
const generatePageOrScreenCustomEventName = (message, userDefinedEventTemplate) => {
if (!userDefinedEventTemplate) {
throw new ConfigurationError(

Check warning on line 318 in src/v0/destinations/mp/util.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/mp/util.js#L318

Added line #L318 was not covered by tests
'Event template is missing. Please provide a valid event template in `Page/Screen Event Name Template` setting',
);
}
const eventTemplate = Handlebars.compile(userDefinedEventTemplate);
return eventTemplate(message);

let eventName = userDefinedEventTemplate
.replace('{{ category }}', message.properties?.category || '')
.trim();
eventName = eventName.replace('{{ name }}', message.name || '').trim();
// Remove any extra space between placeholders
eventName = eventName.replace(/\s{2,}/g, ' ');

// Check if any placeholders remain
if (eventName.includes('{{')) {
// Handle the case where either name or category is missing
eventName = eventName.replace(/{{\s*\w+\s*}}/g, '');

Check warning on line 333 in src/v0/destinations/mp/util.js

View check run for this annotation

Codecov / codecov/patch

src/v0/destinations/mp/util.js#L333

Added line #L333 was not covered by tests
}

return eventName.trim();
};

module.exports = {
Expand Down
44 changes: 26 additions & 18 deletions src/v0/destinations/mp/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,10 +450,26 @@ describe('Unit test cases for trimTraits', () => {
});

describe('generatePageOrScreenCustomEventName', () => {
it('should generate a custom event name when userDefinedEventTemplate contains handlebars and message object is provided', () => {
const message = { name: 'Home' };
const userDefinedEventTemplate = 'Viewed a {{ name }} page';
const expected = 'Viewed a Home page';
it('should generate a custom event name when userDefinedEventTemplate contains event template and message object is provided', () => {
const message = { name: 'Doc', properties: { category: 'Integration' } };
const userDefinedEventTemplate = 'Viewed {{ category }} {{ name }} page';
const expected = 'Viewed Integration Doc page';
const result = generatePageOrScreenCustomEventName(message, userDefinedEventTemplate);
expect(result).toBe(expected);
});

it('should generate a custom event name when userDefinedEventTemplate contains event template and category or name is missing in message object', () => {
const message = { name: 'Doc' };
const userDefinedEventTemplate = 'Viewed {{ category }} {{ name }} page';
const expected = 'Viewed Doc page';
const result = generatePageOrScreenCustomEventName(message, userDefinedEventTemplate);
expect(result).toBe(expected);
});

it('should generate a custom event name when userDefinedEventTemplate contains only category or name placeholder and message object is provided', () => {
const message = { name: 'Doc', properties: { category: 'Integration' } };
const userDefinedEventTemplate = 'Viewed {{ name }} page';
const expected = 'Viewed Doc page';
const result = generatePageOrScreenCustomEventName(message, userDefinedEventTemplate);
expect(result).toBe(expected);
});
Expand All @@ -466,26 +482,18 @@ describe('generatePageOrScreenCustomEventName', () => {
}).toThrow(ConfigurationError);
});

it('should return the userDefinedEventTemplate when it does not contain handlebars', () => {
const message = { name: 'Home' };
it('should return the userDefinedEventTemplate when it does not contain placeholder {{}}', () => {
const message = { name: 'Index' };
const userDefinedEventTemplate = 'Viewed a Home page';
const expected = 'Viewed a Home page';
const result = generatePageOrScreenCustomEventName(message, userDefinedEventTemplate);
expect(result).toBe(expected);
});

it('should return a event name when message object is not provided', () => {
const message = undefined;
const userDefinedEventTemplate = 'Viewed a {{ path }} page';
const expected = 'Viewed a page';
const result = generatePageOrScreenCustomEventName(message, userDefinedEventTemplate);
expect(result).toBe(expected);
});

it('should return a custom event name when userDefinedEventTemplate contains multiple handlebar and message object is provided', () => {
const message = { name: 'Home', properties: { category: 'Index' } };
const userDefinedEventTemplate = 'Viewed a {{ properties.category }} {{ name }} page';
const expected = 'Viewed a Index Home page';
it('should return a event name when message object is not provided/empty', () => {
const message = {};
const userDefinedEventTemplate = 'Viewed {{ category }} {{ name }} page';
const expected = 'Viewed page';
const result = generatePageOrScreenCustomEventName(message, userDefinedEventTemplate);
expect(result).toBe(expected);
});
Expand Down

0 comments on commit fd5d594

Please sign in to comment.