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
182 lines (143 loc) · 4.53 KB
/
Utils.ts
File metadata and controls
182 lines (143 loc) · 4.53 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
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): string {
if (!source || source.length === 0) {
return null;
}
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.toString();
}
public static getCookies(cookies:string): Object {
let result:Object = {};
let parts:string[] = (cookies || '').split('; ');
for (let index = 0; index < parts.length; index++) {
let cookie:string[] = parts[index].split('=');
result[cookie[0]] = cookie[1];
}
return result;
}
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) {
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('=');
result[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
}
return result;
}
public static randomNumber(): number {
return Math.floor(Math.random() * 9007199254740992);
}
public static stringify(data:any, exclusions?:string[]): string {
function checkForMatch(pattern:string, value:string): boolean {
if (!pattern || !value || typeof value !== 'string') {
return false;
}
let trim = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g;
pattern = pattern.toLowerCase().replace(trim, '');
value = value.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 value.indexOf(pattern) !== -1;
}
if (startsWithWildcard) {
return value.lastIndexOf(pattern) === (value.length - pattern.length);
}
if (endsWithWildcard) {
return value.indexOf(pattern) === 0;
}
return value === pattern;
}
function stringifyImpl(obj:any, excludedKeys:string[]): string {
let cache:string[] = [];
return JSON.stringify(obj, function(key:string, value:any) {
for (let index = 0; index < (excludedKeys || []).length; index++) {
if (checkForMatch(excludedKeys[index], key)) {
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 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 || []);
}
}