forked from exceptionless/Exceptionless.JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.ts
More file actions
294 lines (240 loc) · 6.91 KB
/
Utils.ts
File metadata and controls
294 lines (240 loc) · 6.91 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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
export function getHashCode(source: string): number {
if (!source || source.length === 0) {
return 0;
}
let hash = 0;
for (let index = 0; index < source.length; index++) {
const character = source.charCodeAt(index);
hash = ((hash << 5) - hash) + character;
hash |= 0;
}
return hash;
}
export function getCookies(
cookies: string,
exclusions?: string[],
): Record<string, string> | null {
const result: Record<string, string> = {};
const parts: string[] = (cookies || "").split("; ");
for (const part of parts) {
const cookie: string[] = part.split("=");
if (!isMatch(cookie[0], exclusions || [])) {
result[cookie[0]] = cookie[1];
}
}
return !isEmpty(result) ? result : null;
}
export function guid(): string {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
}
return s4() + s4() + "-" + s4() + "-" + s4() + "-" + s4() + "-" + s4() +
s4() + s4();
}
export function parseVersion(source: string): string | null {
if (!source) {
return null;
}
const versionRegex =
/(v?((\d+)\.(\d+)(\.(\d+))?)(?:-([\dA-Za-z-]+(?:\.[\dA-Za-z-]+)*))?(?:\+([\dA-Za-z-]+(?:\.[\dA-Za-z-]+)*))?)/;
const matches = versionRegex.exec(source);
if (matches && matches.length > 0) {
return matches[0];
}
return null;
}
export function parseQueryString(
query: string,
exclusions?: string[],
): Record<string, string> {
if (!query || query.length === 0) {
return {};
}
const pairs: string[] = query.split("&");
if (pairs.length === 0) {
return {};
}
const result: Record<string, string> = {};
for (const pair of pairs) {
const parts = pair.split("=");
if (!exclusions || !isMatch(parts[0], exclusions)) {
result[decodeURIComponent(parts[0])] = decodeURIComponent(parts[1]);
}
}
return !isEmpty(result) ? result : {};
}
export function randomNumber(): number {
return Math.floor(Math.random() * 9007199254740992);
}
/**
* Checks to see if a value matches a pattern.
* @param input the value to check against the @pattern.
* @param pattern The pattern to check, supports wild cards (*).
*/
export function isMatch(
input: string | undefined,
patterns: string[],
ignoreCase = true,
): boolean {
if (typeof input !== "string") {
return false;
}
const trim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g;
input = (ignoreCase ? input.toLowerCase() : input).replace(trim, "");
return (patterns || []).some((pattern) => {
if (typeof pattern !== "string") {
return false;
}
if (pattern) {
pattern = (ignoreCase ? pattern.toLowerCase() : pattern).replace(
trim,
"",
);
}
if (!pattern) {
return input === undefined || input === null;
}
if (pattern === "*") {
return true;
}
if (input === undefined || input === null) {
return false;
}
const startsWithWildcard: boolean = pattern[0] === "*";
if (startsWithWildcard) {
pattern = pattern.slice(1);
}
const endsWithWildcard: boolean = pattern[pattern.length - 1] === "*";
if (endsWithWildcard) {
pattern = pattern.substring(0, pattern.length - 1);
}
if (startsWithWildcard && endsWithWildcard) {
return pattern.length <= input.length && input.indexOf(pattern, 0) !== -1;
}
if (startsWithWildcard) {
return endsWith(input, pattern);
}
if (endsWithWildcard) {
return startsWith(input, pattern);
}
return input === pattern;
});
}
export function isEmpty(input: Record<string, unknown> | null | undefined | unknown): boolean {
if (input === null || input === undefined) {
return true;
}
if (typeof input == "object") {
return Object.keys(<Record<string, unknown>>input).length === 0;
}
return false;
}
export function startsWith(input: string, prefix: string): boolean {
return input.substring(0, prefix.length) === prefix;
}
export function endsWith(input: string, suffix: string): boolean {
return input.indexOf(suffix, input.length - suffix.length) !== -1;
}
// @ts-expect-error TS6133
export function stringify(data: unknown, exclusions?: string[], maxDepth?: number): string {
function stringifyImpl(obj: unknown, excludedKeys: string[]): string {
const cache: unknown[] = [];
return JSON.stringify(obj, (key: string, value: unknown) => {
if (isMatch(key, excludedKeys)) {
return;
}
if (typeof value === "object" && value) {
if (cache.indexOf(value) !== -1) {
// Circular reference found, discard key
return;
}
cache.push(value);
}
return value;
});
}
if (({}).toString.call(data) === "[object Object]") {
const flattened: { [prop: string]: unknown } = {};
// @ts-expect-error TS2407
for (const prop in data) {
// @ts-expect-error TS7053
const value = data[prop] as unknown;
if (value !== data) {
flattened[prop] = value;
}
}
return stringifyImpl(flattened, exclusions || []);
}
if (({}).toString.call(data) === "[object Array]") {
const result: unknown[] = [];
for (let index = 0; index < (<unknown[]>data).length; index++) {
result[index] = JSON.parse(stringifyImpl((<unknown[]>data)[index], exclusions || [])) as unknown;
}
return JSON.stringify(result);
}
// TODO: support maxDepth
return stringifyImpl(data, exclusions || []);
}
/**
* Stringifies an object with optional exclusions and max depth.
* @param data The data object to add.
* @param exclusions Any property names that should be excluded.
* @param maxDepth The max depth of the object to include.
*/
export function stringify2(
data: unknown,
exclusions?: string[],
maxDepth = -1,
): string {
const seen = new WeakSet();
function stringifyImpl(
obj: unknown,
excludedKeys: string[],
currentDepth: number,
currentScope: string,
): string {
return JSON.stringify(obj, (key: string, value: unknown) => {
if (isMatch(key, excludedKeys)) {
return;
}
if (typeof value === "object" && value !== null) {
if (seen.has(value)) {
return;
}
seen.add(value);
if (currentDepth >= maxDepth)
return;
return stringifyImpl(
value,
excludedKeys,
currentDepth + 1,
key.length > 0 ? currentScope + "." + key : currentScope,
);
}
return value;
});
}
return stringifyImpl(data, exclusions || [], 1, "");
}
export function toBoolean(input: unknown, defaultValue: boolean = false): boolean {
if (typeof input === "boolean") {
return input;
}
if (
input === null || typeof input !== "number" && typeof input !== "string"
) {
return defaultValue;
}
switch ((input + "").toLowerCase().trim()) {
case "true":
case "yes":
case "1":
return true;
case "false":
case "no":
case "0":
case null:
return false;
}
return defaultValue;
}