Skip to content

Commit

Permalink
Style: Reformat code using ESLint ruleset refs #CCM-6
Browse files Browse the repository at this point in the history
  • Loading branch information
literat committed Nov 26, 2021
1 parent 5b4756d commit d9d96b9
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 20 deletions.
3 changes: 2 additions & 1 deletion examples/functions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable no-unused-vars */
/**
* Global function to reset cookie for the examples only
*/
function removeCookieAndReload() {
document.cookie = `lmc_ccm=; Max-Age=0; path=/; domain=${location.hostname}`;
document.cookie = `lmc_ccm=; Max-Age=0; path=/; domain=${window.location.hostname}`;
}
4 changes: 2 additions & 2 deletions scripts/build.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-console */
const { build } = require('esbuild');
const fs = require('fs');
const replace = require('replace-in-file');

// iife
Expand Down Expand Up @@ -43,7 +43,7 @@ build({
const options = {
files: './dist/LmcCookieConsentManager.cjs',
from: /0 && \(module.exports = {}\);/gm,
to: `module.exports = LmcCookieConsentManager_default;`,
to: 'module.exports = LmcCookieConsentManager_default;',
};

replace(options)
Expand Down
1 change: 1 addition & 0 deletions scripts/readme-replace-version.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-console */
const replace = require('replace-in-file');
const packageJson = require('../package.json');
// TODO: match just major version after 1.0 release
Expand Down
1 change: 1 addition & 0 deletions scripts/serve.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-console */
const { serve } = require('esbuild');

// Start esbuild's server on a random local port
Expand Down
29 changes: 19 additions & 10 deletions src/LmcCookieConsentManager.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-unused-vars */
import 'vanilla-cookieconsent';
import { nanoid } from 'nanoid';

Expand Down Expand Up @@ -29,17 +30,25 @@ const defaultOptions = {
* @param {string} serviceName - Identifier of the source service (website/application). Must be provided.
* @param {Object} [args] - Options for cookie consent manager
* @param {string} [args.defaultLang] - Default language. Must be one of predefined languages.
* @param {boolean} [args.autodetectLang] - Autodetect language from the value of `<html lang="...">`
* @param {?string} [args.consentCollectorApiUrl] - URL of the API where user consent information should be sent. Null to disable.
* @param {boolean} [args.autodetectLang] - Autodetect language from the browser
* @param {?string} [args.consentCollectorApiUrl] - URL of the API where user consent information should be sent.
* Null to disable.
* @param {function} [args.onFirstAccept] - Callback to be executed right after any consent is just accepted
* @param {function} [args.onFirstAcceptOnlyNecessary] - Callback to be executed right after only necessary cookies are accepted
* @param {function} [args.onFirstAcceptOnlyNecessary] - Callback to be executed right after only necessary cookies
* are accepted
* @param {function} [args.onFirstAcceptAll] - Callback to be executed right after all cookies are accepted
* @param {function} [args.onAccept] - Callback to be executed when any consent is detected (either given right now or already saved previously)
* @param {function} [args.onAcceptOnlyNecessary] - Callback to be executed when consent with only necessary cookies is detected (either given right now or already saved previously)
* @param {function} [args.onAcceptAll] - Callback to be executed when consent with all cookies is detected (either given right now or already saved previously)
* @param {array} [args.companyNames] - Array of strings with company names. Adjust only when the consent needs to be given to multiple companies.
* @param {Object} [args.config] - Override default config. See https://github.com/orestbida/cookieconsent/blob/master/Readme.md#all-available-options
* @returns {Object} Instance of the underlying CookieConsent component. For available API, see https://github.com/orestbida/cookieconsent#apis--configuration-parameters
* @param {function} [args.onAccept] - Callback to be executed when any consent is detected (either given right now
* or already saved previously)
* @param {function} [args.onAcceptOnlyNecessary] - Callback to be executed when consent with only necessary cookies.
* is detected (either given right now or already saved previously)
* @param {function} [args.onAcceptAll] - Callback to be executed when consent with all cookies is detected
* (either given right now or already saved previously)
* @param {array} [args.companyNames] - Array of strings with company names. Adjust only when the consent needs
* to be given to multiple companies.
* @param {Object} [args.config] - Override default config.
* See https://github.com/orestbida/cookieconsent/blob/master/Readme.md#all-available-options
* @returns {Object} Instance of the underlying CookieConsent component.
* For available API, see https://github.com/orestbida/cookieconsent#apis--configuration-parameters
*/
const LmcCookieConsentManager = (serviceName, args) => {
if (!serviceName || serviceName === '' || typeof serviceName !== 'string') {
Expand Down Expand Up @@ -113,7 +122,7 @@ const LmcCookieConsentManager = (serviceName, args) => {
const cookieData = cookieConsent.get('data');
if (cookieData === null || !('uid' in cookieData)) {
cookieConsent.set('data', {
value: { serviceName: serviceName, uid: nanoid() },
value: { serviceName, uid: nanoid() },
mode: 'update',
});
}
Expand Down
12 changes: 6 additions & 6 deletions src/consentCollector.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
* @param {Object} cookieConsent
* @param {boolean} acceptedOnlyNecessary
*/
export const submitConsent = (consentCollectorApiUrl, cookieConsent, acceptedOnlyNecessary) => {
function submitConsent(consentCollectorApiUrl, cookieConsent, acceptedOnlyNecessary) {
const payload = buildPayload(cookieConsent, acceptedOnlyNecessary);

postDataToApi(consentCollectorApiUrl, payload);
};
}

/**
* @param {Object} cookieConsent
* @param {boolean} acceptedOnlyNecessary
* @returns {Object}
*/
const buildPayload = (cookieConsent, acceptedOnlyNecessary) => {
function buildPayload(cookieConsent, acceptedOnlyNecessary) {
const cookieData = cookieConsent.get('data');
const acceptedCategories = cookieConsent.get('level');
// TODO: read actual categories once following is implemented in vanilla-cookieconsent:
Expand All @@ -38,14 +38,14 @@ const buildPayload = (cookieConsent, acceptedOnlyNecessary) => {
},
},
};
};
}

/**
* @param {string} apiUrl
* @param {Object} payload
* @return {Promise<any>}
*/
const postDataToApi = async (apiUrl, payload) => {
async function postDataToApi(apiUrl, payload) {
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
Expand All @@ -56,6 +56,6 @@ const postDataToApi = async (apiUrl, payload) => {
});

return response.json();
};
}

export default submitConsent;
3 changes: 2 additions & 1 deletion src/init.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import LmcCookieConsentManager from './LmcCookieConsentManager';

(function () {
(function initialize() {
const init = 'initLmcCookieConsentManager';
/**
* Make LmcCookieConsent object accessible globally
*/
if (typeof window[init] !== 'function') {
/** @inheritdoc */
window[init] = LmcCookieConsentManager;
}
})();

0 comments on commit d9d96b9

Please sign in to comment.