lukaskupczyk/react-hook-consent

View on GitHub
src/core/cookies/remove.ts

Summary

Maintainability
A
55 mins
Test Coverage
A
100%
import { Cookie } from '../../Context';

export function removeCookies(cookies?: Cookie[]) {
    if (!cookies) {
        return;
    }

    // get all cookies
    const documentCookies = document.cookie.split(';');

    for (const cookie of cookies) {
        // remove cookie by name
        if (typeof cookie.pattern === 'string') {
            document.cookie = cookie.pattern + '=;expires=Thu, 01 Jan 1970 00:00:00 GMT';
            continue;
        }

        // remove cookies by pattern
        for (const documentCookie of documentCookies) {
            if (!cookie.pattern.test(documentCookie)) {
                continue;
            }

            console.log('remove cookie', documentCookie);

            document.cookie = documentCookie + '=;expires=Thu, 01 Jan 1970 00:00:00 GMT';
        }
    }
}