@pubtech-ai/core 2.1.1 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.PurposeRestrictionVectorEncoder=void 0;var BitLength_js_1=require("../BitLength.js"),BooleanEncoder_js_1=require("./BooleanEncoder.js"),index_js_1=require("../../errors/index.js"),IntEncoder_js_1=require("./IntEncoder.js"),index_js_2=require("../../model/index.js"),PurposeRestrictionVectorEncoder=function(){function e(){}return e.encode=function(e){var n=IntEncoder_js_1.IntEncoder.encode(e.numRestrictions,BitLength_js_1.BitLength.numRestrictions);if(!e.isEmpty()){var t=function(n,t){for(var r=n+1;r<=t;r++)if(e.gvl.vendorIds.has(r))return r;return n};e.getRestrictions().forEach((function(r){n+=IntEncoder_js_1.IntEncoder.encode(r.purposeId,BitLength_js_1.BitLength.purposeId),n+=IntEncoder_js_1.IntEncoder.encode(r.restrictionType,BitLength_js_1.BitLength.restrictionType);for(var o=e.getVendors(r),i=o.length,s=0,d=0,_="",c=0;c<i;c++){var B=o[c];if(0===d&&(s++,d=B),c===i-1||o[c+1]>t(B,o[i-1])){var g=!(B===d);_+=BooleanEncoder_js_1.BooleanEncoder.encode(g),_+=IntEncoder_js_1.IntEncoder.encode(d,BitLength_js_1.BitLength.vendorId),g&&(_+=IntEncoder_js_1.IntEncoder.encode(B,BitLength_js_1.BitLength.vendorId)),d=0}}n+=IntEncoder_js_1.IntEncoder.encode(s,BitLength_js_1.BitLength.numEntries),n+=_}))}return n},e.decode=function(e){var n=0,t=new index_js_2.PurposeRestrictionVector,r=IntEncoder_js_1.IntEncoder.decode(e.substr(n,BitLength_js_1.BitLength.numRestrictions),BitLength_js_1.BitLength.numRestrictions);n+=BitLength_js_1.BitLength.numRestrictions;for(var o=0;o<r;o++){var i=IntEncoder_js_1.IntEncoder.decode(e.substr(n,BitLength_js_1.BitLength.purposeId),BitLength_js_1.BitLength.purposeId);n+=BitLength_js_1.BitLength.purposeId;var s=IntEncoder_js_1.IntEncoder.decode(e.substr(n,BitLength_js_1.BitLength.restrictionType),BitLength_js_1.BitLength.restrictionType);n+=BitLength_js_1.BitLength.restrictionType;var d=new index_js_2.PurposeRestriction(i,s),_=IntEncoder_js_1.IntEncoder.decode(e.substr(n,BitLength_js_1.BitLength.numEntries),BitLength_js_1.BitLength.numEntries);n+=BitLength_js_1.BitLength.numEntries;for(var c=0;c<_;c++){var B=BooleanEncoder_js_1.BooleanEncoder.decode(e.substr(n,BitLength_js_1.BitLength.anyBoolean));n+=BitLength_js_1.BitLength.anyBoolean;var g=IntEncoder_js_1.IntEncoder.decode(e.substr(n,BitLength_js_1.BitLength.vendorId),BitLength_js_1.BitLength.vendorId);if(n+=BitLength_js_1.BitLength.vendorId,B){var h=IntEncoder_js_1.IntEncoder.decode(e.substr(n,BitLength_js_1.BitLength.vendorId),BitLength_js_1.BitLength.vendorId);if(n+=BitLength_js_1.BitLength.vendorId,h<g)throw new index_js_1.DecodingError("Invalid RangeEntry: endVendorId ".concat(h," is less than ").concat(g));for(var L=g;L<=h;L++)t.add(L,d)}else t.add(g,d)}}return t.bitLength=n,t},e}();exports.PurposeRestrictionVectorEncoder=PurposeRestrictionVectorEncoder;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.PurposeRestrictionVectorEncoder=void 0;var BitLength_js_1=require("../BitLength.js"),BooleanEncoder_js_1=require("./BooleanEncoder.js"),index_js_1=require("../../errors/index.js"),IntEncoder_js_1=require("./IntEncoder.js"),index_js_2=require("../../model/index.js"),PurposeRestrictionVectorEncoder=function(){function e(){}return e.encode=function(e){var n=IntEncoder_js_1.IntEncoder.encode(e.numRestrictions,BitLength_js_1.BitLength.numRestrictions);if(!e.isEmpty()){var t=Array.from(e.gvl.vendorIds),r=function(e,n){var r=t.indexOf(e);if(t.indexOf(n)-r>0){var o=t.indexOf(e+1);return t[o]}return e},o=new Map;e.getRestrictions().forEach((function(t){n+=IntEncoder_js_1.IntEncoder.encode(t.purposeId,BitLength_js_1.BitLength.purposeId),n+=IntEncoder_js_1.IntEncoder.encode(t.restrictionType,BitLength_js_1.BitLength.restrictionType);var i=e.getVendorsSet(t),s=0,d="";if(o.has(i)){var _=o.get(i);s=_.numEntries,d=_.rangeField}else{for(var c=Array.from(i).sort((function(e,n){return e-n})),g=c.length,B=0,h=0;h<g;h++){var L=c[h];if(0===B&&(s++,B=L),h===g-1||c[h+1]>r(L,c[g-1])){var j=!(L===B);d+=BooleanEncoder_js_1.BooleanEncoder.encode(j),d+=IntEncoder_js_1.IntEncoder.encode(B,BitLength_js_1.BitLength.vendorId),j&&(d+=IntEncoder_js_1.IntEncoder.encode(L,BitLength_js_1.BitLength.vendorId)),B=0}}o.set(i,{numEntries:s,rangeField:d})}n+=IntEncoder_js_1.IntEncoder.encode(s,BitLength_js_1.BitLength.numEntries),n+=d}))}return n},e.decode=function(e){var n=0,t=new index_js_2.PurposeRestrictionVector,r=IntEncoder_js_1.IntEncoder.decode(e.substr(n,BitLength_js_1.BitLength.numRestrictions),BitLength_js_1.BitLength.numRestrictions);n+=BitLength_js_1.BitLength.numRestrictions;for(var o=0;o<r;o++){var i=IntEncoder_js_1.IntEncoder.decode(e.substr(n,BitLength_js_1.BitLength.purposeId),BitLength_js_1.BitLength.purposeId);n+=BitLength_js_1.BitLength.purposeId;var s=IntEncoder_js_1.IntEncoder.decode(e.substr(n,BitLength_js_1.BitLength.restrictionType),BitLength_js_1.BitLength.restrictionType);n+=BitLength_js_1.BitLength.restrictionType;var d=new index_js_2.PurposeRestriction(i,s),_=IntEncoder_js_1.IntEncoder.decode(e.substr(n,BitLength_js_1.BitLength.numEntries),BitLength_js_1.BitLength.numEntries);n+=BitLength_js_1.BitLength.numEntries;for(var c=0;c<_;c++){var g=BooleanEncoder_js_1.BooleanEncoder.decode(e.substr(n,BitLength_js_1.BitLength.anyBoolean));n+=BitLength_js_1.BitLength.anyBoolean;var B=IntEncoder_js_1.IntEncoder.decode(e.substr(n,BitLength_js_1.BitLength.vendorId),BitLength_js_1.BitLength.vendorId);if(n+=BitLength_js_1.BitLength.vendorId,g){var h=IntEncoder_js_1.IntEncoder.decode(e.substr(n,BitLength_js_1.BitLength.vendorId),BitLength_js_1.BitLength.vendorId);if(n+=BitLength_js_1.BitLength.vendorId,h<B)throw new index_js_1.DecodingError("Invalid RangeEntry: endVendorId ".concat(h," is less than ").concat(B));for(var L=B;L<=h;L++)t.add(L,d)}else t.add(B,d)}}return t.bitLength=n,t},e}();exports.PurposeRestrictionVectorEncoder=PurposeRestrictionVectorEncoder;
@@ -14,6 +14,7 @@ export declare class PurposeRestrictionVector extends Cloneable<PurposeRestricti
14
14
  */
15
15
  private map;
16
16
  private gvl_;
17
+ private cachedVendorMap;
17
18
  private has;
18
19
  private isOkToHave;
19
20
  /**
@@ -24,6 +25,7 @@ export declare class PurposeRestrictionVector extends Cloneable<PurposeRestricti
24
25
  * @return {void}
25
26
  */
26
27
  add(vendorId: number, purposeRestriction: PurposeRestriction): void;
28
+ private createOrderedSetOfVendorIdsFromOne;
27
29
  /**
28
30
  * restrictPurposeToLegalBasis - adds all Vendors under a given Purpose Restriction
29
31
  *
@@ -42,6 +44,7 @@ export declare class PurposeRestrictionVector extends Cloneable<PurposeRestricti
42
44
  * @return {number[]} - Unique ID set of vendors
43
45
  */
44
46
  getVendors(purposeRestriction?: PurposeRestriction): number[];
47
+ getVendorsSet(purposeRestriction?: PurposeRestriction): Set<number>;
45
48
  getRestrictionType(vendorId: number, purposeId: number): RestrictionType | undefined;
46
49
  /**
47
50
  * vendorHasRestriction - determines whether a given Vendor ID is under a
@@ -1 +1 @@
1
- "use strict";var __extends=this&&this.__extends||function(){var t=function(e,r){return(t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r])})(e,r)};return function(e,r){if("function"!=typeof r&&null!==r)throw new TypeError("Class extends value "+String(r)+" is not a constructor or null");function o(){this.constructor=e}t(e,r),e.prototype=null===r?Object.create(r):(o.prototype=r.prototype,new o)}}(),__read=this&&this.__read||function(t,e){var r="function"==typeof Symbol&&t[Symbol.iterator];if(!r)return t;var o,s,i=r.call(t),n=[];try{for(;(void 0===e||e-- >0)&&!(o=i.next()).done;)n.push(o.value)}catch(t){s={error:t}}finally{try{o&&!o.done&&(r=i.return)&&r.call(i)}finally{if(s)throw s.error}}return n},__spreadArray=this&&this.__spreadArray||function(t,e,r){if(r||2===arguments.length)for(var o,s=0,i=e.length;s<i;s++)!o&&s in e||(o||(o=Array.prototype.slice.call(e,0,s)),o[s]=e[s]);return t.concat(o||Array.prototype.slice.call(e))};Object.defineProperty(exports,"__esModule",{value:!0}),exports.PurposeRestrictionVector=void 0;var PurposeRestriction_js_1=require("./PurposeRestriction.js"),RestrictionType_js_1=require("./RestrictionType.js"),Cloneable_js_1=require("../Cloneable.js"),PurposeRestrictionVector=function(t){function e(){var e=null!==t&&t.apply(this,arguments)||this;return e.bitLength=0,e.map=new Map,e}return __extends(e,t),e.prototype.has=function(t){return this.map.has(t)},e.prototype.isOkToHave=function(t,e,r){var o,s=!0;if(null===(o=this.gvl)||void 0===o?void 0:o.vendors){var i=this.gvl.vendors[r];if(i)if(t===RestrictionType_js_1.RestrictionType.NOT_ALLOWED)s=i.legIntPurposes.includes(e)||i.purposes.includes(e);else if(i.flexiblePurposes.length)switch(t){case RestrictionType_js_1.RestrictionType.REQUIRE_CONSENT:s=i.flexiblePurposes.includes(e)&&i.legIntPurposes.includes(e);break;case RestrictionType_js_1.RestrictionType.REQUIRE_LI:s=i.flexiblePurposes.includes(e)&&i.purposes.includes(e)}else s=!1;else s=!1}return s},e.prototype.add=function(t,e){if(this.isOkToHave(e.restrictionType,e.purposeId,t)){var r=e.hash;this.has(r)||(this.map.set(r,new Set),this.bitLength=0),this.map.get(r).add(t)}},e.prototype.restrictPurposeToLegalBasis=function(t){var e=Array.from(this.gvl.vendorIds),r=t.hash,o=e[e.length-1],s=__spreadArray([],__read(Array(o).keys()),!1).map((function(t){return t+1}));if(this.has(r))for(var i=1;i<=o;i++)this.map.get(r).add(i);else this.map.set(r,new Set(s)),this.bitLength=0},e.prototype.getVendors=function(t){var e=[];if(t){var r=t.hash;this.has(r)&&(e=Array.from(this.map.get(r)))}else{var o=new Set;this.map.forEach((function(t){t.forEach((function(t){o.add(t)}))})),e=Array.from(o)}return e.sort((function(t,e){return t-e}))},e.prototype.getRestrictionType=function(t,e){var r;return this.getRestrictions(t).forEach((function(t){t.purposeId===e&&(void 0===r||r>t.restrictionType)&&(r=t.restrictionType)})),r},e.prototype.vendorHasRestriction=function(t,e){for(var r=!1,o=this.getRestrictions(t),s=0;s<o.length&&!r;s++)r=e.isSameAs(o[s]);return r},e.prototype.getMaxVendorId=function(){var t=0;return this.map.forEach((function(e){t=Math.max(Array.from(e)[e.size-1],t)})),t},e.prototype.getRestrictions=function(t){var e=[];return this.map.forEach((function(r,o){t?r.has(t)&&e.push(PurposeRestriction_js_1.PurposeRestriction.unHash(o)):e.push(PurposeRestriction_js_1.PurposeRestriction.unHash(o))})),e},e.prototype.getPurposes=function(){var t=new Set;return this.map.forEach((function(e,r){t.add(PurposeRestriction_js_1.PurposeRestriction.unHash(r).purposeId)})),Array.from(t)},e.prototype.remove=function(t,e){var r=e.hash,o=this.map.get(r);o&&(o.delete(t),0==o.size&&(this.map.delete(r),this.bitLength=0))},Object.defineProperty(e.prototype,"gvl",{get:function(){return this.gvl_},set:function(t){var e=this;this.gvl_||(this.gvl_=t,this.map.forEach((function(t,r){var o=PurposeRestriction_js_1.PurposeRestriction.unHash(r);Array.from(t).forEach((function(r){e.isOkToHave(o.restrictionType,o.purposeId,r)||t.delete(r)}))})))},enumerable:!1,configurable:!0}),e.prototype.isEmpty=function(){return 0===this.map.size},Object.defineProperty(e.prototype,"numRestrictions",{get:function(){return this.map.size},enumerable:!1,configurable:!0}),e}(Cloneable_js_1.Cloneable);exports.PurposeRestrictionVector=PurposeRestrictionVector;
1
+ "use strict";var __extends=this&&this.__extends||function(){var e=function(t,r){return(e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r])})(t,r)};return function(t,r){if("function"!=typeof r&&null!==r)throw new TypeError("Class extends value "+String(r)+" is not a constructor or null");function o(){this.constructor=t}e(t,r),t.prototype=null===r?Object.create(r):(o.prototype=r.prototype,new o)}}(),__read=this&&this.__read||function(e,t){var r="function"==typeof Symbol&&e[Symbol.iterator];if(!r)return e;var o,n,s=r.call(e),i=[];try{for(;(void 0===t||t-- >0)&&!(o=s.next()).done;)i.push(o.value)}catch(e){n={error:e}}finally{try{o&&!o.done&&(r=s.return)&&r.call(s)}finally{if(n)throw n.error}}return i},__spreadArray=this&&this.__spreadArray||function(e,t,r){if(r||2===arguments.length)for(var o,n=0,s=t.length;n<s;n++)!o&&n in t||(o||(o=Array.prototype.slice.call(t,0,n)),o[n]=t[n]);return e.concat(o||Array.prototype.slice.call(t))};Object.defineProperty(exports,"__esModule",{value:!0}),exports.PurposeRestrictionVector=void 0;var PurposeRestriction_js_1=require("./PurposeRestriction.js"),RestrictionType_js_1=require("./RestrictionType.js"),Cloneable_js_1=require("../Cloneable.js"),PurposeRestrictionVector=function(e){function t(){var t=null!==e&&e.apply(this,arguments)||this;return t.bitLength=0,t.map=new Map,t}return __extends(t,e),t.prototype.has=function(e){return this.map.has(e)},t.prototype.isOkToHave=function(e,t,r){var o,n=!0;if(null===(o=this.gvl)||void 0===o?void 0:o.vendors){var s=this.gvl.vendors[r];if(s)if(e===RestrictionType_js_1.RestrictionType.NOT_ALLOWED)n=s.legIntPurposes.includes(t)||s.purposes.includes(t);else if(s.flexiblePurposes.length)switch(e){case RestrictionType_js_1.RestrictionType.REQUIRE_CONSENT:n=s.flexiblePurposes.includes(t)&&s.legIntPurposes.includes(t);break;case RestrictionType_js_1.RestrictionType.REQUIRE_LI:n=s.flexiblePurposes.includes(t)&&s.purposes.includes(t)}else n=!1;else n=!1}return n},t.prototype.add=function(e,t){if(this.isOkToHave(t.restrictionType,t.purposeId,e)){var r=t.hash;this.has(r)||(this.map.set(r,new Set),this.bitLength=0),this.map.get(r).add(e)}},t.prototype.createOrderedSetOfVendorIdsFromOne=function(e){if(this.cachedVendorMap||(this.cachedVendorMap=new Map),!this.cachedVendorMap.has(e)){var t=new Set(__spreadArray([],__read(Array(e).keys()),!1).map((function(e){return e+1})));this.cachedVendorMap.set(e,t)}return this.cachedVendorMap.get(e)},t.prototype.restrictPurposeToLegalBasis=function(e){var t=Array.from(this.gvl.vendorIds),r=e.hash,o=t[t.length-1];if(this.has(r))for(var n=1;n<=o;n++)this.map.get(r).add(n);else this.map.set(r,this.createOrderedSetOfVendorIdsFromOne(o)),this.bitLength=0},t.prototype.getVendors=function(e){return Array.from(this.getVendorsSet(e)).sort((function(e,t){return e-t}))},t.prototype.getVendorsSet=function(e){var t=new Set;if(e){var r=e.hash;this.has(r)&&(t=this.map.get(r))}else this.map.forEach((function(e){e.forEach((function(e){t.add(e)}))}));return t},t.prototype.getRestrictionType=function(e,t){var r;return this.getRestrictions(e).forEach((function(e){e.purposeId===t&&(void 0===r||r>e.restrictionType)&&(r=e.restrictionType)})),r},t.prototype.vendorHasRestriction=function(e,t){for(var r=!1,o=this.getRestrictions(e),n=0;n<o.length&&!r;n++)r=t.isSameAs(o[n]);return r},t.prototype.getMaxVendorId=function(){var e=0;return this.map.forEach((function(t){e=Math.max(Array.from(t)[t.size-1],e)})),e},t.prototype.getRestrictions=function(e){var t=[];return this.map.forEach((function(r,o){e?r.has(e)&&t.push(PurposeRestriction_js_1.PurposeRestriction.unHash(o)):t.push(PurposeRestriction_js_1.PurposeRestriction.unHash(o))})),t},t.prototype.getPurposes=function(){var e=new Set;return this.map.forEach((function(t,r){e.add(PurposeRestriction_js_1.PurposeRestriction.unHash(r).purposeId)})),Array.from(e)},t.prototype.remove=function(e,t){var r=t.hash,o=this.map.get(r);o&&(o.delete(e),0==o.size&&(this.map.delete(r),this.bitLength=0))},Object.defineProperty(t.prototype,"gvl",{get:function(){return this.gvl_},set:function(e){var t=this;this.gvl_||(this.gvl_=e,this.map.forEach((function(e,r){var o=PurposeRestriction_js_1.PurposeRestriction.unHash(r);Array.from(e).forEach((function(r){t.isOkToHave(o.restrictionType,o.purposeId,r)||e.delete(r)}))})))},enumerable:!1,configurable:!0}),t.prototype.isEmpty=function(){return 0===this.map.size},Object.defineProperty(t.prototype,"numRestrictions",{get:function(){return this.map.size},enumerable:!1,configurable:!0}),t}(Cloneable_js_1.Cloneable);exports.PurposeRestrictionVector=PurposeRestrictionVector;
@@ -9,53 +9,68 @@ export class PurposeRestrictionVectorEncoder {
9
9
  let bitString = IntEncoder.encode(prVector.numRestrictions, BitLength.numRestrictions);
10
10
  // if the vector is empty we'll just return a string with just the numRestricitons being 0
11
11
  if (!prVector.isEmpty()) {
12
+ const gvlVendorIds = Array.from(prVector.gvl.vendorIds);
12
13
  const nextGvlVendor = (vendorId, lastVendorId) => {
13
- for (let nextId = vendorId + 1; nextId <= lastVendorId; nextId++) {
14
- if (prVector.gvl.vendorIds.has(nextId)) {
15
- return nextId;
16
- }
14
+ const firstIndex = gvlVendorIds.indexOf(vendorId);
15
+ const lastIndex = gvlVendorIds.indexOf(lastVendorId);
16
+ if (lastIndex - firstIndex > 0) {
17
+ const nextIndex = gvlVendorIds.indexOf(vendorId + 1);
18
+ return gvlVendorIds[nextIndex];
17
19
  }
18
20
  return vendorId;
19
21
  };
22
+ const cachedResults = new Map();
20
23
  // create each restriction group
21
24
  prVector.getRestrictions().forEach((purpRestriction) => {
22
25
  // every restriction group has the purposeId and the restrictionType;
23
26
  bitString += IntEncoder.encode(purpRestriction.purposeId, BitLength.purposeId);
24
27
  bitString += IntEncoder.encode(purpRestriction.restrictionType, BitLength.restrictionType);
25
28
  // now get all the vendors under that restriction
26
- const vendors = prVector.getVendors(purpRestriction);
27
- const len = vendors.length;
29
+ const vendorsReference = prVector.getVendorsSet(purpRestriction);
28
30
  /**
29
31
  * numEntries comes first so we will have to keep a counter and the do
30
32
  * the encoding at the end
31
33
  */
32
34
  let numEntries = 0;
33
- let startId = 0;
34
35
  let rangeField = '';
35
- for (let i = 0; i < len; i++) {
36
- const vendorId = vendors[i];
37
- if (startId === 0) {
38
- numEntries++;
39
- startId = vendorId;
40
- }
41
- /**
42
- * either end of the loop or there are GVL vendor IDs before the next one
43
- */
44
- if (i === len - 1 || vendors[i + 1] > nextGvlVendor(vendorId, vendors[len - 1])) {
36
+ if (cachedResults.has(vendorsReference)) {
37
+ const cachedResult = cachedResults.get(vendorsReference);
38
+ numEntries = cachedResult.numEntries;
39
+ rangeField = cachedResult.rangeField;
40
+ }
41
+ else {
42
+ const vendors = Array.from(vendorsReference).sort((a, b) => a - b);
43
+ const len = vendors.length;
44
+ let startId = 0;
45
+ for (let i = 0; i < len; i++) {
46
+ const vendorId = vendors[i];
47
+ if (startId === 0) {
48
+ numEntries++;
49
+ startId = vendorId;
50
+ }
45
51
  /**
46
- * it's a range entry if we've got something other than the start
47
- * ID
52
+ * either end of the loop or there are GVL vendor IDs before the next one
48
53
  */
49
- const isRange = !(vendorId === startId);
50
- // 0 means single 1 means range
51
- rangeField += BooleanEncoder.encode(isRange);
52
- rangeField += IntEncoder.encode(startId, BitLength.vendorId);
53
- if (isRange) {
54
- rangeField += IntEncoder.encode(vendorId, BitLength.vendorId);
54
+ if (i === len - 1 || vendors[i + 1] > nextGvlVendor(vendorId, vendors[len - 1])) {
55
+ /**
56
+ * it's a range entry if we've got something other than the start
57
+ * ID
58
+ */
59
+ const isRange = !(vendorId === startId);
60
+ // 0 means single 1 means range
61
+ rangeField += BooleanEncoder.encode(isRange);
62
+ rangeField += IntEncoder.encode(startId, BitLength.vendorId);
63
+ if (isRange) {
64
+ rangeField += IntEncoder.encode(vendorId, BitLength.vendorId);
65
+ }
66
+ // reset the startId so we grab the next id in the list
67
+ startId = 0;
55
68
  }
56
- // reset the startId so we grab the next id in the list
57
- startId = 0;
58
69
  }
70
+ cachedResults.set(vendorsReference, {
71
+ numEntries,
72
+ rangeField,
73
+ });
59
74
  }
60
75
  /**
61
76
  * now that the range encoding is built, encode the number of ranges
@@ -94,6 +109,7 @@ export class PurposeRestrictionVectorEncoder {
94
109
  if (endVendorId < startOrOnlyVendorId) {
95
110
  throw new DecodingError(`Invalid RangeEntry: endVendorId ${endVendorId} is less than ${startOrOnlyVendorId}`);
96
111
  }
112
+ // Can be optimized if required
97
113
  for (let k = startOrOnlyVendorId; k <= endVendorId; k++) {
98
114
  vector.add(k, purposeRestriction);
99
115
  }
@@ -14,6 +14,7 @@ export declare class PurposeRestrictionVector extends Cloneable<PurposeRestricti
14
14
  */
15
15
  private map;
16
16
  private gvl_;
17
+ private cachedVendorMap;
17
18
  private has;
18
19
  private isOkToHave;
19
20
  /**
@@ -24,6 +25,7 @@ export declare class PurposeRestrictionVector extends Cloneable<PurposeRestricti
24
25
  * @return {void}
25
26
  */
26
27
  add(vendorId: number, purposeRestriction: PurposeRestriction): void;
28
+ private createOrderedSetOfVendorIdsFromOne;
27
29
  /**
28
30
  * restrictPurposeToLegalBasis - adds all Vendors under a given Purpose Restriction
29
31
  *
@@ -42,6 +44,7 @@ export declare class PurposeRestrictionVector extends Cloneable<PurposeRestricti
42
44
  * @return {number[]} - Unique ID set of vendors
43
45
  */
44
46
  getVendors(purposeRestriction?: PurposeRestriction): number[];
47
+ getVendorsSet(purposeRestriction?: PurposeRestriction): Set<number>;
45
48
  getRestrictionType(vendorId: number, purposeId: number): RestrictionType | undefined;
46
49
  /**
47
50
  * vendorHasRestriction - determines whether a given Vendor ID is under a
@@ -13,6 +13,7 @@ export class PurposeRestrictionVector extends Cloneable {
13
13
  */
14
14
  map = new Map();
15
15
  gvl_;
16
+ cachedVendorMap;
16
17
  has(hash) {
17
18
  return this.map.has(hash);
18
19
  }
@@ -89,6 +90,19 @@ export class PurposeRestrictionVector extends Cloneable {
89
90
  this.map.get(hash).add(vendorId);
90
91
  }
91
92
  }
93
+ createOrderedSetOfVendorIdsFromOne(lastEntry) {
94
+ if (!this.cachedVendorMap) {
95
+ this.cachedVendorMap = new Map();
96
+ }
97
+ if (!this.cachedVendorMap.has(lastEntry)) {
98
+ /**
99
+ * Create an ordered array of vendor IDs from `1` (the minimum value for Vendor ID) to `lastEntry`
100
+ */
101
+ const orderedSet = new Set([...Array(lastEntry).keys()].map((i) => i + 1));
102
+ this.cachedVendorMap.set(lastEntry, orderedSet);
103
+ }
104
+ return this.cachedVendorMap.get(lastEntry);
105
+ }
92
106
  /**
93
107
  * restrictPurposeToLegalBasis - adds all Vendors under a given Purpose Restriction
94
108
  *
@@ -99,12 +113,8 @@ export class PurposeRestrictionVector extends Cloneable {
99
113
  const vendors = Array.from(this.gvl.vendorIds);
100
114
  const hash = purposeRestriction.hash;
101
115
  const lastEntry = vendors[vendors.length - 1];
102
- /**
103
- * Create an ordered array of vendor IDs from `1` (the minimum value for Vendor ID) to `lastEntry`
104
- */
105
- const values = [...Array(lastEntry).keys()].map((i) => i + 1);
106
116
  if (!this.has(hash)) {
107
- this.map.set(hash, new Set(values)); // use static method `build` to create a `BST` from the ordered array of IDs
117
+ this.map.set(hash, this.createOrderedSetOfVendorIdsFromOne(lastEntry));
108
118
  this.bitLength = 0;
109
119
  }
110
120
  else {
@@ -129,23 +139,25 @@ export class PurposeRestrictionVector extends Cloneable {
129
139
  * @return {number[]} - Unique ID set of vendors
130
140
  */
131
141
  getVendors(purposeRestriction) {
132
- let vendorIds = [];
142
+ const vendorIds = Array.from(this.getVendorsSet(purposeRestriction));
143
+ return vendorIds.sort((a, b) => a - b);
144
+ }
145
+ getVendorsSet(purposeRestriction) {
146
+ let vendorIds = new Set();
133
147
  if (purposeRestriction) {
134
148
  const hash = purposeRestriction.hash;
135
149
  if (this.has(hash)) {
136
- vendorIds = Array.from(this.map.get(hash));
150
+ vendorIds = this.map.get(hash);
137
151
  }
138
152
  }
139
153
  else {
140
- const vendorSet = new Set();
141
154
  this.map.forEach((set) => {
142
155
  set.forEach((vendorId) => {
143
- vendorSet.add(vendorId);
156
+ vendorIds.add(vendorId);
144
157
  });
145
158
  });
146
- vendorIds = Array.from(vendorSet);
147
159
  }
148
- return vendorIds.sort((a, b) => a - b);
160
+ return vendorIds;
149
161
  }
150
162
  getRestrictionType(vendorId, purposeId) {
151
163
  let rType;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pubtech-ai/core",
3
- "version": "2.1.1",
3
+ "version": "2.3.0",
4
4
  "description": "Ensures consistent encoding and decoding of TC Signals for the iab. Transparency and Consent Framework (TCF).",
5
5
  "author": "Mayank Mishra <mayank@iabtechlab.com>",
6
6
  "homepage": "https://iabtcf.com/",
@@ -28,7 +28,7 @@
28
28
  "test-cov": "rm -rf coverage; nyc --reporter=html mocha"
29
29
  },
30
30
  "devDependencies": {
31
- "@pubtech-ai/testing": "2.1.1",
31
+ "@pubtech-ai/testing": "2.3.0",
32
32
  "@istanbuljs/nyc-config-typescript": "^0.1.3",
33
33
  "@types/sinon": "^10.0.11",
34
34
  "@types/sinon-chai": "3.2.8",