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

fix: add optionalPrams properties in JSON data #59

Merged
merged 7 commits into from
Jul 27, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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: 4 additions & 2 deletions src/third-parties/google-analytics/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
{
"url": "https://www.googletagmanager.com/gtag/js",
"params": ["id", "l"],
"optionalParam": ["l"],
"strategy": "worker",
"location": "head",
"action": "append",
"key": "gtag"
},
{
"code": "window['{{l}}']=window['{{l}}']||[];window['{{l}}'].push({'js':new Date()});window['{{l}}'].push({'config':'{{id}}'})",
"params": ["id", "l"],
"code": "window[{{l}}??'dataLayer']=window[{{l}}??'dataLayer']||[];window[{{l}}??'dataLayer'].push({'js':new Date()});window[{{l}}??'dataLayer'].push({'config':{{id}}})",
"params": ["id"],
"optionalParam": ["l"],
huang-julien marked this conversation as resolved.
Show resolved Hide resolved
"strategy": "worker",
"location": "head",
"action": "append",
Expand Down
5 changes: 3 additions & 2 deletions src/third-parties/google-tag-manager/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
{
"url": "https://www.googletagmanager.com/gtm.js",
"params": ["id", "l"],
"optionalParam": ["l"],
"strategy": "worker",
"location": "head",
"action": "append",
"key": "gtm"
},
{
"code": "window['{{l}}']=window['{{l}}']||[];window['{{l}}'].push({'gtm.start':new Date().getTime(),event:'gtm.js'});",
"params": ["l"],
"code": "window[{{l}}??'dataLayer']=window[{{l}}??'dataLayer']||[];window[{{l}}??'dataLayer'].push({'gtm.start':new Date().getTime(),event:'gtm.js'});",
"optionalParam": ["l"],
huang-julien marked this conversation as resolved.
Show resolved Hide resolved
"strategy": "worker",
"location": "head",
"action": "append",
Expand Down
1 change: 1 addition & 0 deletions src/types/type-declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type HtmlAttributes = {

type ScriptBase = {
params?: Array<string>;
optionalParam?: Array<string>;
huang-julien marked this conversation as resolved.
Show resolved Hide resolved
strategy: ScriptStrategy;
location: ScriptLocation;
action: ScriptAction;
Expand Down
48 changes: 47 additions & 1 deletion src/utils/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { formatUrl, createHtml, formatData } from '.';
import { formatUrl, createHtml, formatData, formatCode } from '.';
import type { Data, ExternalScript } from '../types';

describe('Utils', () => {
Expand Down Expand Up @@ -231,4 +231,50 @@ describe('Utils', () => {
expect(result.scripts).toEqual(undefined);
});
});
describe('formatCode', () => {
it.each([
// string
{
input: "window[{{l}}??'dataLayer']=window[{{l}}??'dataLayer']||[];",
params: {
l: 'some-datalayer',
},
output: `window["some-datalayer"??'dataLayer']=window["some-datalayer"??'dataLayer']||[];`,
},
// number
{
input: '{{number}}+1',
params: {
number: 4,
},
output: `4+1`,
},
// boolean
{
input: '{{bool}}',
params: {
bool: false,
},
output: `false`,
},
// null
{
input: '{{val}}',
params: {
val: null,
},
output: `null`,
},
// undefined
{
input: "window[{{l}}??'dataLayer']=window[{{l}}??'dataLayer']||[];",
output: `window[undefined??'dataLayer']=window[undefined??'dataLayer']||[];`,
},
])(
'should replace the input and stringify it',
({ input, output, params }) => {
expect(formatCode(input, params)).toEqual(output);
},
);
});
});
2 changes: 1 addition & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function formatUrl(

export function formatCode(code: string, args?: Inputs) {
return code.replace(/{{(.*?)}}/g, (match) => {
return args?.[match.split(/{{|}}/).filter(Boolean)[0]];
return JSON.stringify(args?.[match.split(/{{|}}/).filter(Boolean)[0]]);
});
}

Expand Down