(data: any, wrapPrimitives: boolean = false)
| 9 | } |
| 10 | |
| 11 | export function serialize(data: any, wrapPrimitives: boolean = false): any { |
| 12 | if (global.isIOS) { |
| 13 | switch (typeof data) { |
| 14 | case 'string': |
| 15 | case 'boolean': { |
| 16 | return data; |
| 17 | } |
| 18 | case 'number': { |
| 19 | const hasDecimals = numberHasDecimals(data); |
| 20 | if (hasDecimals) { |
| 21 | return NSNumber.alloc().initWithDouble(data); |
| 22 | } else { |
| 23 | return NSNumber.alloc().initWithLongLong(data); |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | case 'object': { |
| 28 | if (data instanceof Date) { |
| 29 | return NSDate.dateWithTimeIntervalSince1970(data.getTime() / 1000); |
| 30 | } |
| 31 | |
| 32 | if (!data) { |
| 33 | if (wrapPrimitives) { |
| 34 | return NSNull.new(); |
| 35 | } |
| 36 | return null; |
| 37 | } |
| 38 | |
| 39 | if (Array.isArray(data)) { |
| 40 | return NSArray.arrayWithArray(data.map((el) => serialize(el, wrapPrimitives)).filter((el) => el !== null)); |
| 41 | } |
| 42 | |
| 43 | const node = Object.fromEntries( |
| 44 | Object.entries(data) |
| 45 | .map(([key, value]) => [key, serialize(value, wrapPrimitives)]) |
| 46 | .filter(([, value]) => value !== null) |
| 47 | ); |
| 48 | |
| 49 | return NSDictionary.dictionaryWithDictionary(node); |
| 50 | } |
| 51 | |
| 52 | default: |
| 53 | if (wrapPrimitives) { |
| 54 | return NSNull.new(); |
| 55 | } |
| 56 | return null; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | if (global.isAndroid) { |
| 61 | let store; |
| 62 | switch (typeof data) { |
| 63 | case 'string': |
| 64 | case 'boolean': { |
| 65 | if (wrapPrimitives) { |
| 66 | if (typeof data === 'string') { |
| 67 | return new java.lang.String(data); |
| 68 | } |
no test coverage detected
searching dependent graphs…