forked from PeterStaev/nativescript-azure-mobile-apps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ios.ts
110 lines (82 loc) · 3.23 KB
/
utils.ios.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
/*! *****************************************************************************
Copyright (c) 2016 Tangra Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
***************************************************************************** */
export function getNativeValueForComparison(value: string|number|boolean|Date): any {
if (value instanceof Date) {
// There is a problem with {N} wrapping NSDate so we just use ISO String to filter
return (<Date>value).toISOString();
}
return value;
}
export function getNativeObject(object: any): any {
if (object instanceof Array) {
return getNSArrayFromJsArray(object);
}
if (object !== null && object !== undefined && typeof object === "object" && !(object instanceof Date)) {
return getNSDictionaryFromJsObject(object);
}
return object;
}
function getNSArrayFromJsArray(array: Array<any>): NSArray<any> {
let result = NSMutableArray.alloc().init();
for (let loop = 0; loop < array.length; loop++) {
result.addObject(getNativeObject(array[loop]));
}
return result;
}
function getNSDictionaryFromJsObject(object: Object): NSDictionary<any, any> {
let result = NSMutableDictionary.alloc().init();
for (let key in object) {
if (object.hasOwnProperty(key)) {
result.setValueForKey(getNativeObject(object[key]), key);
}
}
return result;
}
export function getJsObject(object: any): any {
if (object instanceof NSDictionary) {
return getJsObjectFromNSDictionary(object);
}
if (object instanceof NSArray) {
return getJsArrayFromNSArray(object);
}
return object;
}
function getJsArrayFromNSArray(array: NSArray<any>): Array<Object> {
let result = [];
for (let loop = 0; loop < array.count; loop ++) {
result.push(getJsObject(array.objectAtIndex(loop)));
}
return result;
}
function getJsObjectFromNSDictionary(dictionary: NSDictionary<any, any>): Object {
let keys = dictionary.allKeys;
let result = {};
for (let loop = 0; loop < keys.count; loop++) {
let key = keys[loop];
let item = dictionary.objectForKey(key);
result[key] = getJsObject(item);
}
return result;
}
export function deviceTokenToNsData(token: string): NSData {
let result = NSMutableData.alloc().init();
for (let loop = 0; loop < token.length / 2; loop++) {
let byteChars = "";
let byte: number;
byteChars = `${token[loop * 2]}${token[loop * 2 + 1]}\0`;
byte = strtol(byteChars, null, 16);
let pointer = new interop.Reference(interop.types.unichar, String.fromCharCode(byte));
result.appendBytesLength(pointer, 1);
}
return result;
}