Skip to content
Open
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
31 changes: 15 additions & 16 deletions src/consent-string.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ const { vendorVersionMap } = require('./utils/definitions');
* Regular expression for validating
*/
const consentLanguageRegexp = /^[a-z]{2}$/;
let cachedString;

class ConsentString {
constructor(baseString = null) {
this.cachedString = baseString;
this.maxVendorId = 0;
this.created = new Date();
this.lastUpdated = new Date();
Expand All @@ -24,7 +24,6 @@ class ConsentString {

// Decode the base string
if (baseString) {
cachedString = baseString;
Object.assign(this, decodeConsentString(baseString));
}
}
Expand All @@ -36,8 +35,8 @@ class ConsentString {
* check for cached string that was passed in. This avoids having to
* decode the consent string and even to have a vendorlist
*/
if (cachedString && !updateDate) {
retr = cachedString;
if (this.cachedString && !updateDate) {
retr = this.cachedString;
} else {
if (!this.vendorList) {
throw new Error('ConsentString - A vendor list is required to encode a consent string');
Expand All @@ -61,15 +60,15 @@ class ConsentString {
vendorListVersion: this.vendorListVersion,
});

cachedString = retr;
this.cachedString = retr;
}
return retr;
}
getLastUpdated() {
return this.lastUpdated;
}
setLastUpdated(date = null) {
cachedString = '';
this.cachedString = null;
if (date) {
this.lastUpdated = new Date(date);
} else {
Expand All @@ -80,7 +79,7 @@ class ConsentString {
return this.created;
}
setCreated(date = null) {
cachedString = '';
this.cachedString = null;
if (date) {
this.created = new Date(date);
} else {
Expand Down Expand Up @@ -143,7 +142,7 @@ class ConsentString {

// does a vendorList already exist and is it a different version
if (!this.vendorList || this.vendorListVersion !== vendorList.vendorListVersion) {
cachedString = '';
this.cachedString = null;
// Cloning the GVL
// It's important as we might transform it and don't want to modify objects that we do not own
this.vendorList = {
Expand All @@ -166,7 +165,7 @@ class ConsentString {
}
setCmpId(id) {
if (id !== this.cmpId) {
cachedString = '';
this.cachedString = null;
this.cmpId = id;
}
}
Expand All @@ -175,7 +174,7 @@ class ConsentString {
}
setCmpVersion(version) {
if (version !== this.cmpVersion) {
cachedString = '';
this.cachedString = null;
this.cmpVersion = version;
}
}
Expand All @@ -184,7 +183,7 @@ class ConsentString {
}
setConsentScreen(screenId) {
if (screenId !== this.consentScreen) {
cachedString = '';
this.cachedString = null;
this.consentScreen = screenId;
}
}
Expand All @@ -197,15 +196,15 @@ class ConsentString {
}

if (language !== this.consentLanguage) {
cachedString = '';
this.cachedString = null;
this.consentLanguage = language;
}
}
getConsentLanguage() {
return this.consentLanguage;
}
setPurposesAllowed(purposeIds) {
cachedString = '';
this.cachedString = null;
this.allowedPurposeIds = purposeIds;
}
getPurposesAllowed() {
Expand All @@ -214,7 +213,7 @@ class ConsentString {
setPurposeAllowed(purposeId, value) {
const purposeIndex = this.allowedPurposeIds.indexOf(purposeId);

cachedString = '';
this.cachedString = null;

if (value === true) {
if (purposeIndex === -1) {
Expand All @@ -230,7 +229,7 @@ class ConsentString {
return this.allowedPurposeIds.indexOf(purposeId) !== -1;
}
setVendorsAllowed(vendorIds) {
cachedString = '';
this.cachedString = null;
this.allowedVendorIds = vendorIds;
}
getVendorsAllowed() {
Expand All @@ -239,7 +238,7 @@ class ConsentString {
setVendorAllowed(vendorId, value) {
const vendorIndex = this.allowedVendorIds.indexOf(vendorId);

cachedString = '';
this.cachedString = null;
if (value === true) {
if (vendorIndex === -1) {
this.allowedVendorIds.push(vendorId);
Expand Down
17 changes: 17 additions & 0 deletions test/consent-string.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,21 @@ describe('ConsentString', function () {
]);
});
});

it('two object do not share the same cache', function () {
const consentString1 = new ConsentString();
consentString1.setGlobalVendorList(vendorList);
Object.assign(consentString1, consentData);
consentString1.setCreated(new Date('2018-07-10 PDT'));

const consentString2 = new ConsentString();
consentString2.setGlobalVendorList(vendorList);
Object.assign(consentString2, consentData);
consentString1.setCreated(new Date('2018-07-12 PDT'));

expect(consentString1.getConsentString(false))
.to
.not
.equal(consentString2.getConsentString(false));
});
});