-
Notifications
You must be signed in to change notification settings - Fork 10
/
ResourceHelper.ts
114 lines (80 loc) · 2.13 KB
/
ResourceHelper.ts
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
import { ResourceRequestBodyType } from './Declarations';
export class ResourceHelper {
static cleanDataFields: string[] = [
'$resolved',
'$promise',
'$abort',
'$resource'
];
private static isBrowser: boolean = null;
static isRunningInBrowser(): boolean {
if (this.isBrowser !== null) {
return this.isBrowser;
}
try {
this.isBrowser = !!window;
} catch (e) {
this.isBrowser = false;
}
return this.isBrowser;
}
static getRealTypeOf(data: any): ResourceRequestBodyType {
if (!data) {
return ResourceRequestBodyType.NONE;
}
if (this.isRunningInBrowser()) {
if (FormData && data instanceof FormData) {
return ResourceRequestBodyType.FORM_DATA;
}
if (Blob && data instanceof Blob) {
return ResourceRequestBodyType.BLOB;
}
}
if (data instanceof ArrayBuffer) {
return ResourceRequestBodyType.ARRAY_BUFFER;
}
if (['string', 'number'].indexOf(typeof data) > -1) {
return ResourceRequestBodyType.TEXT;
}
return ResourceRequestBodyType.JSON;
}
static defaults(dst: any, src: any): any {
if (!dst) {
dst = {};
}
Object.keys(src)
.forEach((key: string) => {
if (dst[key] === undefined) {
dst[key] = src[key];
}
});
return dst;
}
static isNullOrUndefined(value: any): boolean {
return value === null || value === undefined;
}
static cleanData(obj: any): any {
if (Array.isArray(obj)) {
return this.cleanDataArray(obj);
} else {
return this.cleanDataObject(obj);
}
}
static cleanDataArray(obj: any[]): any[] {
for (const propName in obj) {
if (typeof obj[propName] === 'function' || this.cleanDataFields.indexOf(propName) > -1) {
delete obj[propName];
}
}
return obj;
}
static cleanDataObject(obj: any): any {
const cleanedObj: any = {};
for (const propName in obj) {
if (typeof obj[propName] !== 'function' && this.cleanDataFields.indexOf(propName) === -1) {
cleanedObj[propName] = obj[propName];
}
}
return cleanedObj;
}
}