-
Notifications
You must be signed in to change notification settings - Fork 5
/
mergeKeys.gs
176 lines (146 loc) · 4.74 KB
/
mergeKeys.gs
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
/**
* @fileoverview Code related to merge keys. Merge keys are used
* to pass context specific data (i.e. sender email of opened email) to
* implementations of the different integration types (i.e. email field of
* a record for a Records based integration).
*/
MERGE_KV_PAIRS = {};
mergeInit();
/**
* Initialtize merge keys. Called each time this file is loaded to ensure
* merge key data is always properly initialized.
*/
function mergeInit() {
let config = getConfig();
if (config) {
MERGE_KV_PAIRS['{{toolName}}'] = config.toolName;
MERGE_KV_PAIRS['{{companyName}}'] = config.companyName;
}
}
/**
* Adds a merge key/value pair, making it later available by calling
* getMergeKeyValue.
*
* @param {string} key
* @param {Object} value
*/
function addMergeKeyValuePair(key, value) {
MERGE_KV_PAIRS[key] = value;
}
/**
* Retrieves a merge value based on its key.
*
* @param {string} key - The merge key associated (i.e. '{{sender_email}}')
*/
function getMergeKeyValue(key) {
if (MERGE_KV_PAIRS.hasOwnProperty(key)) {
return MERGE_KV_PAIRS[key];
}
return undefined;
}
/**
* Returns a structure containing all merge key/value pairs
*
* @return {Object}
*/
function getAllMergeKeyPairs() {
return MERGE_KV_PAIRS;
}
/**
* Replaces all occurences of each merge key in the target string with its
* corresponding value from the given key/value pairs object.
*
* @param {Object} kvPairs Object of merge key/value pairs
* @param {string} targetString String with merge keys that should be replaced.
*
* @return {string}
*/
function findAndReplaceMergeKeys(targetString, opts) {
if (typeof targetString !== 'string') {
return targetString;
}
let kvPairs = getAllMergeKeyPairs();
// search for a {{mergetag}}
let regex = /.*?(\{\{.*?\}\}).*?/g;
//console.log("targetString before: " + targetString);
// look for default-value merge keys in targetString of the form
// {{actualMergeKey || defaultValue}}. If actualMergeKey is present in kvPairs
// then replace the entire pattern with {{actualMergeKey}}. Else, replace it
// with defaultValue. All of this is done prior to actually replacing any
// merge keys in the targetString.
let match;
match = regex.exec(targetString);
while (match && match.length > 1) {
// check for a default value to use if merge string not found
let mergeTag = match[1];
//console.log('mergeTag=' + mergeTag)
let mergeDefaultPair = mergeTag.split('||');
//console.log('mergeDefaultPair =' + mergeDefaultPair)
if (mergeDefaultPair.length > 1) {
let actualMergeTag = mergeDefaultPair[0].trim() + '}}';
if (
!kvPairs.hasOwnProperty(actualMergeTag) ||
kvPairs[actualMergeTag] === ''
) {
let defaultMergeValue = mergeDefaultPair[1].trim().slice(0, -2);
//console.log('defaultMergeValue = ' + defaultMergeValue)
targetString = targetString.replace(mergeTag, defaultMergeValue);
} else {
targetString = targetString.replace(mergeTag, actualMergeTag);
}
}
match = regex.exec(targetString);
}
// Replace any merge keys in targetString with their corresponding values
// based on the key/value pairs in kvPairs.
for (let [key, value] of Object.entries(kvPairs)) {
if (!key) continue; // skip odd blank key seen sometimes
//console.log("checking for key: " + key)
if (opts && opts.uriEncodeValue) {
value = encodeURIComponent(value);
}
if (opts && opts.boldValue) {
value = `<b>${value}</b>`;
}
targetString = targetString.replace(new RegExp(key, 'g'), value);
}
return targetString;
}
/**
* Given a source string with a merge tag in it, checks if the
* merge tag is of the type {{mergeKey || defaultValue}}, and if so
* returns defaultValue. If no merge key is detected, or a merge key
* is detected but is has no default value, then the empty-string is
* returned.
*
* @param {string} sourceString - The string to check
*
* @return {string}
*/
function getDefaultMergeValue(sourceString) {
let regex = /.*?(\{\{.*?\}\}).*?/g;
let match;
match = regex.exec(sourceString);
if (!match || match.length < 2) {
return '';
}
let mergeTag = match[1];
let mergeDefaultPair = mergeTag.split('||');
if (mergeDefaultPair.length < 2) {
return '';
}
let defaultMergeValue = mergeDefaultPair[1].trim().slice(0, -2);
return defaultMergeValue;
}
/** Internal test function
*
*/
function _testMergeKeys() {
mergeInit();
addMergeKeyValuePair('{{senderEmal}}', 'testemail@domain.com');
addMergeKeyValuePair('{{token}}', 'Test User');
let targetString =
'{{senderName || Bobby McFee}} plus also {{senderEmail}}'
+ 'and {{token || participants}}';
console.log('final result: ' + findAndReplaceMergeKeys(targetString));
}