Skip to content
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
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,13 @@ var orejimeConfig = {
cookies: [
'_ga',
'_gat',
'_gid',
'__utma',
'__utmb',
'__utmc',
'__utmt',
'__utmz',
'_gat_gtag_' + GTM_UA,
'_gat_' + GTM_UA
'_gat_' + GTM_UA,

// Regexes can be provided as strings or RegExp objects to match names
// more broadly.
'/^_gat_.+$/i',
/^_gat_.+$/i,
new RegExp('^_gat_.+$', 'i')
],

// [optional]
Expand Down Expand Up @@ -148,6 +147,7 @@ var orejimeConfig = {
description: 'Example of an inline tracking script',
cookies: [
'inline-tracker',

// When deleting a cookie, Orejime will try to delete a cookie with the
// given name, the "/" path, and multiple domains (the current domain
// and `"." + current domain`).
Expand Down
8 changes: 3 additions & 5 deletions src/core/utils/deletePurposeCookies.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {PurposeCookie} from '../types';
import {deleteCookie, getCookieNames} from './cookies';
import escapeRegex from './escapeRegex';
import {toRegex} from './regexes';

export default (cookies: PurposeCookie[]) => {
const cookieNames = getCookieNames();
Expand All @@ -13,12 +13,10 @@ export default (cookies: PurposeCookie[]) => {
[pattern, path, domain] = pattern;
}

if (!(pattern instanceof RegExp)) {
pattern = new RegExp(`^${escapeRegex(pattern)}$`);
}
const regex = toRegex(pattern);

cookieNames
.filter((name) => (pattern as RegExp).test(name))
.filter((name) => regex.test(name))
.forEach((cookie) => {
deleteCookie(cookie, path, domain);
});
Expand Down
2 changes: 0 additions & 2 deletions src/core/utils/escapeRegex.ts

This file was deleted.

22 changes: 22 additions & 0 deletions src/core/utils/regexes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {toRegex} from './regexes';

describe('toRegex', () => {
test('should return a regex as-is', () => {
const regex = new RegExp('pattern', 'gi');
expect(toRegex(regex)).toEqual(regex);
});

test('should turn a pattern into a regex', () => {
expect(toRegex('/pattern/')).toEqual(new RegExp('pattern'));
expect(toRegex('/pattern/i')).toEqual(new RegExp('pattern', 'i'));

// The only flag allowed is `i`
expect(toRegex('/pattern/g')).not.toEqual(new RegExp('pattern', 'g'));
expect(toRegex('/pattern/gi')).not.toEqual(new RegExp('pattern', 'gi'));
});

test('should turn a string into a regex', () => {
expect(toRegex('pattern')).toEqual(new RegExp('^pattern$'));
expect(toRegex('^pattern$')).toEqual(new RegExp('^\\^pattern\\$$'));
});
});
27 changes: 27 additions & 0 deletions src/core/utils/regexes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Turns a pattern into a regex.
// If the string looks like a regex pattern, it is used as-is.
// Otherwise, a regex is build to match the string exactly.
export const toRegex = (pattern: string | RegExp) => {
if (pattern instanceof RegExp) {
return pattern;
}

if (pattern.startsWith('/')) {
// A regex to match a (simplistic) regex to match cookie names.
// `i` is the only relevant flag in that context.
const matches = /^\/(.*)\/(i?)$/.exec(pattern);

if (matches) {
try {
return new RegExp(matches[1], matches[2]);
} catch (e) {}
}
}

const escaped = pattern.replace(
/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g,
'\\$&'
);

return new RegExp(`^${escaped}$`);
};
Loading