-
Notifications
You must be signed in to change notification settings - Fork 134
/
Copy pathutils.js
260 lines (232 loc) · 7.16 KB
/
utils.js
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import constants from './constants';
import type from './type';
var logLevels = {
DISABLE: 0,
ERROR: 1,
WARN: 2,
INFO: 3,
};
let logLevel = logLevels.WARN;
const setLogLevel = function setLogLevel(logLevelName) {
if (Object.prototype.hasOwnProperty.call(logLevels, logLevel)) {
logLevel = logLevels[logLevelName];
}
};
const getLogLevel = function getLogLevel() {
return logLevel;
};
const log = {
error: (s) => {
if (logLevel >= logLevels.ERROR) {
_log(s);
}
},
warn: (s) => {
if (logLevel >= logLevels.WARN) {
_log(s);
}
},
info: (s) => {
if (logLevel >= logLevels.INFO) {
_log(s);
}
},
};
var _log = function _log(s) {
try {
console.log('[Amplitude] ' + s);
} catch (e) {
// console logging not available
}
};
var isEmptyString = function isEmptyString(str) {
return !str || str.length === 0;
};
var sessionStorageEnabled = function sessionStorageEnabled() {
try {
if (window.sessionStorage) {
return true;
}
} catch (e) {
// sessionStorage disabled
}
return false;
};
// truncate string values in event and user properties so that request size does not get too large
var truncate = function truncate(value) {
if (type(value) === 'array') {
for (var i = 0; i < value.length; i++) {
value[i] = truncate(value[i]);
}
} else if (type(value) === 'object') {
for (var key in value) {
if (key in value) {
value[key] = truncate(value[key]);
}
}
} else {
value = _truncateValue(value);
}
return value;
};
var _truncateValue = function _truncateValue(value) {
if (type(value) === 'string') {
return value.length > constants.MAX_STRING_LENGTH ? value.substring(0, constants.MAX_STRING_LENGTH) : value;
}
return value;
};
var validateInput = function validateInput(input, name, expectedType) {
if (type(input) !== expectedType) {
log.error('Invalid ' + name + ' input type. Expected ' + expectedType + ' but received ' + type(input));
return false;
}
return true;
};
// do some basic sanitization and type checking, also catch property dicts with more than 1000 key/value pairs
var validateProperties = function validateProperties(properties) {
var propsType = type(properties);
if (propsType !== 'object') {
log.error('Error: invalid properties format. Expecting Javascript object, received ' + propsType + ', ignoring');
return {};
}
if (Object.keys(properties).length > constants.MAX_PROPERTY_KEYS) {
log.error('Error: too many properties (more than 1000), ignoring');
return {};
}
var copy = {}; // create a copy with all of the valid properties
for (var property in properties) {
if (!Object.prototype.hasOwnProperty.call(properties, property)) {
continue;
}
// validate key
var key = property;
var keyType = type(key);
if (keyType !== 'string') {
key = String(key);
log.warn('WARNING: Non-string property key, received type ' + keyType + ', coercing to string "' + key + '"');
}
// validate value
var value = validatePropertyValue(key, properties[property]);
if (value === null) {
continue;
}
copy[key] = value;
}
return copy;
};
var invalidValueTypes = ['nan', 'function', 'arguments', 'regexp', 'element'];
var validatePropertyValue = function validatePropertyValue(key, value) {
var valueType = type(value);
if (invalidValueTypes.indexOf(valueType) !== -1) {
log.warn('WARNING: Property key "' + key + '" with invalid value type ' + valueType + ', ignoring');
value = null;
} else if (valueType === 'undefined') {
value = null;
} else if (valueType === 'error') {
value = String(value);
log.warn('WARNING: Property key "' + key + '" with value type error, coercing to ' + value);
} else if (valueType === 'array') {
// check for nested arrays or objects
var arrayCopy = [];
for (var i = 0; i < value.length; i++) {
var element = value[i];
var elemType = type(element);
if (elemType === 'array') {
log.warn('WARNING: Cannot have ' + elemType + ' nested in an array property value, skipping');
continue;
} else if (elemType === 'object') {
arrayCopy.push(validateProperties(element));
} else {
arrayCopy.push(validatePropertyValue(key, element));
}
}
value = arrayCopy;
} else if (valueType === 'object') {
value = validateProperties(value);
}
return value;
};
var validateGroups = function validateGroups(groups) {
var groupsType = type(groups);
if (groupsType !== 'object') {
log.error('Error: invalid groups format. Expecting Javascript object, received ' + groupsType + ', ignoring');
return {};
}
var copy = {}; // create a copy with all of the valid properties
for (var group in groups) {
if (!Object.prototype.hasOwnProperty.call(groups, group)) {
continue;
}
// validate key
var key = group;
var keyType = type(key);
if (keyType !== 'string') {
key = String(key);
log.warn('WARNING: Non-string groupType, received type ' + keyType + ', coercing to string "' + key + '"');
}
// validate value
var value = validateGroupName(key, groups[group]);
if (value === null) {
continue;
}
copy[key] = value;
}
return copy;
};
var validateGroupName = function validateGroupName(key, groupName) {
var groupNameType = type(groupName);
if (groupNameType === 'string') {
return groupName;
}
if (groupNameType === 'date' || groupNameType === 'number' || groupNameType === 'boolean') {
groupName = String(groupName);
log.warn(
'WARNING: Non-string groupName, received type ' + groupNameType + ', coercing to string "' + groupName + '"',
);
return groupName;
}
if (groupNameType === 'array') {
// check for nested arrays or objects
var arrayCopy = [];
for (var i = 0; i < groupName.length; i++) {
var element = groupName[i];
var elemType = type(element);
if (elemType === 'array' || elemType === 'object') {
log.warn('WARNING: Skipping nested ' + elemType + ' in array groupName');
continue;
} else if (elemType === 'string') {
arrayCopy.push(element);
} else if (elemType === 'date' || elemType === 'number' || elemType === 'boolean') {
element = String(element);
log.warn('WARNING: Non-string groupName, received type ' + elemType + ', coercing to string "' + element + '"');
arrayCopy.push(element);
}
}
return arrayCopy;
}
log.warn(
'WARNING: Non-string groupName, received type ' +
groupNameType +
'. Please use strings or array of strings for groupName',
);
};
// parses the value of a url param (for example ?gclid=1234&...)
var getQueryParam = function getQueryParam(name, query) {
name = name.replace(/[[]/, '\\[').replace(/[\]]/, '\\]');
var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
var results = regex.exec(query);
return results === null ? undefined : decodeURIComponent(results[1].replace(/\+/g, ' '));
};
export default {
setLogLevel,
getLogLevel,
logLevels,
log,
isEmptyString,
getQueryParam,
sessionStorageEnabled,
truncate,
validateGroups,
validateInput,
validateProperties,
};