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 10d7442 commit ab70e02
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 50 deletions.
11 changes: 8 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@
"crypto-js": "4.2.0",
"deep-object-diff": "1.1.9",
"get-value": "3.0.1",
"handlebars": "^4.7.8",
"is": "3.3.0",
"lodash.get": "4.4.2",
"lodash.isempty": "4.4.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/analytics-js-integrations/.size-limit.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ module.exports = [
{
name: 'All Integrations (legacy build) - CDN',
path: 'dist/cdn/legacy/js-integrations/*.min.js',
limit: '123 KiB',
limit: '105 KiB',
},
];
Original file line number Diff line number Diff line change
Expand Up @@ -457,58 +457,42 @@ describe('formatTraits', () => {
});

describe('generatePageCustomEventName', () => {
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 = generatePageCustomEventName(message, userDefinedEventTemplate);
expect(result).toBe(expected);
});

it('should generate a default `Loaded a Page` event name when userDefinedEventTemplate is undefined/null/empty', () => {
const message = { name: 'Home' };
const userDefinedEventTemplate = undefined;
const expected = 'Loaded a Page';
const result = generatePageCustomEventName(message, userDefinedEventTemplate);
expect(result).toBe(expected);
});

it('should return the userDefinedEventTemplate when it does not contain handlebars', () => {
const message = { name: 'Home' };
const userDefinedEventTemplate = 'Viewed a Home page';
const expected = 'Viewed a Home page';
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 = generatePageCustomEventName(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';
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 = generatePageCustomEventName(message, userDefinedEventTemplate);
expect(result).toBe(expected);
});

it('should return a custom event name when userDefinedEventTemplate contains handlebars and message object is nested', () => {
const message = { a: { b: 'abc' } };
const userDefinedEventTemplate = 'Viewed a {{ a.b }} page';
const expected = 'Viewed a abc page';
const result = generatePageCustomEventName(message, userDefinedEventTemplate);
expect(result).toBe(expected);
});

it('should return a default `Loaded a Page` event name when userDefinedEventTemplate contains invalid handlebars and message object is provided', () => {
const message = { name: 'Home' };
const userDefinedEventTemplate = 'Viewed a {{ name } page';
const expected = 'Loaded a Page';
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 = generatePageCustomEventName(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 = generatePageCustomEventName(message, userDefinedEventTemplate);
expect(result).toBe(expected);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
/* eslint-disable no-prototype-builtins */
import get from 'get-value';
import { DISPLAY_NAME } from '@rudderstack/analytics-js-common/constants/integrations/Mixpanel/constants';
import Handlebars from 'handlebars';
import Logger from '../../utils/logger';
import { getDefinedTraits, extractCustomFields } from '../../utils/utils';

Expand Down Expand Up @@ -241,21 +240,32 @@ const getConsolidatedPageCalls = config =>
: true;

/**
* Generates a custom event name for a page calls.
* Generates a custom event name for a page or screen.
*
* @param {Object} message - The message object
* @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 generatePageCustomEventName = (message, userDefinedEventTemplate) => {
try {
const eventTemplate = Handlebars.compile(userDefinedEventTemplate);
return eventTemplate(message);
} catch (error) {
logger.error(`Error generating custom event name: ${error.message}`);
// Return a default or empty string as a fallback
return 'Loaded a Page';
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 265 in packages/analytics-js-integrations/src/integrations/Mixpanel/util.js

View check run for this annotation

Codecov / codecov/patch

packages/analytics-js-integrations/src/integrations/Mixpanel/util.js#L265

Added line #L265 was not covered by tests
}

return eventName.trim();
};

export {
Expand Down

0 comments on commit ab70e02

Please sign in to comment.