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

Add support for new GTM IDs starting with 'G-' #427

Merged
merged 3 commits into from
May 27, 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
8 changes: 5 additions & 3 deletions src/assert-is-gtm-id.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/** GTM Container ID pattern. */
export const GTM_ID_PATTERN: RegExp = /^GTM-[0-9A-Z]+$/;
export const GTM_ID_PATTERN: RegExp = /^(GTM|G)-[0-9A-Z]+$/;

/**
* Assert that the given id is a valid GTM Container ID.
*
* Tested against pattern: `/^GTM-[0-9A-Z]+$/`.
* Tested against pattern: `/^(GTM|G)-[0-9A-Z]+$/`.
*
* @param id A GTM Container ID.
*/
Expand All @@ -14,7 +14,9 @@ export function assertIsGtmId(id: string): asserts id {
.toUpperCase()
.replace(/.*-|[^0-9A-Z]/g, '');
const suggestionText: string =
suggestion.length === 0 ? '' : ` Did you mean 'GTM-${suggestion}'?`;
suggestion.length === 0
? ''
: ` Did you mean 'GTM-${suggestion}' or 'G-${suggestion}'?`;
throw new Error(
`'${id}' is not a valid GTM-ID (${GTM_ID_PATTERN}).${suggestionText}`,
);
Expand Down
18 changes: 12 additions & 6 deletions tests/assert-is-gtm-id.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,48 @@ import { assertIsGtmId } from '../src/index';
describe('assert-is-gtm-id', () => {
test('valid', () => {
expect(() => assertIsGtmId('GTM-DEMO')).not.toThrow(
new Error("'GTM-DEMO' is not a valid GTM-ID (/^GTM-[0-9A-Z]+$/)."),
new Error("'GTM-DEMO' is not a valid GTM-ID (/^(GTM|G)-[0-9A-Z]+$/)."),
);
});

test('valid', () => {
expect(() => assertIsGtmId('G-DEMO')).not.toThrow(
new Error("'G-DEMO' is not a valid GTM-ID (/^(GTM|G)-[0-9A-Z]+$/)."),
);
});

test('invalid - empty string', () => {
expect(() => assertIsGtmId('')).toThrow(
new Error("'' is not a valid GTM-ID (/^GTM-[0-9A-Z]+$/)."),
new Error("'' is not a valid GTM-ID (/^(GTM|G)-[0-9A-Z]+$/)."),
);
});

test('invalid - wrong prefix', () => {
expect(() => assertIsGtmId('GA-DEMO')).toThrow(
new Error(
"'GA-DEMO' is not a valid GTM-ID (/^GTM-[0-9A-Z]+$/). Did you mean 'GTM-DEMO'?",
"'GA-DEMO' is not a valid GTM-ID (/^(GTM|G)-[0-9A-Z]+$/). Did you mean 'GTM-DEMO' or 'G-DEMO'?",
),
);
});

test('invalid - non alphanumeric', () => {
expect(() => assertIsGtmId('GTM-@')).toThrow(
new Error("'GTM-@' is not a valid GTM-ID (/^GTM-[0-9A-Z]+$/)."),
new Error("'GTM-@' is not a valid GTM-ID (/^(GTM|G)-[0-9A-Z]+$/)."),
);
});

test('invalid - wrong type', () => {
expect(() => assertIsGtmId(42 as unknown as string)).toThrow(
new Error(
"'42' is not a valid GTM-ID (/^GTM-[0-9A-Z]+$/). Did you mean 'GTM-42'?",
"'42' is not a valid GTM-ID (/^(GTM|G)-[0-9A-Z]+$/). Did you mean 'GTM-42' or 'G-42'?",
),
);
});

test('invalid - passed nothing', () => {
expect(() => assertIsGtmId(undefined as unknown as string)).toThrow(
new Error(
"'undefined' is not a valid GTM-ID (/^GTM-[0-9A-Z]+$/). Did you mean 'GTM-UNDEFINED'?",
"'undefined' is not a valid GTM-ID (/^(GTM|G)-[0-9A-Z]+$/). Did you mean 'GTM-UNDEFINED' or 'G-UNDEFINED'?",
),
);
});
Expand Down