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
221 lines (178 loc) · 5.74 KB
/
Utils.ts
File metadata and controls
221 lines (178 loc) · 5.74 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
export class Utils {
public static addRange<T>(target: T[], ...values: T[]) {
if (!target) {
target = [];
}
if (!values || values.length === 0) {
return target;
}
for (let index = 0; index < values.length; index++) {
if (values[index] && target.indexOf(values[index]) < 0) {
target.push(values[index]);
}
}
return target;
}
public static getHashCode(source: string): number {
if (!source || source.length === 0) {
return 0;
}
let hash: number = 0;
for (let index = 0; index < source.length; index++) {
let character = source.charCodeAt(index);
hash = ((hash << 5) - hash) + character;
hash |= 0;
}
return hash;
}
public static getCookies(cookies: string, exclusions?: string[]): Object {
let result: Object = {};
let parts: string[] = (cookies || '').split('; ');
for (let index = 0; index < parts.length; index++) {
let cookie: string[] = parts[index].split('=');
if (!Utils.isMatch(cookie[0], exclusions)) {
result[cookie[0]] = cookie[1];
}
}
return !Utils.isEmpty(result) ? result : null;
}
public static guid(): string {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
}
return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
}
public static merge(defaultValues: Object, values: Object) {
let result: Object = {};
for (let key in defaultValues || {}) {
if (!!defaultValues[key]) {
result[key] = defaultValues[key];
}
}
for (let key in values || {}) {
if (!!values[key]) {
result[key] = values[key];
}
}
return result;
}
public static parseVersion(source: string): string {
if (!source) {
return null;
}
let versionRegex = /(v?((\d+)\.(\d+)(\.(\d+))?)(?:-([\dA-Za-z\-]+(?:\.[\dA-Za-z\-]+)*))?(?:\+([\dA-Za-z\-]+(?:\.[\dA-Za-z\-]+)*))?)/;
let matches = versionRegex.exec(source);
if (matches && matches.length > 0) {
return matches[0];
}
return null;
}
public static parseQueryString(query: string, exclusions?: string[]) {
if (!query || query.length === 0) {
return null;
}
let pairs: string[] = query.split('&');
if (pairs.length === 0) {
return null;
}
let result: Object = {};
for (let index = 0; index < pairs.length; index++) {
let pair = pairs[index].split('=');
if (!Utils.isMatch(pair[0], exclusions)) {
result[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
}
}
return !Utils.isEmpty(result) ? result : null;
}
public static 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 (*).
*/
public static isMatch(input: string, patterns: string[]): boolean {
if (!input || typeof input !== 'string') {
return false;
}
let trim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g;
return (patterns || []).some(pattern => {
if (!pattern) {
return false;
}
pattern = pattern.toLowerCase().replace(trim, '');
input = input.toLowerCase().replace(trim, '');
if (pattern.length <= 0) {
return false;
}
let startsWithWildcard: boolean = pattern[0] === '*';
if (startsWithWildcard) {
pattern = pattern.slice(1);
}
let endsWithWildcard: boolean = pattern[pattern.length - 1] === '*';
if (endsWithWildcard) {
pattern = pattern.substring(0, pattern.length - 1);
}
if (startsWithWildcard && endsWithWildcard) {
return input.indexOf(pattern) !== -1;
}
if (startsWithWildcard) {
let lastIndexOf = input.lastIndexOf(pattern);
return lastIndexOf !== -1 && lastIndexOf === (input.length - pattern.length);
}
if (endsWithWildcard) {
return input.indexOf(pattern) === 0;
}
return input === pattern;
});
}
public static isEmpty(input: Object) {
return input === null || (typeof (input) === 'object' && Object.keys(input).length === 0);
}
/**
* Stringifys 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.
*/
public static stringify(data: any, exclusions?: string[], maxDepth?: number): string {
function stringifyImpl(obj: any, excludedKeys: string[]): string {
let cache: string[] = [];
return JSON.stringify(obj, function(key: string, value: any) {
if (Utils.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]') {
let flattened = {};
/* tslint:disable:forin */
for (let prop in data) {
let value = data[prop];
if (value === data) {
continue;
}
flattened[prop] = data[prop];
}
/* tslint:enable:forin */
return stringifyImpl(flattened, exclusions);
}
if (({}).toString.call(data) === '[object Array]') {
let result = [];
for (let index = 0; index < data.length; index++) {
result[index] = JSON.parse(stringifyImpl(data[index], exclusions));
}
return JSON.stringify(result);
}
return stringifyImpl(data, exclusions);
}
}