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 1 commit
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
12 changes: 8 additions & 4 deletions src/third-parties/google-analytics/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@
"scripts": [
{
"url": "https://www.googletagmanager.com/gtag/js",
"params": ["id", "l"],
"optionalParam": ["l"],
"params": ["id"],
"optionalParams": {
"l": null
},
"strategy": "worker",
"location": "head",
"action": "append",
"key": "gtag"
},
{
"code": "window[{{l}}??'dataLayer']=window[{{l}}??'dataLayer']||[];window[{{l}}??'dataLayer'].push({'js':new Date()});window[{{l}}??'dataLayer'].push({'config':{{id}}})",
"code": "window[{{l}}]=window[{{l}}]||[];window[{{l}}].push({'js':new Date()});window[{{l}}].push({'config':{{id}}})",
"params": ["id"],
"optionalParam": ["l"],
"optionalParams": {
"l": "dataLayer"
},
"strategy": "worker",
"location": "head",
"action": "append",
Expand Down
12 changes: 8 additions & 4 deletions src/third-parties/google-tag-manager/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@
"scripts": [
{
"url": "https://www.googletagmanager.com/gtm.js",
"params": ["id", "l"],
"optionalParam": ["l"],
"params": ["id"],
"optionalParams": {
"l": null
},
"strategy": "worker",
"location": "head",
"action": "append",
"key": "gtm"
},
{
"code": "window[{{l}}??'dataLayer']=window[{{l}}??'dataLayer']||[];window[{{l}}??'dataLayer'].push({'gtm.start':new Date().getTime(),event:'gtm.js'});",
"optionalParam": ["l"],
"code": "window[{{l}}]=window[{{l}}]||[];window[{{l}}].push({'gtm.start':new Date().getTime(),event:'gtm.js'});",
"optionalParams": {
"l": "dataLayer"
},
"strategy": "worker",
"location": "head",
"action": "append",
Expand Down
2 changes: 1 addition & 1 deletion src/types/type-declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export type HtmlAttributes = {

type ScriptBase = {
params?: Array<string>;
optionalParam?: Array<string>;
optionalParams?: Record<string, string | number | undefined>;
huang-julien marked this conversation as resolved.
Show resolved Hide resolved
strategy: ScriptStrategy;
location: ScriptLocation;
action: ScriptAction;
Expand Down
58 changes: 44 additions & 14 deletions src/utils/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,26 @@ describe('Utils', () => {
const newUrl = formatUrl(oldUrl, requiredParams, args);
expect(newUrl).toEqual('https://example.com/?unit=imperial&type=main');
});

it('should add default value', () => {
const oldUrl = 'https://example.com';
const requiredParams = ['unit', 'type'];
const args = {
unit: 'imperial',
};
const optionalParams = {
type: 'main',
};

const newUrl = formatUrl(
oldUrl,
requiredParams,
args,
undefined,
optionalParams,
);
expect(newUrl).toEqual('https://example.com/?unit=imperial&type=main');
});
});

describe('createHtml', () => {
Expand Down Expand Up @@ -231,15 +251,16 @@ describe('Utils', () => {
expect(result.scripts).toEqual(undefined);
});
});

describe('formatCode', () => {
it.each([
const inputs = [
// string
{
input: "window[{{l}}??'dataLayer']=window[{{l}}??'dataLayer']||[];",
input: 'window[{{l}}]=window[{{l}}]||[];',
params: {
l: 'some-datalayer',
},
output: `window["some-datalayer"??'dataLayer']=window["some-datalayer"??'dataLayer']||[];`,
output: `window["some-datalayer"]=window["some-datalayer"]||[];`,
},
// number
{
Expand All @@ -257,24 +278,33 @@ describe('Utils', () => {
},
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']||[];`,
input: 'window[{{l}}]=window[{{l}}]||[];',
output: `window[undefined]=window[undefined]||[];`,
},
])(
];

it.each(inputs)(
'should replace the input and stringify it',
({ input, output, params }) => {
expect(formatCode(input, params)).toEqual(output);
},
);

it.each(inputs)(
'should replace the input and stringify it with the default value',
({ input, output, params }) => {
expect(formatCode(input, undefined, params)).toEqual(output);
},
);

it('should replace the input and stringify it with the default value', () => {
const input = 'window[{{l}}]=window[{{l}}]||[];';

expect(
formatCode(input, { l: 'test' }, { l: 'dataLayer' }),
).toMatchInlineSnapshot(`"window["test"]=window["test"]||[];"`);
});
});
});
13 changes: 11 additions & 2 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export function formatUrl(
params?: string[],
args?: Inputs,
slug?: Inputs,
optionalParams?: Inputs,
) {
const newUrl =
slug && Object.keys(slug).length > 0
Expand All @@ -39,15 +40,23 @@ export function formatUrl(
if (params && args) {
params.forEach((param: string) => {
if (args[param]) newUrl.searchParams.set(param, args[param]);
else if (optionalParams?.[param]) {
newUrl.searchParams.set(param, optionalParams?.[param]);
}
});
}

return newUrl.toString();
}

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

Expand Down