-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcasting.js
213 lines (191 loc) · 4.59 KB
/
casting.js
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
function isKindOfClass(arg, nativeClass) {
return (
!!arg && !!arg.isKindOfClass && toBoolean(arg.isKindOfClass(nativeClass))
);
}
function objectToString(o) {
return Object.prototype.toString.call(o);
}
// check if the argument is a native sketch object
function getNativeClass(arg) {
try {
return (
arg &&
arg.isKindOfClass &&
typeof arg.class === "function" &&
String(arg.class())
);
} catch (err) {
return undefined;
}
}
exports.getNativeClass = getNativeClass;
function isNativeObject(arg) {
return !!getNativeClass(arg);
}
exports.isNativeObject = isNativeObject;
/**
* Coerce common NSObjects to their JS counterparts
* @param arg Any object
*
* Converts NSDictionary, NSArray, NSString, and NSNumber to
* native JS equivilents.
*
* Note that NSDictionary and NSArray elements are not recursively converted
* unless the options.recurse is set to `true`
*/
function toJSObject(arg, options) {
if (arg) {
if (isObject(arg)) {
var obj = toObject(arg, options);
if (options && options.recurse) {
Object.keys(obj).forEach(function(k) {
obj[k] = toJSObject(obj[k], options);
});
}
return obj;
} else if (isArray(arg)) {
var arr = toArray(arg, options);
if (options && options.recurse) {
arr.forEach(function(x, i) {
arr[i] = toJSObject(x, options);
});
}
return arr;
} else if (isString(arg)) {
return String(arg);
} else if (isBoolean(arg)) {
return toBoolean(arg);
} else if (isNumber(arg)) {
return Number(arg);
}
}
return arg;
}
exports.toJSObject = toJSObject;
function isArray(ar) {
if (Array.isArray(ar)) {
return true;
}
return isKindOfClass(ar, NSArray);
}
exports.isArray = isArray;
function toArray(object, options) {
if (Array.isArray(object)) {
return object;
}
var arr = [];
for (var j = 0; j < (object || []).length; j += 1) {
arr.push(object[j]);
}
return arr;
}
exports.toArray = toArray;
function isBoolean(arg) {
if (typeof arg === "boolean") {
return true;
}
return getNativeClass(arg) === "__NSCFBoolean";
}
exports.isBoolean = isBoolean;
function toBoolean(arg) {
if (typeof arg === "boolean") {
return arg;
}
return Boolean(Number(arg));
}
exports.toBoolean = toBoolean;
function isNull(arg) {
return arg === null;
}
exports.isNull = isNull;
function isNullOrUndefined(arg) {
return arg == null;
}
exports.isNullOrUndefined = isNullOrUndefined;
function isNumber(arg) {
if (typeof arg === "number") {
return true;
}
return isKindOfClass(arg, NSNumber);
}
exports.isNumber = isNumber;
function isString(arg) {
if (typeof arg === "string") {
return true;
}
return isKindOfClass(arg, NSString);
}
exports.isString = isString;
function isSymbol(arg) {
return typeof arg === "symbol";
}
exports.isSymbol = isSymbol;
function isUndefined(arg) {
return typeof arg === "undefined";
}
exports.isUndefined = isUndefined;
function isRegExp(re) {
return isObject(re) && objectToString(re) === "[object RegExp]";
}
exports.isRegExp = isRegExp;
function isObject(arg) {
if (typeof arg === "object" && arg !== null && !isNativeObject(arg)) {
return true;
}
return isKindOfClass(arg, NSDictionary) || isKindOfClass(arg, MOStruct);
}
exports.isObject = isObject;
function toObject(obj) {
if (isKindOfClass(obj, MOStruct)) {
return obj.memberNames().reduce(function(prev, k) {
prev[k] = obj[k];
return prev;
}, {});
} else if (isNativeObject(obj) && typeof obj.objectForKey === "function") {
var res = {};
Object.keys(obj).forEach(function(key) {
res[key] = obj.objectForKey(key);
});
return res;
} else if (typeof obj === "object") {
return obj;
}
return Object(obj);
}
exports.toObject = toObject;
function isDate(d) {
return isObject(d) && objectToString(d) === "[object Date]";
}
exports.isDate = isDate;
function isError(e) {
return (
isObject(e) &&
(objectToString(e) === "[object Error]" || e instanceof Error)
);
}
exports.isError = isError;
function isFunction(arg) {
return typeof arg === "function" || arg instanceof MOMethod;
}
exports.isFunction = isFunction;
function isPrimitive(arg) {
return (
isNull(arg) ||
isBoolean(arg) ||
isNumber(arg) ||
isString(arg) ||
isSymbol(arg) ||
isUndefined(arg)
);
}
exports.isPrimitive = isPrimitive;
exports.isBuffer = function isBuffer(arg) {
return (
arg &&
typeof arg === "object" &&
typeof arg.copy === "function" &&
typeof arg.fill === "function" &&
typeof arg.readUInt8 === "function"
);
};