Repository navigation
Expand file tree
/
Copy pathstring.ts
More file actions
105 lines (92 loc) · 2.45 KB
/
Copy pathstring.ts
File metadata and controls
105 lines (92 loc) · 2.45 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
/**
* Truncates given string to the maximum characters count
*
* @param str An object that contains serializable values
* @param max Maximum number of characters in truncated string
* @returns string Encoded
*/
export function truncate(str: string, max: number = 0): string {
if (max === 0) {
return str;
}
return str.length <= max ? str : `${str.substr(0, max)}...`;
}
/**
* This is basically just `trim_line` from
* https://github.com/getsentry/sentry/blob/master/src/sentry/lang/javascript/processor.py#L67
*
* @param str An object that contains serializable values
* @param max Maximum number of characters in truncated string
* @returns string Encoded
*/
export function snipLine(line: string, colno: number): string {
let newLine = line;
const ll = newLine.length;
if (ll <= 150) {
return newLine;
}
if (colno > ll) {
colno = ll; // tslint:disable-line:no-parameter-reassignment
}
let start = Math.max(colno - 60, 0);
if (start < 5) {
start = 0;
}
let end = Math.min(start + 140, ll);
if (end > ll - 5) {
end = ll;
}
if (end === ll) {
start = Math.max(end - 140, 0);
}
newLine = newLine.slice(start, end);
if (start > 0) {
newLine = `'{snip} ${newLine}`;
}
if (end < ll) {
newLine += ' {snip}';
}
return newLine;
}
/**
* Join values in array
* @param input array of values to be joined together
* @param delimiter string to be placed in-between values
* @returns Joined values
*/
export function safeJoin(input: any[], delimiter?: string): string {
if (!Array.isArray(input)) {
return '';
}
const output = [];
// tslint:disable-next-line:prefer-for-of
for (let i = 0; i < input.length; i++) {
const value = input[i];
try {
output.push(String(value));
} catch (e) {
output.push('[value cannot be serialized]');
}
}
return output.join(delimiter);
}
/** Merges provided array of keys into */
export function keysToEventMessage(keys: string[], maxLength: number = 40): string {
if (!keys.length) {
return '[object has no keys]';
}
if (keys[0].length >= maxLength) {
return truncate(keys[0], maxLength);
}
for (let includedKeys = keys.length; includedKeys > 0; includedKeys--) {
const serialized = keys.slice(0, includedKeys).join(', ');
if (serialized.length > maxLength) {
continue;
}
if (includedKeys === keys.length) {
return serialized;
}
return truncate(serialized, maxLength);
}
return '';
}