Skip to content

Commit

Permalink
refactor: fix code spacing and rename _log to debug
Browse files Browse the repository at this point in the history
  • Loading branch information
orestbida committed Mar 3, 2024
1 parent 7716d62 commit e64b426
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 40 deletions.
2 changes: 1 addition & 1 deletion rollup-full.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export const terserPlugin = terser({
},
compress: {
passes: 3,
pure_funcs: [ '_log', 'console.log']
pure_funcs: [ 'debug', 'console.log']
}
});

Expand Down
14 changes: 7 additions & 7 deletions src/core/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
appendChild,
addClass,
removeClass,
_log,
debug,
getCurrentCategoriesState,
elContains,
setAcceptedCategories,
Expand Down Expand Up @@ -212,7 +212,7 @@ export const show = (createModal) => {
focus(globalObj._dom._cmDivTabindex);
}, 100);

_log('CookieConsent [TOGGLE]: show consentModal');
debug('CookieConsent [TOGGLE]: show consentModal');

fireEvent(globalObj._customEvents._onModalShow, CONSENT_MODAL_NAME);
};
Expand Down Expand Up @@ -245,7 +245,7 @@ export const hide = () => {
focus(_state._lastFocusedElemBeforeModal);
_state._lastFocusedElemBeforeModal = null;

_log('CookieConsent [TOGGLE]: hide consentModal');
debug('CookieConsent [TOGGLE]: hide consentModal');

fireEvent(_customEvents._onModalHide, CONSENT_MODAL_NAME);
};
Expand Down Expand Up @@ -283,7 +283,7 @@ export const showPreferences = () => {
focus(globalObj._dom._pmDivTabindex);
}, 100);

_log('CookieConsent [TOGGLE]: show preferencesModal');
debug('CookieConsent [TOGGLE]: show preferencesModal');

fireEvent(globalObj._customEvents._onModalShow, PREFERENCES_MODAL_NAME);
};
Expand Down Expand Up @@ -354,7 +354,7 @@ export const hidePreferences = () => {
state._lastFocusedElemBeforeModal = null;
}

_log('CookieConsent [TOGGLE]: hide preferencesModal');
debug('CookieConsent [TOGGLE]: hide preferencesModal');

fireEvent(globalObj._customEvents._onModalHide, PREFERENCES_MODAL_NAME);
};
Expand Down Expand Up @@ -499,7 +499,7 @@ export const setCookieData = (props) => {
set = true;
}
}
}else if ((sameType || !cookieData) && cookieData !== newData) {
} else if ((sameType || !cookieData) && cookieData !== newData) {
cookieData = newData;
set = true;
}
Expand Down Expand Up @@ -606,7 +606,7 @@ const retrieveState = () => {
state._invalidConsent && (localStorageManager._removeItem(config.cookie.name));
}

_log('CookieConsent [STATUS] valid consent:', !state._invalidConsent);
debug('CookieConsent [STATUS] valid consent:', !state._invalidConsent);
retrieveEnabledCategoriesAndServices();

/**
Expand Down
10 changes: 5 additions & 5 deletions src/core/config-init.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { globalObj } from './global';
import { _log, getKeys, isObject, retrieveScriptElements, fetchCategoriesAndServices } from '../utils/general';
import { debug, getKeys, isObject, retrieveScriptElements, fetchCategoriesAndServices } from '../utils/general';
import { OPT_OUT_MODE } from '../utils/constants';
import { resolveCurrentLanguageCode, setCurrentLanguageCode } from '../utils/language';

Expand Down Expand Up @@ -102,10 +102,10 @@ export const setConfig = (userConfig) => {
if (isObject(userCookieConfig))
config.cookie = {...cookie, ...userCookieConfig};

_log('CookieConsent [CONFIG]: configuration:', userConfig);
_log('CookieConsent [CONFIG]: autoClearCookies:', config.autoClearCookies);
_log('CookieConsent [CONFIG]: revision enabled:', state._revisionEnabled);
_log('CookieConsent [CONFIG]: manageScriptTags:', config.manageScriptTags);
debug('CookieConsent [CONFIG]: configuration:', userConfig);
debug('CookieConsent [CONFIG]: autoClearCookies:', config.autoClearCookies);
debug('CookieConsent [CONFIG]: revision enabled:', state._revisionEnabled);
debug('CookieConsent [CONFIG]: manageScriptTags:', config.manageScriptTags);

fetchCategoriesAndServices(allCategoryNames);
retrieveScriptElements();
Expand Down
10 changes: 5 additions & 5 deletions src/core/modals/consentModal.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { globalObj } from '../global';

import {
_log,
debug,
createNode,
addClass,
addClassCm,
Expand Down Expand Up @@ -119,7 +119,7 @@ export const createConsentModal = (api, createMainContainer) => {
addClassCm(dom._cmCloseIconBtn, 'btn');
addClassCm(dom._cmCloseIconBtn, 'btn--close');
addEvent(dom._cmCloseIconBtn, CLICK_EVENT, () => {
_log('CookieConsent [ACCEPT]: necessary');
debug('CookieConsent [ACCEPT]: necessary');
acceptAndHide([]);
});
appendChild(dom._cmBody, dom._cmCloseIconBtn);
Expand Down Expand Up @@ -180,7 +180,7 @@ export const createConsentModal = (api, createMainContainer) => {
setAttribute(dom._cmAcceptAllBtn, DATA_ROLE, 'all');

addEvent(dom._cmAcceptAllBtn, CLICK_EVENT, () => {
_log('CookieConsent [ACCEPT]: all');
debug('CookieConsent [ACCEPT]: all');
acceptAndHide('all');
});
}
Expand All @@ -196,7 +196,7 @@ export const createConsentModal = (api, createMainContainer) => {
setAttribute(dom._cmAcceptNecessaryBtn, DATA_ROLE, 'necessary');

addEvent(dom._cmAcceptNecessaryBtn, CLICK_EVENT, () => {
_log('CookieConsent [ACCEPT]: necessary');
debug('CookieConsent [ACCEPT]: necessary');
acceptAndHide([]);
});
}
Expand Down Expand Up @@ -269,7 +269,7 @@ export const createConsentModal = (api, createMainContainer) => {
if (!state._consentModalExists) {
state._consentModalExists = true;

_log('CookieConsent [HTML] created', CONSENT_MODAL_NAME);
debug('CookieConsent [HTML] created', CONSENT_MODAL_NAME);

fireEvent(globalObj._customEvents._onModalReady, CONSENT_MODAL_NAME, dom._cm);
createMainContainer(api);
Expand Down
8 changes: 4 additions & 4 deletions src/core/modals/preferencesModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
fireEvent,
getSvgIcon,
handleFocusTrap,
_log
debug
} from '../../utils/general';

import { guiManager } from '../../utils/gui-manager';
Expand Down Expand Up @@ -474,7 +474,7 @@ export const createPreferencesModal = (api, createMainContainer) => {
if (!state._preferencesModalExists) {
state._preferencesModalExists = true;

_log('CookieConsent [HTML] created', PREFERENCES_MODAL_NAME);
debug('CookieConsent [HTML] created', PREFERENCES_MODAL_NAME);

fireEvent(globalObj._customEvents._onModalReady, PREFERENCES_MODAL_NAME, dom._pm);
createMainContainer(api);
Expand Down Expand Up @@ -590,10 +590,10 @@ function createToggleLabel(label, value, sCurrentCategoryObject, isService, cate
if (isService) {
const enabledServices = state._acceptedServices[categoryName];
toggle.checked = sCurrentCategoryObject.readOnly || elContains(enabledServices, value);
}else if (elContains(state._acceptedCategories, value)) {
} else if (elContains(state._acceptedCategories, value)) {
toggle.checked = true;
}
}else if (sCurrentCategoryObject.readOnly || sCurrentCategoryObject.enabled) {
} else if (sCurrentCategoryObject.readOnly || sCurrentCategoryObject.enabled) {
toggle.checked = true;
}

Expand Down
6 changes: 3 additions & 3 deletions src/utils/cookies.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { OPT_OUT_MODE, OPT_IN_MODE } from './constants';
import { manageExistingScripts } from './scripts';

import {
_log,
debug,
indexOf,
uuidv4,
getRemainingExpirationTimeMS,
Expand Down Expand Up @@ -269,7 +269,7 @@ export const setCookie = (useRemainingExpirationTime) => {
? localStorageManager._setItem(name, value)
: document.cookie = cookieStr;

_log('CookieConsent [SET_COOKIE]: ' + name + ':', globalObj._state._savedCookieContent);
debug('CookieConsent [SET_COOKIE]: ' + name + ':', globalObj._state._savedCookieContent);
};

/**
Expand Down Expand Up @@ -335,7 +335,7 @@ export const eraseCookiesHelper = (cookies, customPath, customDomain) => {
if (isWwwSubdomain)
erase(cookieName, mainDomain);

_log('CookieConsent [AUTOCLEAR]: deleting cookie: "' + cookieName + '" path: "' + path + '" domain:', domain);
debug('CookieConsent [AUTOCLEAR]: deleting cookie: "' + cookieName + '" path: "' + path + '" domain:', domain);
}
};

Expand Down
20 changes: 8 additions & 12 deletions src/utils/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,10 @@ import {

/**
* Helper console.log function
* @param {Object} printMsg
* @param {Object} [optionalParam]
* @param {any} [params]
*/
export const _log = (printMsg, optionalParam) => {
console.log(printMsg, optionalParam !== undefined
? optionalParam
: ' '
);
export const debug = (...params) => {
console.log(...params);
};

/**
Expand Down Expand Up @@ -324,7 +320,7 @@ export const resolveEnabledCategories = (categories, excludedCategories) => {

if (isArray(categories)) {
enabledCategories.push(...categories);
}else if (isString(categories)) {
} else if (isString(categories)) {
enabledCategories = categories === 'all'
? _allCategoryNames
: [categories];
Expand Down Expand Up @@ -460,7 +456,7 @@ export const updateModalToggles = (service, category) => {
}
}
}
}else if (isArray(service)) {
} else if (isArray(service)) {
for (let serviceName of allServiceNames) {
const validService = elContains(service, serviceName);
validService && _enabledServices[category].push(serviceName);
Expand Down Expand Up @@ -921,7 +917,7 @@ export const fireEvent = (eventName, modalName, modal) => {

if (eventName === events._onModalShow) {
isFunction(_onModalShow) && _onModalShow(modalParams);
}else if (eventName === events._onModalHide) {
} else if (eventName === events._onModalHide) {
isFunction(_onModalHide) && _onModalHide(modalParams);
} else {
modalParams.modal = modal;
Expand All @@ -938,9 +934,9 @@ export const fireEvent = (eventName, modalName, modal) => {

if (eventName === events._onFirstConsent) {
isFunction(_onFirstConsent) && _onFirstConsent(deepCopy(params));
}else if (eventName === events._onConsent) {
} else if (eventName === events._onConsent) {
isFunction(_onConsent) && _onConsent(deepCopy(params));
}else {
} else {
params.changedCategories = globalObj._state._lastChangedCategoryNames;
params.changedServices = globalObj._state._lastChangedServices;
isFunction(_onChange) && _onChange(deepCopy(params));
Expand Down
6 changes: 3 additions & 3 deletions src/utils/language.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { globalObj } from '../core/global';
import {
_log,
debug,
elContains,
fetchJson,
addClass,
Expand Down Expand Up @@ -74,7 +74,7 @@ export const resolveCurrentLanguageCode = () => {
const autoDetect = globalObj._state._userConfig.language.autoDetect;

if (autoDetect) {
_log('CookieConsent [LANG]: autoDetect strategy: "' + autoDetect + '"');
debug('CookieConsent [LANG]: autoDetect strategy: "' + autoDetect + '"');

const detectionStrategies = {
browser: getBrowserLanguageCode(),
Expand Down Expand Up @@ -128,7 +128,7 @@ export const loadTranslationData = async (desiredLanguageCode) => {
state._currentTranslation = translationData;
setCurrentLanguageCode(currentLanguageCode);

_log('CookieConsent [LANG]: set language: "' + currentLanguageCode + '"');
debug('CookieConsent [LANG]: set language: "' + currentLanguageCode + '"');

return true;
};
Expand Down

0 comments on commit e64b426

Please sign in to comment.