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

feat: add util for applying json string template #3699

Merged
merged 1 commit into from
Sep 3, 2024
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
6 changes: 6 additions & 0 deletions src/v0/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2268,6 +2268,11 @@ const validateEventAndLowerCaseConversion = (event, isMandatory, convertToLowerC
const applyCustomMappings = (message, mappings) =>
JsonTemplateEngine.createAsSync(mappings, { defaultPathType: PathType.JSON }).evaluate(message);

const applyJSONStringTemplate = (message, template) =>
JsonTemplateEngine.createAsSync(template.replace(/{{/g, '${').replace(/}}/g, '}'), {
defaultPathType: PathType.JSON,
}).evaluate(message);

/**
* Gets url path omitting the hostname & protocol
*
Expand All @@ -2293,6 +2298,7 @@ module.exports = {
addExternalIdToTraits,
adduserIdFromExternalId,
applyCustomMappings,
applyJSONStringTemplate,
base64Convertor,
batchMultiplexedEvents,
checkEmptyStringInarray,
Expand Down
24 changes: 24 additions & 0 deletions src/v0/util/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const {
combineBatchRequestsWithSameJobIds,
validateEventAndLowerCaseConversion,
} = require('./index');
const exp = require('constants');

// Names of the utility functions to test
const functionNames = [
Expand Down Expand Up @@ -691,6 +692,29 @@ describe('extractCustomFields', () => {
});
});

describe('applyJSONStringTemplate', () => {
it('should apply JSON string template to the payload', () => {
const payload = {
domain: 'abc',
};
const template = '`https://{{$.domain}}.com`';

const result = utilities.applyJSONStringTemplate(payload, template);
expect(result).toEqual('https://abc.com');
});

it('should apply JSON string template to the payload multiple times', () => {
const payload = {
domain: 'abc',
subdomain: 'def',
};
const template = '`https://{{$.subdomain}}.{{$.domain}}.com`';

const result = utilities.applyJSONStringTemplate(payload, template);
expect(result).toEqual('https://def.abc.com');
});
});

describe('get relative path from url', () => {
test('valid url', () => {
expect(utilities.getRelativePathFromURL('https://google.com/a/b/c')).toEqual('/a/b/c');
Expand Down
Loading