UNPKG

3.25 kBJavaScriptView Raw
1import * as iOSUtils from './ios';
2import { platformCheck } from './platform-check';
3import { getClass, isNullOrUndefined, numberHasDecimals, numberIs64Bit } from './types';
4export function dataDeserialize(nativeData) {
5 if (isNullOrUndefined(nativeData)) {
6 // some native values will already be js null values
7 // calling types.getClass below on null/undefined will cause crash
8 return null;
9 }
10 else {
11 switch (getClass(nativeData)) {
12 case 'NSNull':
13 return null;
14 case 'NSMutableDictionary':
15 case 'NSDictionary': {
16 const obj = {};
17 const length = nativeData.count;
18 const keysArray = nativeData.allKeys;
19 for (let i = 0; i < length; i++) {
20 const nativeKey = keysArray.objectAtIndex(i);
21 obj[nativeKey] = dataDeserialize(nativeData.objectForKey(nativeKey));
22 }
23 return obj;
24 }
25 case 'NSMutableArray':
26 case 'NSArray': {
27 const array = [];
28 const len = nativeData.count;
29 for (let i = 0; i < len; i++) {
30 array[i] = dataDeserialize(nativeData.objectAtIndex(i));
31 }
32 return array;
33 }
34 default:
35 return nativeData;
36 }
37 }
38}
39export function dataSerialize(data, wrapPrimitives = false) {
40 switch (typeof data) {
41 case 'string':
42 case 'boolean': {
43 return data;
44 }
45 case 'number': {
46 const hasDecimals = numberHasDecimals(data);
47 if (numberIs64Bit(data)) {
48 if (hasDecimals) {
49 return NSNumber.alloc().initWithDouble(data);
50 }
51 else {
52 return NSNumber.alloc().initWithLongLong(data);
53 }
54 }
55 else {
56 if (hasDecimals) {
57 return NSNumber.alloc().initWithFloat(data);
58 }
59 else {
60 return data;
61 }
62 }
63 }
64 case 'object': {
65 if (data instanceof Date) {
66 return NSDate.dateWithTimeIntervalSince1970(data.getTime() / 1000);
67 }
68 if (!data) {
69 return null;
70 }
71 if (Array.isArray(data)) {
72 return NSArray.arrayWithArray(data.map((el) => dataSerialize(el, wrapPrimitives)).filter((el) => el !== null));
73 }
74 const node = Object.fromEntries(Object.entries(data)
75 .map(([key, value]) => [key, dataSerialize(value, wrapPrimitives)])
76 .filter(([, value]) => value !== null));
77 return NSDictionary.dictionaryWithDictionary(node);
78 }
79 default:
80 return null;
81 }
82}
83// these don't exist on iOS. Stub them to empty functions.
84export const ad = platformCheck('Utils.ad');
85export const android = platformCheck('Utils.android');
86export var ios = iOSUtils;
87/**
88 * @deprecated Use `Utils.ios` instead.
89 */
90export var iOSNativeHelper = iOSUtils;
91//# sourceMappingURL=native-helper.ios.js.map
\No newline at end of file