forked from InteractiveAdvertisingBureau/iabtcf-es
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTCData.ts
More file actions
199 lines (127 loc) · 5.14 KB
/
TCData.ts
File metadata and controls
199 lines (127 loc) · 5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import {TCModel, PurposeRestriction, PurposeRestrictionVector, Vector, IdBoolTuple} from '@iabtechlabtcf/core';
import {CmpApiModel} from '../CmpApiModel.js';
import {BooleanVector} from './BooleanVector.js';
import {Restrictions} from './Restrictions.js';
import {Booleany} from './Booleany.js';
import {Response} from './Response.js';
import {EventStatus, CmpStatus} from '../status/index.js';
export class TCData extends Response {
public tcString: string;
public listenerId: number;
public eventStatus: EventStatus;
public cmpStatus: CmpStatus;
public isServiceSpecific: Booleany;
public useNonStandardTexts: Booleany;
public publisherCC: string;
public purposeOneTreatment: Booleany;
public outOfBand: {
allowedVendors: BooleanVector | string;
disclosedVendors: BooleanVector | string;
};
public purpose: {
consents: BooleanVector | string;
legitimateInterests: BooleanVector | string;
};
public vendor: {
consents: BooleanVector | string;
legitimateInterests: BooleanVector | string;
disclosedVendors: BooleanVector | string;
};
public specialFeatureOptins: BooleanVector | string;
public publisher: {
consents: BooleanVector | string;
legitimateInterests: BooleanVector | string;
customPurpose: {
consents: BooleanVector | string;
legitimateInterests: BooleanVector | string;
};
restrictions: Restrictions;
};
/**
* Constructor to create a TCData object from a TCModel
* @param {number[]} vendorIds - if not undefined, will be used to filter vendor ids
* @param {number} listenerId - if there is a listenerId to add
*/
public constructor(vendorIds?: number[], listenerId?: number) {
super();
this.eventStatus = CmpApiModel.eventStatus;
this.cmpStatus = CmpApiModel.cmpStatus;
this.listenerId = listenerId;
if (CmpApiModel.gdprApplies) {
const tcModel = CmpApiModel.tcModel as TCModel;
this.tcString = CmpApiModel.tcString;
this.isServiceSpecific = tcModel.isServiceSpecific;
this.useNonStandardTexts = tcModel.useNonStandardTexts;
this.purposeOneTreatment = tcModel.purposeOneTreatment;
this.publisherCC = tcModel.publisherCountryCode;
if (this.isServiceSpecific === false) {
this.outOfBand = {
allowedVendors: this.createVectorField(tcModel.vendorsAllowed, vendorIds),
disclosedVendors: this.createVectorField(tcModel.vendorsDisclosed, vendorIds),
};
}
this.purpose = {
consents: this.createVectorField(tcModel.purposeConsents),
legitimateInterests: this.createVectorField(tcModel.purposeLegitimateInterests),
};
this.vendor = {
consents: this.createVectorField(tcModel.vendorConsents, vendorIds),
legitimateInterests: this.createVectorField(tcModel.vendorLegitimateInterests, vendorIds),
disclosedVendors: this.createVectorField(tcModel.vendorsDisclosed, vendorIds),
};
this.specialFeatureOptins = this.createVectorField(tcModel.specialFeatureOptins);
this.publisher = {
consents: this.createVectorField(tcModel.publisherConsents),
legitimateInterests: this.createVectorField(tcModel.publisherLegitimateInterests),
customPurpose: {
consents: this.createVectorField(tcModel.publisherCustomConsents),
legitimateInterests: this.createVectorField(tcModel.publisherCustomLegitimateInterests),
},
restrictions: this.createRestrictions(tcModel.publisherRestrictions),
};
}
}
/**
* Creates a restrictions object given a PurposeRestrictionVector
* @param {PurposeRestrictionVector} purpRestrictions
* @return {Restrictions}
*/
protected createRestrictions(purpRestrictions: PurposeRestrictionVector): Restrictions {
const retr = {};
if (purpRestrictions.numRestrictions > 0) {
const max = purpRestrictions.getMaxVendorId();
for (let vendorId = 1; vendorId <= max; vendorId++) {
const strVendorId = vendorId.toString();
// vendors restrictions
purpRestrictions.getRestrictions(vendorId).forEach((pRestrict: PurposeRestriction): void => {
const strPurpId = pRestrict.purposeId.toString();
if (!retr[strPurpId]) {
retr[strPurpId] = {};
}
retr[strPurpId][strVendorId] = pRestrict.restrictionType;
});
}
}
return retr;
};
/**
* Creates a string bit field with a value for each id where each value is
* '1' if its id is in the passed in vector Can be overwritten to return a
* string
* @param {Vector} vector
* @param {number[]} ids filter
* @return {BooleanVector | string}
*/
protected createVectorField(vector: Vector, ids?: number[]): BooleanVector | string {
if (ids) {
return ids.reduce<BooleanVector>((booleanVector, obj): BooleanVector => {
booleanVector[String(obj)] = vector.has(Number(obj));
return booleanVector;
}, {});
}
return [...vector].reduce<BooleanVector>((booleanVector, keys: IdBoolTuple): BooleanVector => {
booleanVector[keys[0].toString(10)] = keys[1];
return booleanVector;
}, {});
}
}