forked from NativeScript/firebase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
215 lines (187 loc) · 4.52 KB
/
utils.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
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
import { Firebase } from '.';
function numberHasDecimals(item: number) {
return !(item % 1 === 0);
}
function numberIs64Bit(item: number) {
return item < -Math.pow(2, 31) + 1 || item > Math.pow(2, 31) - 1;
}
export function serialize(data: any, wrapPrimitives: boolean = false): any {
if (global.isIOS) {
switch (typeof data) {
case 'string':
case 'boolean': {
return data;
}
case 'number': {
const hasDecimals = numberHasDecimals(data);
if (hasDecimals) {
return NSNumber.alloc().initWithDouble(data);
} else {
return NSNumber.alloc().initWithLongLong(data);
}
}
case 'object': {
if (data instanceof Date) {
return NSDate.dateWithTimeIntervalSince1970(data.getTime() / 1000);
}
if (!data) {
if (wrapPrimitives) {
return NSNull.new();
}
return null;
}
if (Array.isArray(data)) {
return NSArray.arrayWithArray(data.map((el) => serialize(el, wrapPrimitives)).filter((el) => el !== null));
}
const node = Object.fromEntries(
Object.entries(data)
.map(([key, value]) => [key, serialize(value, wrapPrimitives)])
.filter(([, value]) => value !== null)
);
return NSDictionary.dictionaryWithDictionary(node);
}
default:
if (wrapPrimitives) {
return NSNull.new();
}
return null;
}
}
if (global.isAndroid) {
let store;
switch (typeof data) {
case 'string':
case 'boolean': {
if (wrapPrimitives) {
if (typeof data === 'string') {
return new java.lang.String(data);
}
return new java.lang.Boolean(data);
}
return data;
}
case 'number': {
const hasDecimals = numberHasDecimals(data);
if (hasDecimals) {
return java.lang.Double.valueOf(data);
} else {
return java.lang.Long.valueOf(data);
}
}
case 'object': {
if (!data) {
return null;
}
if (data instanceof Date) {
return new java.util.Date(data.getTime());
}
if (Array.isArray(data)) {
store = new java.util.ArrayList();
data.forEach((item) => store.add(serialize(item, wrapPrimitives)));
return store;
}
if (data.native) {
return data.native;
}
store = new java.util.HashMap();
Object.keys(data).forEach((key) => store.put(key, serialize(data[key], wrapPrimitives)));
return store;
}
default:
return null;
}
}
}
export function deserialize(data: any): any {
if (global.isIOS) {
if (data instanceof NSNull) {
return null;
}
if (data instanceof NSArray) {
let array = [];
for (let i = 0, n = data.count; i < n; i++) {
array[i] = deserialize(data.objectAtIndex(i));
}
return array;
}
if (data instanceof NSDictionary) {
let dict = {};
for (let i = 0, n = data.allKeys.count; i < n; i++) {
let key = data.allKeys.objectAtIndex(i);
dict[key] = deserialize(data.objectForKey(key));
}
return dict;
}
return data;
}
if (global.isAndroid) {
if (data === null || typeof data !== 'object') {
return data;
}
let store;
switch (data.getClass().getName()) {
case 'java.lang.String': {
return String(data);
}
case 'java.lang.Boolean': {
return String(data) === 'true';
}
case 'java.lang.Float':
case 'java.lang.Integer':
case 'java.lang.Long':
case 'java.lang.Double':
case 'java.lang.Short': {
return Number(data);
}
case 'org.json.JSONArray': {
store = [];
for (let j = 0; j < data.length(); j++) {
store[j] = deserialize(data.get(j));
}
break;
}
case 'org.json.JSONObject': {
store = {};
let i = data.keys();
while (i.hasNext()) {
let key = i.next();
store[key] = deserialize(data.get(key));
}
break;
}
case 'androidx.collection.SimpleArrayMap': {
const count = data.size();
for (let l = 0; l < count; l++) {
const key = data.keyAt(l);
store[key] = deserialize(data.get(key));
}
break;
}
case 'androidx.collection.ArrayMap':
case 'android.os.Bundle':
case 'java.util.HashMap':
case 'java.util.Map': {
store = {};
const keys = data.keySet().toArray();
for (let k = 0; k < keys.length; k++) {
const key = keys[k];
store[key] = deserialize(data.get(key));
}
break;
}
default:
if (typeof data === 'object' && data instanceof java.util.List) {
const array = [];
const size = data.size();
for (let i = 0, n = size; i < n; i++) {
array[i] = deserialize(data.get(i));
}
store = array;
} else {
store = null;
}
break;
}
return store;
}
}